For many computer users, the most stressful part of working with a Linux system is having to back up their data. The good news is that there is a simple solution to this problem: set up an automatic rsync backup script that will automatically keep your data safe. In this article, we will go over the tools and steps that you need to take to set up an automated backup system on a Linux system with rsync. You will learn how to use rsync to automatically create backups of files, how to keep these backups up-to-date, and how to restore them in the event of data loss or corruption.
If you regularly perform backups on your Linux system, chances are you already know about rsync, a command-line utility that can be used to back up and synchronize files and directories. However, if you’re new to rsync, it might come as a surprise that this simple command is capable of backing up your entire Linux system. In this guide, we’ll show you how to use rsync to back up your Linux system using different strategies.
Steps to Backup Your Linux System
- Prepare a Backup Device
To make a full backup of the system, you need a device that has much space to keep all files. A backup device can be a locally attached drive, network device, or cloud storage like Amazon S3, Azure Spaces, etc.
Create a directory to store the backup on the backup device. Assuming you have attached a separate disk in your local machine mounted at /mnt directory.
mkdir /mmnt/full-backup
- Install Rsync Utility
Rsync is a command line utility that helps to synchronize content between two directories. They either exist on the local system or one can be the remote. You can quickly install on using the default package manager on most modern Linux distributions. To install Rsync on Debian-based systems, type:
sudo apt install rsync
- Backup Your System
You can run the command directly to make a backup of the complete system. For example, to create a backup of the system to the “/mnt/full-backup” directory, run the following command.
sudo rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/full-backup
The above command will backup the entire root (/) directory, excluding /dev, /proc, /sys, /tmp, /run, /mnt, /media, and /lost+found directories, and save the data in /mnt/full-backup folder. Here:
The
`-aAXv`
options are used so that the files are transferred in “archive” mode, which ensures that symbolic links, devices, permissions, ownerships, modification times, ACLs, and extended attributes are preserved. - Automate the Backup
It’s good practice to schedule automatic backups. You can simply add the above command in crontab, or write them in a shell script and then schedule the script.
12345678910111213#!/usr/bin/evn bashBACKUP_PATH="/mnt/full-backup"EXCLUDE_DIR='{"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'SOURCE_DIR="/"sudo rsync -aAXv ${SOURCE_DIR} --exclude=${EXCLUDE_DIR} ${BACKUP_PATH}if [ $? -eq 0 ]; thenecho "Backup completed successfully"elseecho "Some error occurred during backup"fiYou can schedule the script to run automatically using a tool such as cron. This will allow you to create regular backups of your system without having to manually run the script.
To schedule the script, edit the crontab:
crontab -e
Append the following entry. Make sure to set the correct script name and path. The below schedule will run the script at 02 AM every day.
10 2 * * * bash backup.sh >> backup.logSave and close the editor.
Conclusion
Now that you know how to use rsync, you may want to take advantage of its advanced features. For instance, you can use rsync to efficiently copy files from one directory to another. You can also generate incremental backups that allow you to quickly recover files at any time. If you want even more control over your backup process, you can even schedule backups.