If you manage an Ubuntu server, one of the most important maintenance tasks is creating reliable backups. Copying entire directories to a backup drive using cp works, but it is brutally slow because it copies every single file from scratch every time, even if only one small file has changed since the last backup.
The rsync command is the industry-standard tool for efficient file synchronisation and backups on Linux. Instead of copying everything, rsync uses a delta-transfer algorithm that compares the source and destination and only transfers the files that have actually changed. This makes subsequent backups dramatically faster, as rsync skips unchanged files entirely.
How to Perform a Basic Local Backup
At its simplest, rsync mirrors one directory to another location on the same machine.
- Open your Ubuntu terminal.
- Type the following command:
rsync -av /home/user/documents/ /mnt/backup/documents/ - Press Enter.
Let us break down the flags:
-a(archive): This is the most important flag. It preserves file permissions, ownership, timestamps, and symbolic links, ensuring your backup is an exact replica of the original.-v(verbose): Displays a detailed list of every file being transferred in real-time, so you can monitor progress.
Critical note: The trailing forward slash / at the end of the source path matters. /home/user/documents/ copies the contents of the directory. Omitting the slash (/home/user/documents) copies the directory itself, creating /mnt/backup/documents/documents/.
How to Perform a Backup to a Remote Server
Rsync can also transfer files over the network to a remote backup server via SSH, encrypting the data in transit.
rsync -avz /home/user/documents/ [email protected]:/backups/documents/
-z(compress): Compresses the file data during the transfer, reducing bandwidth usage over the network.[email protected]:The SSH credentials and IP address of the remote backup server, followed by the destination directory path.
You will be prompted for the remote server’s SSH password (or authenticated automatically if you have SSH keys configured).
How to Create an Exact Mirror (Delete Extra Files)
A standard rsync backup only adds new or changed files to the destination. If you delete a file from the source, it will continue to exist in the backup. To make the backup a perfect, exact mirror of the source, add the --delete flag:
rsync -av --delete /home/user/documents/ /mnt/backup/documents/
Warning: The --delete flag will permanently remove any file from the backup that no longer exists in the source. Always perform a dry run first to preview what will be deleted.
How to Perform a Dry Run
Before running a potentially destructive backup command, you can add the --dry-run (or -n) flag to simulate the entire operation without actually changing any files.
rsync -avn --delete /home/user/documents/ /mnt/backup/documents/
Rsync will print a complete list of every file that would be transferred or deleted, allowing you to review the actions and verify they are correct before committing.