How to Backup and Restore Files with Rsnapshot on Ubuntu
Rsnapshot is a free backup tool for Ubuntu that saves space by storing only the files you change. It uses rsync and hard links to create smart archives on your local computer or a remote server.
You set up the whole system by editing the `/etc/rsnapshot.conf` file to pick your folders and schedules. Regular backups keep your important documents safe from hard drive crashes and accidental deletions.
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
Because the tool lives in the default Ubuntu repositories, getting it onto your machine takes only a moment. Run the command below to install the package.
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 configuring rsnapshot, establish SSH authentication so the Ubuntu host communicates with target machines without prompting for passwords. Generating an SSH key pair (cryptographic credentials for secure logins) lets your local machine talk to remote servers without requiring a password every time, sending the public half over to the destination host. This step secures remote communication channels.
Use the 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, configure rsnapshot on your Ubuntu system. This file dictates target paths and destination directories. Before making changes, copy existing settings using `sudo cp /etc/rsnapshot.conf /etc/rsnapshot.conf.orig`, then open the file with `sudo nano /etc/rsnapshot.conf` to set backup locations.
Before making changes, back up the original configuration file.
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/
# 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.
###############################
### 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
Automating rsnapshot execution on Ubuntu relies on Cron for scheduling routines. Edit the cron job file with `sudo nano /etc/cron.d/rsnapshot` to set up the backup schedule. This ensures data archiving happens regularly based on rules defined within `rsnapshot.conf`, bypassing manual intervention entirely.
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 completes the setup process.
Conclusion:
Backing up and restoring files with Rsnapshot on Ubuntu provides an efficient method for safeguarding critical data. Review these core operational points:
- 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 file safety and system reliability.
Does timeshift work with Ubuntu?
Recovering from a failed kernel update or broken package transaction goes much faster when system snapshots exist. Installing Timeshift on Ubuntu from default repositories allows rolling back system files via RSYNC or BTRFS archives without hunting for outside downloads.
What is rsnapshot?
Rsnapshot functions as an rsync-driven filesystem snapshot utility. It captures incremental copies of local and remote targets across multiple machines. Extensive use of hard links ensures storage consumption scales only when data changes.
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!