Backup and restore data on Linux
This article explains how to back up and restore data on Linux systems. We cover different tools liketar, rsync, and dd, as well as cloud services such as Google Drive and Amazon S3. The guide covers both the backup and restore procedures for each tool.
Keep a copy outside this VPS and test restoring it. Files copied to another directory on the same virtual disk do not protect you against disk erasure or service deletion. Stop application writes or use an application-consistent backup method; databases should use their supported dump or backup tools.
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.
You can also use tar for incremental backups by archiving only files that have changed since the last backup. This 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
- Open Terminal
- To back up a specific folder (e.g.,
/home/user/data), use the following command:
Bash
-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:
Restore steps
- Open Terminal
- To restore data from a backup file to a specific directory, run the following command:
Bash
-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
- Open Terminal
- To back up data from a specific folder, run the following command:
Bash
-a: Archive mode, preserves file attributes like permissions and timestamps.-v: Show detailed progress.-z: Compress data for faster transfer.
Bash
Restore using rsync
To restore data from a backup made with rsync, use the same command structure for restoring files.
Restore steps
- Open Terminal
- To restore data from a backup directory, run:
Bash
Bash
3. Backup using dd
dd copies raw blocks. Its output is a raw disk image, not a bootable ISO installer or a QCOW2 image.
Run lsblk -o NAME,SIZE,FSTYPE,MOUNTPOINT and positively identify the source, destination, and their sizes before either command. /dev/sda below is a placeholder example; an EDIS VPS may use /dev/vda or another name. The restore target must be at least as large as the original disk.
Backup steps
- Open Terminal
- To create a backup of a disk, use:
Bash
if: Input file (the source disk or partition).of: Output file (backup destination).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
- Open Terminal
- To restore data to the target disk, run:
Bash
4. Backup using cloud services with rclone
If you use cloud services like Google Drive, Amazon S3, or Dropbox for storing backups, you can use the rclone tool. It 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.
If you 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
- Open Terminal
- To back up data to a cloud service, use:
Bash
Restore steps
- Open Terminal
- To restore data from the cloud service, use:
Bash