Rsnapshot: Difference between revisions
No edit summary |
(→Automation: add a script to write the contents of each file) |
||
Line 50: | Line 50: | ||
== Automation == | == Automation == | ||
After setting up and testing <samp>rsnapshot</samp> as described above, the next step is to make [[cron|<samp>cron</samp>]] automatically run <samp>rsnapshot</samp> at fixed intervals. The easiest way to achieve this is to create a few scripts in the folders <samp>/etc/periodic/*</samp> that <samp>crond</samp> monitors: | After setting up and testing <samp>rsnapshot</samp> as described above, the next step is to make [[cron|<samp>cron</samp>]] automatically run <samp>rsnapshot</samp> at fixed intervals. The easiest way to achieve this is to create a few scripts in the folders <samp>/etc/periodic/*</samp> that <samp>crond</samp> monitors. The script below will write the appropriate contents to those files and make them executable: | ||
<pre> | |||
#!/bin/sh | |||
cd /etc/periodic | |||
dirs=("daily" "weekly" "monthly") | |||
for dir in "${dirs[@]}"; do | |||
echo -e "#!/bin/sh\nexec /usr/bin/rsnapshot $dir" > "$dir/rsnapshot" | |||
chmod +x "$dir/rsnapshot" | |||
done | |||
</pre> | |||
Revision as of 05:49, 26 March 2023
rsnapshot is a filesystem backup utility based on rsync. Using rsnapshot, it is possible to take snapshots of your filesystems at different points in time. Using hard links, rsnapshot creates the illusion of multiple full backups, while only taking up the space of one full backup plus differences. When coupled with ssh, it is possible to take snapshots of remote filesystems as well. This document is a tutorial on the installation and configuration of rsnapshot.
Installation
To install rsnapshot:
apk add rsnapshot
Configuration
To configure rsnapshot, copy the example configuration /etc/rsnapshot.conf.default to /etc/rsnapshot.conf, and edit it to your needs based on the comments and the official documentation. Note that rsnapshot requires tabs between options and values in rsnapshot.conf. This is done so spaces can be included in filenames without requiring any extra escaping or quoting.
The most important parts to modify are where to store the backups:
snapshot_root /mnt/backup
How many backups to retain:
retain daily 7 retain weekly 4 retain monthly 12
And what to backup:
# Local backup /home/ local/ backup /etc/ local/ # Remote backup user@remote:/home/user/ remote/ exclude=/home/user/Downloads
In this case, every 7th daily backup is saved as a weekly backup, every 4th weekly backup is retained as a monthly backup, and every 12th monthly backup is deleted. The folders /home and /etc from the local machine are backed up to /mnt/backup/local/, while it uses ssh to back up the folder /home/user on the machine remote to /mnt/backup/remote/. Make sure root has passwordless ssh access to the machines you want to backup over the internet (i.e. run ssh-keygen and ssh-copy-id as root).
The last line also shows an example of how you can exclude parts of the location from backups.
Testing
To test that your config file has the correct syntax:
rsnapshot configtest
To check what the system would do when running a backup without executing the commands, i.e. a dry run:
rsnapshot -t daily rsnapshot -t weekly rsnapshot -t monthly
Finally, perform the first backup:
rsnapshot daily
The last part might take a while. Subsequent backups should be much faster, as it will then only have to copy files that have changed since the last backup.
Automation
After setting up and testing rsnapshot as described above, the next step is to make cron automatically run rsnapshot at fixed intervals. The easiest way to achieve this is to create a few scripts in the folders /etc/periodic/* that crond monitors. The script below will write the appropriate contents to those files and make them executable:
#!/bin/sh cd /etc/periodic dirs=("daily" "weekly" "monthly") for dir in "${dirs[@]}"; do echo -e "#!/bin/sh\nexec /usr/bin/rsnapshot $dir" > "$dir/rsnapshot" chmod +x "$dir/rsnapshot" done
Contents of /etc/periodic/daily/rsnapshot
Contents of /etc/periodic/weekly/rsnapshot
Contents of /etc/periodic/monthly/rsnapshot
Remember to make the scripts executable:
chmod +x /etc/periodic/*/rsnapshot
After that, test that the scripts work as expected:
run-parts /etc/periodic/daily run-parts /etc/periodic/weekly run-parts /etc/periodic/monthly
Assuming crond is set to start at boot (the default), your system should now make backups automatically.