rsync
rsync copies files between directories or computers and transfers only what changed, which makes repeated copies and backups fast. It works locally or over SSH and keeps permissions, times and links with the archive option -a.
1Common options
-a (archive) keeps permissions, times, owners and symbolic links and copies recursively. -v shows each file, -h prints sizes readably, -z compresses over the network and --progress shows progress.
2The trailing slash
rsync -a src/ dest/ copies the contents of src into dest. rsync -a src dest/ copies the directory itself, creating dest/src. Getting this wrong is the most common rsync mistake.
3--delete and --dry-run
--delete removes files in the destination that no longer exist in the source, making an exact mirror. --dry-run (-n) shows what would happen without changing anything, and should always be used first with --delete.
How to back up a folder to a server
- Test first:
rsync -avhn --delete ~/Documents/ user@server:/backup/Documents/ - Read the list of files that would be copied or deleted.
- Run it for real:
rsync -avh --delete ~/Documents/ user@server:/backup/Documents/ - Repeat the same command later; only changes are transferred.
Cautions
- With
--delete, a wrong source path can empty the destination; always run with -n first. - A mirror is not a backup history: deleted or damaged files are mirrored too.
Related articles
- SSH keysHow SSH key authentication works, how to create a key with ssh-keygen, install it on a server and protect it.
- Finding large and old files with findHow to use find to list big or old files on Linux, preview them safely and delete only what you checked.
- cron and crontabHow cron runs scheduled jobs, the five crontab time fields, where system cron files live and how systemd timers compare.
Sources
- rsync(1) manual page download.samba.org
- rsync project page rsync.samba.org
Last reviewed: