Technology Encyclopedia Home >How to backup and restore data on Linux?

How to backup and restore data on Linux?

To backup and restore data on Linux, you can use several commands and tools. One common method is using the tar command to create backups and the rsync command for restoring or transferring data.

Backup with tar:

The tar command is used to create an archive file, which can include many files and directories. Here’s how you can use it for backup:

tar -cvpzf backup.tar.gz /path/to/directory
  • -c: Creates a new archive.
  • -v: Shows the progress in the terminal.
  • -p: Preserves the permissions of the files.
  • -z: Compresses the archive with gzip.
  • -f: Specifies the filename of the archive.

Example: To back up the /home/user/documents directory:

tar -cvpzf documents_backup.tar.gz /home/user/documents

Restore with tar:

To restore data from a tar archive:

tar -xvpzf backup.tar.gz -C /path/to/restore/
  • -x: Extracts files from an archive.
  • -C: Changes to the specified directory before performing any operations.

Example: To restore the documents_backup.tar.gz to /home/user/:

tar -xvpzf documents_backup.tar.gz -C /home/user/

Backup and Restore with rsync:

rsync is a utility for efficient remote file transfer and synchronization. It can also be used for local backups and restores.

Backup:

rsync -av /path/to/source/ /path/to/backup/
  • -a: Archive mode, which preserves permissions, symlinks, etc.
  • -v: Verbose mode.

Restore:

rsync -av /path/to/backup/ /path/to/restore/

Cloud Storage for Backup:

For enhanced backup solutions, consider using cloud storage services. Tencent Cloud offers services like Tencent Cloud Object Storage (COS), which provides a reliable and scalable storage solution for backups. You can use coscmd or SDKs provided by Tencent Cloud to upload and download your backups to and from COS.

This approach ensures your data is not only backed up locally but also securely stored in the cloud, providing an additional layer of protection against data loss.