Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using rsync for Backups

Introduction

The rsync command in Linux is a powerful tool for backing up and synchronizing files and directories. It is a versatile utility that supports incremental backups, copying over SSH, preserving file permissions, and more. This tutorial will guide you through using rsync for backups, from basic commands to more advanced use cases.

Installing rsync

Most Linux distributions come with rsync pre-installed. You can check if rsync is installed on your system by running:

rsync --version

If it is not installed, you can install it using your package manager. For example:

sudo apt-get install rsync
sudo yum install rsync

Basic Usage

The basic syntax for rsync is:

rsync [options] source destination

For example, to copy a directory from one location to another on the same machine:

rsync -av /path/to/source /path/to/destination

Explanation of options:

  • -a: Archive mode, which preserves permissions, symlinks, and other attributes.
  • -v: Verbose mode, which provides detailed output.

Using rsync over SSH

One of the powerful features of rsync is its ability to copy files over SSH, making it suitable for remote backups. The syntax is similar but includes the remote host information:

rsync -av -e ssh /path/to/source user@remote_host:/path/to/destination

For example, to back up a directory to a remote server:

rsync -av -e ssh /home/user/documents user@192.168.1.100:/backup/documents

Incremental Backups

Incremental backups only copy files that have changed since the last backup, saving time and bandwidth. By default, rsync performs incremental backups:

rsync -av /path/to/source /path/to/destination

To use rsync for a more advanced incremental backup, you can use the --link-dest option:

rsync -av --link-dest=/previous/backup /path/to/source /path/to/new/backup

Excluding Files and Directories

You can exclude specific files or directories from the backup using the --exclude option:

rsync -av --exclude='*.tmp' /path/to/source /path/to/destination

To exclude multiple patterns, you can use multiple --exclude options or an exclude file:

rsync -av --exclude-from='exclude.txt' /path/to/source /path/to/destination

Where exclude.txt contains patterns to exclude, one per line.

Preserving File and Directory Attributes

To preserve file permissions, modification times, and other attributes, use the -a option. If you need to preserve hard links, use the -H option:

rsync -aH /path/to/source /path/to/destination

Deleting Files

To make the destination an exact copy of the source, including deleting files that no longer exist in the source, use the --delete option:

rsync -av --delete /path/to/source /path/to/destination

Be cautious with this option, as it will permanently delete files from the destination.

Conclusion

rsync is a powerful and flexible tool for backups and file synchronization. By understanding and utilizing its various options, you can create efficient and reliable backup solutions tailored to your needs. Whether you're performing local backups or syncing files to a remote server, rsync provides the performance and reliability required for the task.