How to Backup and Restore Files with Rsnapshot on Ubuntu
Rsnapshot is a free backup tool for Ubuntu. It keeps snapshots of your files on your local computer or on a remote server. It uses rsync to copy the data and hard links (directory entries that point to the same file on disk) to save space. A file that has not changed between snapshots is stored once, and only new or changed files take up extra room.
You control the whole system through the /etc/rsnapshot.conf file, where you pick your folders and schedules. Regular snapshots protect your important documents from hard drive failures and accidental deletions. This guide covers installing the tool, setting up SSH keys for remote backups, editing the configuration, scheduling with Cron, and restoring files.
Install Rsnapshot on Ubuntu by running sudo apt install rsnapshot in a terminal, then confirm it works with rsnapshot –version. Rsnapshot is in the default Ubuntu repositories, so no extra sources are needed. Snapshots are configured in /etc/rsnapshot.conf, and unchanged files are stored once using hard links, so each new snapshot takes little extra space.
Install rsnapshot
Rsnapshot is in the default Ubuntu software repositories, so you do not need to add any outside package sources.
- Open a terminal.
- Run sudo apt install rsnapshot. Confirm the prompt if apt asks you to.
- Wait for the installation to finish. You should be back at the shell prompt with no error messages.
- Run rsnapshot –version to check that the tool is ready. It prints the version number.
You need an account that can use sudo. If apt says it cannot find the package, check that the “universe” repository is enabled on your system.
After installation, running the command rsnapshot –version shows Rsnapshot’s version. This confirms the software installed correctly and is ready to use. You should see version 1.4.4 or later.
If the shell reports “command not found”, the installation did not complete. Go back to the previous step and look for errors in the apt output.
which rsnapshot
rsnapshot --version
Configure SSH authentication for remote system
If you only back up folders on the same computer, you can skip this section. To back up a remote server, rsnapshot has to connect over SSH without asking for a password. That means key-based logins. You create a keypair with the ssh-keygen command, then copy the public key to the destination host.
Two things matter here:
- Create the key as the user that will run the backup. The Cron job in this guide runs as root, so the key normally has to belong to root, not to your regular account.
- Leave the passphrase empty when ssh-keygen asks for one. A passphrase would bring back the password prompt and stop unattended backups.
- Run the
ssh-keygen -t ed25519command below to generate your SSH keypair. Accept the default file location when prompted. When it finishes, you should see the key fingerprint and a small ASCII image. - Run
ssh-copy-idto copy the public key to the remote server. Enter the remote account’s password one last time when asked. - Try an SSH login to the remote server. It should now connect without a password. This first connection also lets you accept the host’s fingerprint, which would otherwise block an unattended job.
ssh-keygen -t ed25519
ssh-copy-id username@remote_server_name
Replace username and remote_server_name with your actual username and system names. The username must be an account on the remote machine that can read the files you want to back up.
For more on setting up SSH authentication, check out the post below.
Configure rsnapshot
Rsnapshot reads everything from one settings file. It says where your data comes from, where the snapshots go, and how many of each kind to keep. Back up the original file before you change anything, then open it in an editor. The steps below follow the order of the file.
If you type spaces, rsnapshot reports a syntax error or ignores the line. Copy lines from this guide carefully and check that the separators are tabs.
First, back up the original configuration file. Run sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.orig. If you make a mess of the file later, copy the .orig file back over the original to undo everything.
sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.orig
Then open the file with sudo nano /etc/rsnapshot.conf and make your adjustments. You need sudo because the file belongs to root. Nano opens with the file contents on screen, and you can move around with the arrow keys.
sudo nano /etc/rsnapshot.conf
Choose where to store your snapshots. This is the root folder for all your backups. Pick a location with plenty of free space, ideally on a different disk from the data you are protecting. A backup on the same disk will not help if that disk fails.
# All snapshots will be stored under this root directory.
#
snapshot_root /var/cache/rsnapshot/
Make sure these lines aren’t commented out. A line is commented out if it starts with a # character. Delete the # to enable the line. These lines enable remote backups, so rsnapshot can use SSH.
# 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. These lines set how many snapshots of each kind (such as hourly, daily or weekly) rsnapshot keeps. When the limit is reached, the oldest snapshot is rotated out. Keep only what you need, because more snapshots use more disk space over time. Each interval you keep here also needs a matching Cron entry later.
#########################################
# 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. You only need this if the remote server’s SSH service listens on a different port. Otherwise leave the default alone.
# ssh has no args passed by default, but you can specify some here.
#
ssh_args -p 22
The backup section is where you tell Rsnapshot what to back up and where to put it. The highlighted lines show how to back up a remote server and how to specify a local path. Each line has a source and a destination. The destination is a folder name inside your snapshot root. Also end source folder paths with a slash. Without it, the copy may not land where you expect.
Remember to replace username and remote_server_name with your actual system details. Use the same username you used when copying the SSH key.
###############################
### 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.
- Press Ctrl+O, then Enter to write the file.
- Press Ctrl+X to leave nano. You should be back at the shell prompt.
Automatic backup with Cron
Cron is the built-in Linux scheduler. It runs your backups in the background according to the schedule you give it, so you don't have to start them by hand. To create the schedule file, run sudo nano /etc/cron.d/rsnapshot. If the file already exists, nano opens it with its current contents. You can edit the lines in place.
sudo nano /etc/cron.d/rsnapshot
Add a schedule for each backup interval you defined in the rsnapshot.conf file. The names must match the ones in the config file exactly. Space out the times so jobs do not run at the same moment. Run the longer intervals (such as weekly) earlier than the shorter ones (such as daily) so they rotate the older snapshots before the next short one is taken.
# 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. Press Ctrl+O, then Enter, then Ctrl+X. Cron picks up changes to files in /etc/cron.d on its own, so you do not need to restart anything. Check the snapshot folder after the first scheduled run to confirm that a new snapshot appeared.
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. Each snapshot is a normal folder, named after its interval and a number, with the lowest number being the newest. Inside, you will find your backed-up folders laid out as they were on the source. Because snapshots are plain files, you can browse them with a file manager or the terminal and copy out what you need.
To be safe, restore into a temporary folder first and check the files before you overwrite anything in your live data.
That completes the setup process.
Backing up and restoring files with Rsnapshot on Ubuntu provides an efficient method for safeguarding critical data. Review these core operational points:
- Automated BackupsRsnapshot allows for easy scheduling of automated backups, ensuring your data is regularly protected without manual intervention.
- Incremental BackupsBy utilizing hard links, rsnapshot can perform incremental backups, saving both time and storage space.
- Remote BackupsThe tool can back up files to and from remote systems, enhancing flexibility and security for critical data.
- Simple ConfigurationWith its straightforward configuration file, users can easily manage backup settings, including frequency and directories to back up.
- Ease of RestorationRestoring 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 improves file safety and system reliability. Once it is running, check the snapshot folder from time to time and try restoring a test file, so you know your backups work before you need them.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!