Blog about Web Programming and Electronics
💬 👤
back Back to blog
Linux

How to Make a Backup in Linux: rsync, tar and cron

A backup is the only defense against data loss — a disk failure, a mistake with the rm command, a virus, or a stolen computer can destroy years of work in an instant. In this article, we will look at how to make a backup in Linux using tools already present in the system: rsync, tar, and cron. No additional software, no cloud, no costs.

The Golden Rule: 3-2-1

Before we get to the tools, let's look at the widely accepted rule of backups:

  1. 3 copies of the data — the original plus two backups
  2. 2 different media — e.g., an internal disk and an external USB disk
  3. 1 copy off-site — at a friend's place, in a safe, or in an encrypted cloud

Why so many copies? Because unexpected things fail: an external disk sitting next to the computer burns with it in case of fire or burglary.

rsync — The King of Synchronization

rsync is the most popular backup tool in Linux. Its biggest advantage is that it transfers only the changed parts of files — the first copy takes a long time, but all subsequent ones are fast.

# Basic copy of the home folder to an external disk
rsync -av ~/ /media/user/external-drive/backup/

# Explanation of the switches:
# -a (archive) preserves permissions, timestamps, symbolic links
# -v (verbose) prints what is being copied

The most commonly used form also includes the --delete switch, which carries an important pitfall:

# A copy that exactly mirrors the original
# (deletes files at the destination that no longer exist in the source!)
rsync -av --delete ~/ /media/user/external-drive/backup/

IMPORTANT: Pay attention to the trailing slash (/) on the source path! ~/ means the contents of the folder, while ~ means the folder itself. A wrong slash can cause nested folders or unwanted deletions.

Useful additional switches:

Switch Meaning When to use
--delete Deletes at the destination what is missing in the source Mirror copies
--dry-run Simulation only — nothing is changed Always before the first run!
-z Compresses data during transfer Copies over the network
--exclude Excludes folders or files --exclude="cache/*"
--progress Shows progress Larger copies

rsync Over the Network (SSH)

If you have another computer or a server at home, you can copy over SSH — the data is encrypted during transfer:

# Copy the home folder to a remote server
rsync -avz ~/backup/ user@192.168.1.20:/mnt/backup/

# And back — restore from the server
rsync -avz user@192.168.1.20:/mnt/backup/ ~/backup/

To connect without entering a password every time, set up SSH keys (see the SSH article).

tar — An Archive in One Place

Where rsync works with a live copy of folders, tar creates a single standalone archive file. It is suitable for storing on another disk or uploading to the cloud:

# Create a compressed archive of the home folder with a date
tar -czvf backup-$(date +%Y-%m-%d).tar.gz ~/

# Explanation of the switches:
# -c (create) creates an archive
# -z (gzip) compresses with gzip
# -v (verbose) prints the files
# -f (file) output file name

Restoring from an archive:

# Unpack the archive into the current folder
tar -xzvf backup-2026-09-03.tar.gz

# Unpack into a specific folder
tar -xzvf backup-2026-09-03.tar.gz -C ~/restored/

Tip: Add a date to the archive names (as above with $(date +%Y-%m-%d)), so you keep a history of copies and know when each one was made.

cron — Automatic Backups

The best backup is the one that makes itself. With cron, you can schedule automatic execution at night:

# Open your user's schedule
crontab -e

# Add a line: every day at 23:00 run rsync
0 23 * * * rsync -a --delete ~/documents/ /media/user/external-drive/backup/

Explanation of the cron fields (five columns before the command):

Field Value in the example Meaning
Minute 0 Exactly on the hour
Hour 23 At 23:00
Day of month * Every day
Month * Every month
Day of week * Every day of the week

Checking whether the task ran is easiest with a log:

# Cron log
grep CRON /var/log/syslog

# Or send the command's output to your own log file
0 23 * * * rsync -a ~/documents/ /media/backup/ >> ~/backup.log 2>&1

Encrypted Backup

A backup on an external disk or in the cloud should be encrypted — a stolen disk means the data is simply open. Linux offers cryptsetup and LUKS for encrypting an entire partition, and an easier path is gpg:

# First create an archive, then encrypt it with a password
tar -czf - ~/documents | gpg -c -o backup.tar.gz.gpg

# Restore: decrypt and unpack
gpg -d backup.tar.gz.gpg | tar -xzvf -

An encrypted copy that you take to a friend's place or a safe satisfies the third part of the 3-2-1 rule — off-site, yet safe from strangers.

Which Tool When?

Tool When to use Drawback
rsync Regular copies to an external disk or server Mirror copy without history (without --backup)
tar Occasional archives you move elsewhere Always copies everything again
tar + gpg Copies that leave your home Slower, you must store the password safely
cron + rsync Automatic daily backups Requires the target disk to be plugged in

Common Mistakes

  1. Never testing the restore: A backup you have never restored may not work. Test the restore before you need it
  2. --delete without --dry-run: One of the fastest ways to lose data. Always simulate first
  3. Backup on the same disk: When the disk fails, the original and the backup fail together
  4. Unencrypted external disk: A lost USB disk with tax documents is a data leak
  5. Trusting only the cloud: The cloud depends on the provider and an internet connection — keep a local copy of at least your most important data

Conclusion

A backup is not a question of if, but of when the disk fails or when you accidentally delete the wrong folder. With rsync, tar, and cron, every Linux user has a complete backup toolbox at hand — free and independent of external services. Start today: one line in crontab and an external disk can be the difference between disaster and relief.

back Back to blog