Copying and Backing Up with rsync

Why not just cp?

cp -r copies a folder, but it copies everything every time. rsync is cleverer: it copies only what has changed, keeps permissions and timestamps, and can pick up where it left off. It's the standard tool for backups and for copying folders between machines. -a (archive) is the everyday option, and -v shows the files:

rsync -av site/ backup/
ls backup

In the terminal

student@vulntech:~$ rsync -av site/ backup/
sending incremental file list
index.html
style.css
images/
images/logo.png

sent 20,314 bytes  received 85 bytes  13,599.33 bytes/sec
total size is 20,035  speedup is 0.98
student@vulntech:~$ ls backup
images  index.html  style.css

The trailing slash matters

This is the one rsync rule everyone has to learn. A slash after the source means the contents of this folder; no slash means the folder itself:

CommandResult
rsync -a site/ backup/The files in site go directly into backup
rsync -a site backup/The folder is copied as backup/site
rm -rf backup/*
rsync -a site backup/
ls backup
ls backup/site

In the terminal

student@vulntech:~$ rm -rf backup/*
student@vulntech:~$ rsync -a site backup/
student@vulntech:~$ ls backup
site
student@vulntech:~$ ls backup/site
images  index.html  style.css

Copy only what changed

Run rsync again after changing one file, and it transfers just that file. This is what makes it fast for repeated backups:

rsync -a site/ backup/
echo '<h1>Updated</h1>' > site/index.html
rsync -av site/ backup/

In the terminal (only the changed file is listed the second time)

student@vulntech:~$ rsync -a site/ backup/
student@vulntech:~$ echo '<h1>Updated</h1>' > site/index.html
student@vulntech:~$ rsync -av site/ backup/
sending incremental file list
index.html

sent 205 bytes  received 36 bytes  160.67 bytes/sec
total size is 20,038  speedup is 83.15

Mirroring: --delete

By default rsync only adds and updates; it never removes. --delete makes the destination an exact mirror of the source, deleting anything that's no longer in the source. That's powerful and dangerous, so try it with -n (dry run) first: -n shows what would happen and changes nothing.

rsync -a site/ backup/
rm site/style.css
rsync -avn --delete site/ backup/

In the terminal (a dry run: see what --delete would remove)

student@vulntech:~$ rsync -a site/ backup/
student@vulntech:~$ rm site/style.css
student@vulntech:~$ rsync -avn --delete site/ backup/
sending incremental file list
deleting style.css

sent 123 bytes  received 33 bytes  104.00 bytes/sec
total size is 20,014  speedup is 128.29 (DRY RUN)

Happy with the plan? Run it again without -n to actually mirror.

Common mistake: --delete can wipe files if you point it the wrong way or get the trailing slash wrong. Always do a -n dry run first, and double-check which side is the source and which is the destination.

Remember: rsync -av src/ dst/ copies only what changed. A trailing / on the source means its contents. --delete mirrors; test it with -n first.

HOME LEARN COMMUNITY DASHBOARD
Practice terminal Starting…