Linux: take backups using rsnapshot the easy way, plus a restore example!
Backups is something you usually dont think a lot but is essential in case of a failure or a server crash; there are many solutions to deal…
Backups is something you usually dont think a lot but is essential in case of a failure or a server crash; there are many solutions to deal with backups but i tend to like plain simple solutions that work well in small dev environments like the one i use at work, the solution of my choise is rsnapshot which is essentially a perl wrapper for rsync, it needs almost zero configuration and its very simple to start backing up files!
How to install rsnapshot
On deb systems you can install rsnapshot using$ sudo apt-get install rsnapshot
and for rpm systems$ sudo yum install rsnapshot
How to configure rsnapshot
In my case we store all the backups in an nfs mount point under directory /backups, this is an example /etc/fstab entry for the mount point:<nfs_server>:/volume1/backups /backups nfs defaults 0 0
You can mount without a rebooting using$ sudo mount -a
Now for each server you want to add to backup you have to ensure the following two things
- The server has the fstab entry
- The server can communicate with the nfs
Then you can edit the /etc/rsnapshot.conf file and do the following changes to match your configuration, i will not explain all configuration options only what i used to move on.config_version 1.2snapshot_root /backups/<hostname>/cmd_rm /usr/bin/rm
cmd_rsync /usr/bin/rsync
cmd_logger /usr/bin/loggerretain daily 14
verbose 2
loglevel 3
lockfile /var/run/rsnapshot.pidbackup /home/ <hostname>/
backup /etc/ <hostname>/
backup /bin/ <hostname>/
backup /usr/ <hostname>/
backup /root/ <hostname>/
backup /opt/ <hostname>/
backup /var/ <hostname>/
Caveat 1: the spaces are not spaces! instead those are tabs, if you dont use tabs rsnapshot will exit with an error, you can always verify your configuration using:# rsnapshot configcheck
SYNTAX OK
- <hostname> its not really needed but i replaced <hostname> with my server actual hostname, all backups for server <hostname> will be saved under /backups/<hostname>/
- cmd_ : these are just paths for those commands, usually works without any changes, do not touch unless something is not working
- retain <backup_job_name> <days> : retain is a keyword for a backup job name, i have named the backup job daily since i plan to run this everyday and i have asked rsnapshot to keep the backups of last 14 runs.
- verbose, loglevel,lockfile: do not touch unless something does not work
- backup <source_dir <dest_dir>: each backup line instructs rsnapshot to take a backup and store it under snapshot_root/<dest_dir>/<backup_job_name.n> where n is the number of run.
How to run rsnapshot
You can run rsnapshot two-ways:
- manually: as root# rsnapshot <backup_job_name>
Example# rsnapshot daily
- as a scheduled task using cron0 3 * * * /bin/rsnapshot <backup_job_name>
Example0 3 * * * /bin/rsnapshot daily
How do i restore a backup?
A backup is useful only if you can restore from it! Note that many organizations do backups, but a few do restore exercises, fortunately restoring is super easy since we use rsync.
Go to the backup directory you wish to restore and enter, note that you need to do this for each destination directory you wish to restore which can be a pain in the ass, unless you create a custom script solution.# rsync -avr * <dest_dir>
I hope you found this article easy to follow and do some backups as soon as possible :)