Skip to main content

Backup and Restore Data on Linux

In this article, we will explain how to back up and restore data on Linux systems using different tools like tar, rsync, dd, and cloud services such as Google Drive and Amazon S3. The guide covers both the backup and restore procedures for each tool.

1. Backup Using tar

One of the most commonly used tools for backing up files and directories in Linux is the tar command. It allows you to easily compress files and store them in an archive, helping you save important files and directories for later restoration. Tar can also be used for incremental backups by archiving only files that have changed since the last backup, which saves storage space and reduces backup time. Note: tar archives do not include certain metadata like boot records. A common use case is backing up the home directory.

Backup Steps:

  1. Open Terminal
  2. To backup a specific folder (e.g., /home/user/data), use the following command:
Bash
tar -cvpzf /path/to/backup/backup_file.tar.gz /home/user/data
Explanation:
  • -c: Create a new archive.
  • -v: Show detailed operations.
  • -p: Preserve file permissions.
  • -z: Compress using gzip.
  • -f: Specify the archive file name.

Restore Using tar

To restore data from a .tar.gz archive file, use the following command:
tar -xzvf backup.tar.gz -C /destination/directory
This restoration process with tar will return your files to a previous state by extracting them from the backup archive. Restoring from a tar archive will replace any existing files in the destination directory with the versions from the backup, effectively reverting the system or files to their earlier configuration.

Restore Steps:

  1. Open Terminal
  2. To restore data from a backup file to a specific directory, run the following command:
Bash
tar -xvpzf /path/to/backup/backup_file.tar.gz -C /path/to/restore/
Explanation:
  • -x: Extract files from the archive.
  • -v: Show extraction details.
  • -p: Preserve permissions.
  • -z: Decompress with gzip.
  • -C: Specify the destination directory for extraction.

2. Backup Using rsync

rsync is a reliable and useful tool for backups, allowing you to sync data between directories, or even between servers. Rsync is commonly used to sync files and directories for backup purposes, ensuring data consistency and integrity.

Backup Steps:

  1. Open Terminal
  2. To backup data from a specific folder, run the following command:
Bash
rsync -avz /path/to/source/ /path/to/backup/
Explanation:
  1. -a: Archive mode, preserves file attributes like permissions and timestamps.
  2. -v: Show detailed progress.
  3. -z: Compress data for faster transfer.
To backup from a remote server, use:
Bash
rsync -avz user@remote_server:/path/to/source/ /path/to/backup/

Restore Using rsync

To restore data from a backup made with rsync, use the same command structure for restoring files.

Restore Steps:

  1. Open Terminal
  2. To restore data from a backup directory, run:
Bash
rsync -avz /path/to/backup/ /path/to/restore/
To restore data from a remote server:
Bash
rsync -avz user@remote_server:/path/to/backup/ /path/to/restore/

3. Backup Using dd

The dd tool is used for making complete backups of disks or partitions (for example, for backing up an operating system or a specific partition). dd is commonly used for cloning entire disks or partitions, making an exact clone of a machine or host. It supports backing up the entire file system, including important system directories and logs. dd can also be used to create an ISO image of a disk for backup or migration purposes. Before starting the backup process, ensure proper setup to avoid data loss and ensure a reliable restore. dd can be used to back up the entire computer for disaster recovery or migration to new hardware. When handling disk images, consider security, as these images may contain sensitive data from your system.

Backup Steps:

  1. Open Terminal
  2. To create a backup of a disk, use:
Bash
dd if=/dev/sda of=/path/to/backup/backup.img bs=64K
Explanation:
  1. if: Input file (the source disk or partition).
  2. of: Output file (backup destination).
  3. bs: Block size (in this case, 64K).

Restore Using dd

To restore data from a disk or partition backup created with dd, use the following command for restoring. This restoration process will replace all data on the target disk with the backup image.

Restore Steps:

  1. Open Terminal
  2. To restore data to the target disk, run:
Bash
dd if=/path/to/backup/backup.img of=/dev/sda bs=64K

4. Backup Using Cloud Services with rclone

If you use cloud services like Google Drive, Amazon S3, or Dropbox for storing backups, the rclone tool supports backing up and restoring data to/from these services for off-site backups. Rclone can sync and copy files between your local system and remote locations, and it supports encrypted backups to enhance data security through encryption. When configuring cloud backups, make sure to specify the correct location for your data. For users who prefer a graphical interface, GUI programs like Deja Dup are available. Deja Dup (a graphical interface for Duplicity)**, **supports incremental and encrypted backups, and integrates with cloud storage.

Backup Steps:

  1. Open Terminal
  2. To back up data to a cloud service, use:
Bash
rclone copy /path/to/data remote:backup_folder

Restore Steps:

  1. Open Terminal
  2. To restore data from the cloud service, use:
Bash
rclone copy remote:backup_folder /path/to/restore/
I