How to Back Up a Linux Directory with rsync
In this article
To back up a Linux directory to a remote machine with rsync, run: rsync -avz /path/to/source user@remote:/path/to/destination. This copies the directory over SSH, preserving permissions and timestamps, and only transfers changed files on subsequent runs. You’ll need rsync installed on both machines and SSH access to the remote host.
What You'll Need
- Two Linux machines (or a Linux machine and a remote server) with rsync installed. On most distros it’s preinstalled; if not, install it with your package manager (e.g.,
sudo apt install rsyncon Debian/Ubuntu,sudo dnf install rsyncon Fedora). - SSH access to the remote machine: either a password or, better, an SSH key set up for passwordless login.
- Write permission on the remote destination directory.
- Enough disk space on the remote machine to hold the backup.
Step-by-Step
- Verify rsync is installed on both machines. On the local machine, run
rsync --version. On the remote machine, SSH in and run the same command. If it’s missing, install it on that machine too. - Set up SSH key authentication (recommended). If you haven’t already, generate an SSH key pair on the local machine with
ssh-keygen -t ed25519(press Enter to accept defaults). Then copy the public key to the remote machine withssh-copy-id user@remote. This lets rsync run without prompting for a password each time. - Test SSH connectivity. Run
ssh user@remoteto confirm you can log in. If it fails, check the remote hostname/IP, username, and that SSH is running on the remote machine. - Create the destination directory on the remote machine. SSH in and run
mkdir -p /path/to/backup/destination. If the directory doesn’t exist, rsync will create it, but creating it first avoids permission surprises. - Run a dry run first. Use
rsync -avz --dry-run /path/to/source user@remote:/path/to/destinationto see what would be copied without actually transferring anything. Check the output for any obvious errors (like permission denied). - Execute the backup. Remove
--dry-runand run the actual command:rsync -avz /path/to/source user@remote:/path/to/destination. The-a(archive) preserves permissions, timestamps, and symlinks;-vgives verbose output;-zcompresses data during transfer. - Verify the backup. SSH to the remote machine and list the destination directory:
ls -la /path/to/destination. Check that the files and subdirectories are present and have the expected sizes. - Automate with cron (optional). To run the backup regularly, create a cron job. Edit your crontab with
crontab -eand add a line like0 2 * * * rsync -avz /path/to/source user@remote:/path/to/destinationto run daily at 2 AM. Make sure the SSH key is set up so cron can run without a password prompt.
Common Mistakes
- Forgetting the trailing slash on the source.
rsync -avz /source user@remote:/destcopies the source directory itself into/dest, creating/dest/source. If you want the contents of/sourceto go directly into/dest, use/source/(with trailing slash). - Not using
-a. Without archive mode, file permissions and timestamps are not preserved, which can break applications that rely on them. - Running rsync as root when you don’t need to. This can change file ownership on the remote side. Use your normal user unless you specifically need root.
- Ignoring the destination path. If the destination directory doesn’t exist, rsync creates it, but if the parent path is not writable, you’ll get an error. Always create the destination first and check permissions.
- Not testing with
--dry-run. A dry run catches typos and permission issues before you transfer gigabytes of data.
Tips
- Use
--deletecarefully. Adding--deletemakes rsync remove files in the destination that no longer exist in the source, keeping the backup an exact mirror. Only use this if you want the remote to exactly match the source; otherwise, you might accidentally delete files you wanted to keep. - Use
--progressfor large transfers. It shows per-file progress and speeds, which helps you estimate time remaining. - Use
--excludeto skip unnecessary files. For example,--exclude='*.tmp'or--exclude='cache/'reduces transfer time and storage. - Consider using
--partial. If a transfer is interrupted,--partialkeeps partially transferred files so the next run can resume them instead of starting over. - Set up SSH keys for automation. Cron jobs and scripts won’t work if rsync prompts for a password. Use
ssh-copy-idto install your public key.
Troubleshooting
“rsync: connection refused” or “Connection timed out.” The remote SSH service may not be running, or a firewall is blocking port 22. Check with ssh user@remote – if that fails, verify the remote machine is up and SSH is enabled (on the remote, run sudo systemctl status ssh).
“Permission denied (publickey).” Your SSH key isn’t authorized on the remote. Run ssh-copy-id user@remote again, or check that the public key is in ~/.ssh/authorized_keys on the remote.
“rsync: mkdir: Permission denied.” The destination parent directory isn’t writable by your user. Create the directory manually with sudo mkdir -p /path/to/dest and then sudo chown youruser: /path/to/dest, or choose a different destination.
“rsync: command not found.” rsync isn’t installed on the remote machine. Install it via your package manager (e.g., sudo apt install rsync on Debian/Ubuntu).
Transfer is very slow. If you have a slow link, try removing -z (compression can slow things down on fast networks) or use --bwlimit to throttle if the transfer is saturating your connection.
Changes don’t appear on the remote. Check the destination path – remember the trailing slash rule. Also verify you’re running the command from the correct local directory.
Frequently Asked Questions
Does rsync work over the internet?
Yes, as long as SSH is accessible on the remote machine (port 22 open) and you have the correct hostname/IP and credentials. For security, consider using a VPN or SSH key authentication instead of passwords.
Can I back up to a Windows machine?
Not directly with rsync over SSH, because Windows doesn’t natively run an SSH server that supports rsync. You could install an SSH server on Windows (like OpenSSH) and use rsync, but it’s often easier to use a Linux server or a NAS that supports rsync.
What’s the difference between rsync and scp?
rsync is designed for efficient synchronization: it only transfers changed parts of files, preserves attributes, and can resume interrupted transfers. scp simply copies files without these features. For backups, rsync is almost always the better choice.
How do I make a full backup that includes hidden files?
rsync includes hidden files by default. The -a flag preserves them. If you use a wildcard like * in the source, hidden files are skipped, but with a directory path (like /home/user/), they are included.
Can I run rsync as a cron job without a password?
Yes, if you set up SSH keys. Use ssh-copy-id to install your public key on the remote, then the cron job will run without prompting for a password. Ensure the key is for the user that owns the cron job.
What does --delete do exactly?
It tells rsync to remove files in the destination that are not present in the source. This makes the destination an exact mirror of the source. Use it only if you want the backup to be identical to the source; otherwise, old files remain.