How to Backup and Restore Files with Rsnapshot on Ubuntu
Rsnapshot makes backing up and restoring your files on Ubuntu simple and efficient.
This tool creates backups by only saving the files that have changed, which saves a lot of disk space. Rsnapshot uses two other tools, rsync and hard links, to make these smart backups. You can use it to back up files on your own computer or on another computer somewhere else.
You set up Rsnapshot by changing a file called `/etc/rsnapshot.conf`. This main setup file tells Rsnapshot exactly what to back up, when to do it, and where to keep the backup copies. This makes it easy to have your backups happen automatically.
Having regular backups protects your important files if something goes wrong, like accidentally deleting a file or a hard drive failing. Rsnapshot helps make backing up and getting your files back much easier, so you can recover your data quickly when you need it.
Install rsnapshot using ‘sudo apt install rsnapshot’. Configure backups by editing /etc/rsnapshot.conf, specifying snapshot locations, retention intervals, and directories to back up. Schedule backups using cron jobs in /etc/cron.d/rsnapshot.
Install rsnapshot
Rsnapshot is included in the default Ubuntu repository, so installation is quite manageable. To install it, run the command below.
After installation, running the command `rsnapshot –version` will display Rsnapshot’s binary location and version on Ubuntu. This command verifies that the software installed correctly and is ready for use, showing you have version 1.4.4 or later.
which rsnapshot
rsnapshot --version
Configure SSH authentication for remote system
Before you configure rsnapshot, it’s important to set up SSH authentication so your Ubuntu system can connect to another computer without needing a password. You’ll create a special SSH key pair on your computer and then copy the public key to the remote computer. This makes your remote rsnapshot backup secure and convenient.
ssh-keygen -t ed25519 command below to generate your SSH keypair. Then, use ssh-copy-id to copy the public key to the remote server.ssh-keygen -t ed25519
ssh-copy-id username@remote_server_name
Replace the username and remote_server_name with your actual username and system names.
For more on setting up SSH authentication, check out the post below.
Configure rsnapshot
After setting up SSH, you need to configure rsnapshot on your Ubuntu system. This tells rsnapshot exactly what files to back up and where to store them. Before making changes, it’s wise to copy your current settings using `sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.orig`, then open the file with `sudo nano /etc/rsnapshot.conf` to set your backup locations.
sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.orig
Then, open the file and make your adjustments.
sudo nano /etc/rsnapshot.conf
Choose where to store your snapshots.
# All snapshots will be stored under this root directory.
#
snapshot_root /var/cache/rsnapshot/
Make sure these lines aren’t commented out; they enable remote backups.
# rsync must be enabled for anything to work. This is the only command that
# must be enabled.
#
cmd_rsync /usr/bin/rsync
# Uncomment this to enable remote ssh backups over rsync.
#
cmd_ssh /usr/bin/ssh
You can also change how often backups occur.
#########################################
# BACKUP LEVELS / INTERVALS #
# Must be unique and in ascending order #
# e.g. alpha, beta, gamma, etc. #
#########################################
retain daily 6
retain weekly 7
retain monthly 4
#retain delta 3
SSH normally uses port 22, but you can specify a custom port here.
# ssh has no args passed by default, but you can specify some here.
#
ssh_args -p 22
The backup section is where you’ll tell Rsnapshot what to back up and where. The highlighted lines demonstrate how to back up a remote server and specify a local path.
Remember to replace username and remote_server_name with your actual system details.
###############################
### BACKUP POINTS / SCRIPTS ###
###############################
# LOCALHOST
backup /home/ localhost/
backup /etc/ localhost/
backup /usr/local/ localhost/
backup username@remote_server_name:/etc/ local_server_name
backup username@remote_server_name:/www/data/ local_server_name
#backup /var/log/rsnapshot localhost/
#backup /etc/passwd localhost/
#backup /home/foo/My Documents/ localhost/
#backup /foo/bar/ localhost/ one_fs=1,rsync_short_args=-urltvpog
#backup_script /usr/local/bin/backup_pgsql.sh localhost/postgres/
# You must set linux_lvm_* parameters below before using lvm snapshots
#backup lvm://vg0/xen-home/ lvm-vg0/xen-home/
Once you’re done, save your changes and exit.
Automatic backup with Cron
Making your rsnapshot backup on Ubuntu automatic is done using Cron to schedule when backups run. You edit the cron job file with `sudo nano /etc/cron.d/rsnapshot` to set up the backup schedule. This ensures your data is backed up regularly based on the settings you defined in your `rsnapshot.conf` file, without manual intervention.
sudo nano /etc/cron.d/rsnapshot
Set up the backup settings you defined in the `rsnapshot.conf` file.
# This is a sample cron file for rsnapshot.
# The values used correspond to the examples in /etc/rsnapshot.conf.
# There you can also set the backup points and many other things.
#
# To activate this cron file you have to uncomment the lines below.
# Feel free to adapt it to your needs.
0 0 * * * root /usr/bin/rsnapshot daily
0 0 * * 0 root /usr/bin/rsnapshot weekly
0 0 1 * * root /usr/bin/rsnapshot monthly
Save the file and exit.
For help setting up a cron task, check out the post below.
How to run cron jobs on Ubuntu
To restore your data, copy files from the snapshots location you defined in the Rsnapshot file.
That should do it!
Conclusion:
Backing up and restoring files with Rsnapshot on Ubuntu is an efficient and effective way to safeguard your data. Here are the key takeaways:
- Automated Backups: Rsnapshot allows for easy scheduling of automated backups, ensuring your data is regularly protected without manual intervention.
- Incremental Backups: By utilizing hard links, rsnapshot can perform incremental backups, saving both time and storage space.
- Remote Backups: The tool can back up files to and from remote systems, enhancing flexibility and security for critical data.
- Simple Configuration: With its straightforward configuration file, users can easily manage backup settings, including frequency and directories to back up.
- Ease of Restoration: Restoring data from snapshots is a simple process, making recovery quick and efficient in case of data loss.
Incorporating Rsnapshot into your data management routine significantly enhances data safety and reliability.
Does timeshift work with Ubuntu?
A failed kernel update or broken package transaction is much easier to undo when you already have system snapshots. You can install Timeshift on Ubuntu from the default repositories, then roll system files back with RSYNC or BTRFS snapshots without hunting for a separate download.
What is rsnapshot?
Rsnapshot is an rsync-based filesystem snapshot utility. It can take incremental backups of local and remote filesystems for any number of machines. Rsnapshot makes extensive use of hard links, so disk space is only used when absolutely necessary.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!