Your first backup with rsync
Your files live in one place only: one dead disk and they are gone. Here is how to make a backup with rsync, copy it to your server, prove that it works and let it run by itself.
- Course
- Tutorial 15 of 35
- Time
- 25 min to read
- Tested with
- Debian 13.7 (trixie), rsync 3.5.0
What you will be able to do
- Copy a folder with rsync, and send only what changed.
- Avoid the trailing slash trap and try a risk-free copy with --dry-run.
- Send the copy to your server over SSH and check it.
- Restore a file, then run the copy every night with cron.
Before you start: Secure SSH: turn off passwords.
Contents
How do you make a backup with rsync, and be sure it's worth anything? You copy a folder, send it to the server, then restore a file to prove it. Your server now accepts your SSH key, as covered in Secure SSH: turn off passwords. It is about to become the second place where your files live.
Why back up, and where to
A backup is a copy of your files, kept somewhere other than the originals. A dying disk, one command too many or a virus can take the originals, but not a copy stored elsewhere. A copy on the same disk therefore doesn't protect you from that disk failing.
People often remember the 3-2-1 rule: three copies of your data, on two different media, with one away from home. Here we take a first step: a second copy, on your server. The tool is called rsync ("remote sync"). It copies one folder to another, and transfers only what changed since last time.
To practice safely, we first build a fake folder, then copy it into another folder on your computer.
1Make a first copy
The rsync command reads "source, then destination". Let's start by building the documents folder in your home folder (the one where a terminal opens). It holds three small files, one of them in a subfolder.
mkdir -p documents/photos
echo "liste de courses" > documents/notes.txt
echo "rapport de mars" > documents/rapport.txt
echo "photo de chat" > documents/photos/chat.jpg
ls -R documentsdocuments:
notes.txt
photos
rapport.txt
documents/photos:
chat.jpgIf rsync isn't installed on your Debian computer, install it with sudo apt install rsync. Now let's copy this folder into a new folder called sauvegarde, with two options (settings added after the command name).
-a("archive") copies subfolders and keeps the dates and permissions of the files;-v("verbose") prints each file it copies.
rsync -av documents/ sauvegarde/sending incremental file list
created directory sauvegarde
./
notes.txt
rapport.txt
photos/
photos/chat.jpgTwo summary lines follow: the amount sent in bytes (the unit for file sizes) and the speed of the transfer. Their numbers change every time. The sauvegarde folder now holds the same files as documents. You may have noticed the / after documents: that tiny sign changes everything, as we're about to see.
2The trailing slash trap
A / at the end of the source folder means "copy its contents". Without it, rsync copies the folder itself. The two commands look alike, and the result differs by one level of folder.
| Command | What rsync copies |
|---|---|
rsync -a documents/ copie-a/ | the contents of documents: copie-a/notes.txt |
rsync -a documents copie-b/ | the folder itself: copie-b/documents/notes.txt |
Let's try both, then look at what landed in each folder.
rsync -a documents/ copie-a/ && ls copie-anotes.txt
photos
rapport.txtrsync -a documents copie-b/ && ls copie-bdocumentsIn copie-b, everything sits one floor down, inside a documents folder. That isn't dangerous here, but it becomes so with --delete, an option that erases things and that we'll meet later. The / matters on the source only: on the destination, with or without it, the result is the same. Let's clean up these two tests, then see what happens when we copy again.
rm -r copie-a copie-b3Repeat the copy, risk-free
When you run rsync again, it compares the two folders and copies only the new or modified files. We'll change one file and add another. Before copying for real, we ask rsync to simulate with --dry-run: it announces what it would do, without touching anything.
echo "du pain" >> documents/notes.txt
echo "photo de chien" > documents/photos/chien.jpg
rsync -av --dry-run documents/ sauvegarde/sending incremental file list
notes.txt
photos/chien.jpgThe summary ends with "(DRY RUN)": nothing was copied. Only the two touched files are listed, the others are already up to date. Happy with the simulation? Run the same command without --dry-run.
rsync -av documents/ sauvegarde/sending incremental file list
notes.txt
photos/chien.jpgIf you ran the same command a third time, the list would be empty: everything is identical, so rsync has nothing to do. That's what makes the tool fast, even on big folders. Get into the habit of simulating before any new command. Now for two trickier settings.
4Exclude a folder, delete with care
Two settings come up often, and the second one demands care:
- Exclude:
--exclude cachetells rsync to ignore any file or folder namedcache. Handy for what you can rebuild, like temporary files. - Delete:
--deleteerases from the copy whatever no longer exists in the original. The copy becomes an exact mirror.
Let's create a cache folder inside documents. First, with no exclusion, rsync wants to copy it.
mkdir documents/cache
echo "fichier temporaire" > documents/cache/temp.dat
rsync -av --dry-run documents/ sauvegarde/sending incremental file list
cache/
cache/temp.datWith the exclusion, the list is empty again, since nothing else changed.
rsync -av --dry-run --exclude cache documents/ sauvegarde/sending incremental file listLet's try --delete: we remove rapport.txt from the original, then simulate. The command reads: simulate, with deletion, without the cache folder. The simulation announces the deletion in the copy.
rm documents/rapport.txt
rsync -av --dry-run --delete --exclude cache documents/ sauvegarde/sending incremental file list
deleting rapport.txt
./The simulation matches what we want. Let's run it for real: rapport.txt disappears from sauvegarde.
rsync -av --delete --exclude cache documents/ sauvegarde/ && ls sauvegardesending incremental file list
deleting rapport.txt
./
notes.txt
photosThe local copy works, but it lives on the same disk as the original. This is the end of the part on your computer, so feel free to take a break. Next, we send the copy elsewhere, to your server.
5Send the copy to the server
To copy to another computer, rsync goes through SSH: the destination is written user@address:folder. It also needs rsync on the server, because one rsync talks to another rsync. Install it on the server, then check its version.
sudo apt install rsyncrsync --version | head -1rsync version 3.5.0 protocol version 32Back on your computer, send documents to a sauvegarde folder of the server's alice user (replace it with your own user name), using your key. The folder is created if it doesn't exist. The path sauvegarde/, with no / at the start, begins in alice's home folder on the server.
rsync -av --exclude cache documents/ alice@203.0.113.10:sauvegarde/sending incremental file list
created directory sauvegarde
./
notes.txt
photos/
photos/chat.jpg
photos/chien.jpg"created directory sauvegarde" means the folder was just created on the server. Your files are there. Now to prove it.
6Check, then restore a file
A backup you've never read back is only a promise. First check: replay the command as a simulation. An empty list means the server has the same files as you, with the same sizes and the same dates.
rsync -av --dry-run --exclude cache documents/ alice@203.0.113.10:sauvegarde/sending incremental file listLet's also look at the contents of the folder on the server. After the machine name, SSH runs the ls -R command over there and brings the result back here.
ssh alice@203.0.113.10 ls -R sauvegardesauvegarde:
notes.txt
photos
sauvegarde/photos:
chat.jpg
chien.jpgCompare file contents, not just size and date
By default, rsync decides two files are identical when their size and modification date match. The -c option ("checksum") compares the contents instead. It is slower, because every file is read in full.
rsync -avc --dry-run --exclude cache documents/ alice@203.0.113.10:sauvegarde/sending incremental file listSecond check, the most important one: restore a file. Let's pretend you lost notes.txt. To get it back, swap source and destination: the source is now on the server.
rm documents/notes.txt
rsync -av alice@203.0.113.10:sauvegarde/notes.txt documents/
cat documents/notes.txtreceiving incremental file list
notes.txt
liste de courses
du painThe file is back with its contents, including the "du pain" line added earlier. Your backup is proven, not just assumed. One weak spot remains: all of this depends on you remembering to do it.
7Run the copy by itself
cron is Debian's scheduler: a program that runs a command at the time you set. Open your task list with crontab -e (the first time, it offers an editor: pick one) and add this line at the bottom.
0 3 * * * rsync -a --exclude cache /home/alice/documents/ alice@203.0.113.10:sauvegarde/The first five fields say when, from left to right:
- minute (0), hour (3): at 3:00 a.m.;
- day of the month, month, day of the week: three stars mean "every", so every day.
The command follows. It has no -v because nobody reads it, and the path starts with /home/alice (replace it with the output of the pwd command typed in your home folder) so it doesn't depend on any current folder. We leave --delete out: an automatic backup that erases without asking would copy your mistakes. The next morning, check that the server is up to date with the command from step 6.
Keep a trace of each copy
Without a log, a copy that fails says nothing. Add >> ~/sauvegarde.log 2>&1 at the end of the cron line: everything rsync prints, errors included, is then written to that file. Read it with tail ~/sauvegarde.log.
The backup now runs by itself. If something went wrong along the way, here are the most common errors.
If it fails
Three messages come back most often, plus one silent trap.
rsync: command not found: rsync is missing on the server. Install it as in step 5.Permission deniedwhen creating the folder: alice isn't allowed to write there. Pick a folder inside her home folder, likesauvegarde/.Permission denied (publickey): the server refuses the key. Go back to step 2 of Secure SSH: turn off passwords.- A copy with one folder too many: that's the trailing slash. Compare with the table in step 2.
Here is the first message in full, as you see it when rsync is missing on the server.
rsync -av --exclude cache documents/ alice@203.0.113.10:sauvegarde/bash: line 1: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(285) [sender=3.5.0]And the second, when the target folder sits in an area where alice can't write, here /etc.
rsync -av --exclude cache documents/ alice@203.0.113.10:/etc/sauvegarde/sending incremental file list
rsync: [Receiver] mkdir "/etc/sauvegarde" failed: Permission denied (13)
rsync error: error in file IO (code 11) at main.c(808) [Receiver=3.5.0]An excluded folder stays in the copy
--delete doesn't remove an excluded folder: if it was already in the copy, it stays there. Add --delete-excluded only if you really want it gone.
What's next
You now have a copy of your files on the server, checked by a restore, and renewed every night. That's the second copy of the 3-2-1 rule. Your server still has only a number, 203.0.113.10, which is hard to remember. The next article, Domain name and DNS, gives it a readable name. It comes at the right time: everything after that (website, HTTPS, apps) rests on that name.