Rsnapshot: Difference between revisions

From Alpine Linux
(use https)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[http://rsnapshot.org/ <samp>rsnapshot</samp>] is a filesystem backup utility based on [[rsync|<samp>rsync</samp>]]. Using <samp>rsnapshot</samp>, 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 <samp>ssh</samp>, it is possible to take snapshots of remote filesystems as well. This document is a tutorial in the installation and configuration of rsnapshot.
[https://rsnapshot.org/ <samp>rsnapshot</samp>] is a filesystem backup utility based on [[rsync|<samp>rsync</samp>]]. Using <samp>rsnapshot</samp>, 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 <samp>ssh</samp>, it is possible to take snapshots of remote filesystems as well. This document is a tutorial on the installation and configuration of rsnapshot.


== Installation ==
== Installation ==
Line 8: Line 8:


== Configuration ==
== Configuration ==
To configure <samp>rsnapshot</samp>, copy the example configuration <samp>/etc/rsnapshot.conf.default</samp> to <samp>/etc/rsnapshot.conf</samp>, and edit it to your needs based on the comments and the [http://rsnapshot.org/rsnapshot/docs/docbook/rest.html official documentation]. Note that <samp>rsnapshot</samp> requires tabs between options and values in <samp>rsnapshot.conf</samp>; this is done such that spaces can be included in filenames without requiring any extra escaping or quoting.
To configure <samp>rsnapshot</samp>, copy the example configuration <samp>/etc/rsnapshot.conf.default</samp> to <samp>/etc/rsnapshot.conf</samp>, and edit it to your needs based on the comments and the [https://rsnapshot.org/rsnapshot/docs/docbook/rest.html official documentation]. Note that <samp>rsnapshot</samp> requires tabs between options and values in <samp>rsnapshot.conf</samp>. 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:
The most important parts to modify are where to store the backups:
Line 15: Line 15:
</pre>
</pre>
How many backups to retain:
How many backups to retain:
These settings used to be called "hourly, daily, weekly, monthly" in older releases of rsnapshot, but were changed to "alpha, beta, gamma, delta" because the time interval between backups is arbitrary.
Setting this means I end up with 6 backups which were done most recently, then one of those gets hived off to become the beta backup, which it keeps 7 of, one of which gets hived off as gamma, and so on. This means you have multiple alpha backups fairly recently, multiple older beta backups representing days then gamma representing weeks and delta backups going back months. Set this according to how many backups you want to have.
<pre>
<pre>
retain daily 7
retain alpha 6
retain weekly 4
retain beta 7
retain monthly 12
retain gamma 4
retain delta 3
</pre>
</pre>
And what to backup:
And what to backup:
Line 29: Line 34:
backup user@remote:/home/user/ remote/ exclude=/home/user/Downloads
backup user@remote:/home/user/ remote/ exclude=/home/user/Downloads
</pre>
</pre>
In this case, every 7th <samp>daily</samp> backup is saved as a <samp>weekly</samp> backup, every 4th <samp>weekly</samp> backup is retained as a <samp>monthly</samp> backup, and every 12th <samp>monthly</samp> backup is deleted. The folders <samp>/home</samp> and <samp>/etc</samp> from the local machine are backed up to <samp>/mnt/backup/local/</samp>, while it uses <samp>ssh</samp> to back up the folder <samp>/home/user</samp> on the machine <samp>remote</samp> to <samp>/mnt/backup/remote/</samp>. Make sure that <samp>root</samp> has password-less <samp>ssh</samp>-access to the machines you want to backup over the internet (i.e. run <samp>ssh-keygen</samp> and <samp>ssh-copy-id</samp> as <samp>root</samp>). The last line also shows an example of how you can exclude parts of the location from backups.
In this case, every 7th <samp>daily</samp> backup is saved as a <samp>weekly</samp> backup, every 4th <samp>weekly</samp> backup is retained as a <samp>monthly</samp> backup, and every 12th <samp>monthly</samp> backup is deleted. The folders <samp>/home</samp> and <samp>/etc</samp> from the local machine are backed up to <samp>/mnt/backup/local/</samp>, while it uses <samp>ssh</samp> to back up the folder <samp>/home/user</samp> on the machine <samp>remote</samp> to <samp>/mnt/backup/remote/</samp>. Make sure <samp>root</samp> has passwordless <samp>ssh</samp> access to the machines you want to backup over the internet (i.e. run <samp>ssh-keygen</samp> and <samp>ssh-copy-id</samp> as <samp>root</samp>).<BR>
The last line also shows an example of how you can exclude parts of the location from backups.


== Testing ==
== Testing ==
Line 36: Line 42:
rsnapshot configtest
rsnapshot configtest
</pre>
</pre>
To check what the system would do when running a backup without executing the commands:
To check what the system would do when running a backup without executing the commands, i.e. a dry run:
<pre>
<pre>
rsnapshot -t daily
rsnapshot -t alpha
rsnapshot -t weekly
rsnapshot -t beta
rsnapshot -t monthly
rsnapshot -t gamma
rsnapshot -t delta
</pre>
</pre>
Finally, perform the first backup:
Finally, perform the first backup:
<pre>
<pre>
rsnapshot daily
rsnapshot alpha
</pre>
</pre>
The last part might take a while, so grab a cup of tea and a biscuit. Subsequent backups should be much faster, as it will then only have to copy files that has actually changed since last backup.
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 ==
== 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> for us 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:
 
{{Cat|/etc/periodic/hourly/rsnapshot|#!/bin/sh
 
exec /usr/bin/rsnapshot alpha
}}




{{Cat|/etc/periodic/daily/rsnapshot|#!/bin/sh
{{Cat|/etc/periodic/daily/rsnapshot|#!/bin/sh


exec /usr/bin/rsnapshot daily
exec /usr/bin/rsnapshot beta
}}
}}


Line 60: Line 72:
{{Cat|/etc/periodic/weekly/rsnapshot|#!/bin/sh
{{Cat|/etc/periodic/weekly/rsnapshot|#!/bin/sh


exec /usr/bin/rsnapshot weekly
exec /usr/bin/rsnapshot gamma
}}
}}


Line 66: Line 78:
{{Cat|/etc/periodic/monthly/rsnapshot|#!/bin/sh
{{Cat|/etc/periodic/monthly/rsnapshot|#!/bin/sh


exec /usr/bin/rsnapshot monthly
exec /usr/bin/rsnapshot delta
}}
}}


Line 76: Line 88:
After that, test that the scripts work as expected:
After that, test that the scripts work as expected:
<pre>
<pre>
run-parts /etc/periodic/hourly
run-parts /etc/periodic/daily
run-parts /etc/periodic/daily
run-parts /etc/periodic/weekly
run-parts /etc/periodic/weekly
run-parts /etc/periodic/monthly
run-parts /etc/periodic/monthly
</pre>
</pre>
Assuming <samp>crond</samp> is set to start at boot (the default), your system should now take backups automatically.
Assuming <samp>crond</samp> is set to start at boot (the default), your system should now make backups automatically.
 
[[Category:System Administration]]

Latest revision as of 21:18, 25 August 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:

These settings used to be called "hourly, daily, weekly, monthly" in older releases of rsnapshot, but were changed to "alpha, beta, gamma, delta" because the time interval between backups is arbitrary.

Setting this means I end up with 6 backups which were done most recently, then one of those gets hived off to become the beta backup, which it keeps 7 of, one of which gets hived off as gamma, and so on. This means you have multiple alpha backups fairly recently, multiple older beta backups representing days then gamma representing weeks and delta backups going back months. Set this according to how many backups you want to have.

retain	alpha	6
retain	beta	7
retain	gamma	4
retain	delta	3

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 alpha
rsnapshot -t beta
rsnapshot -t gamma
rsnapshot -t delta

Finally, perform the first backup:

rsnapshot alpha

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:

Contents of /etc/periodic/hourly/rsnapshot

#!/bin/sh exec /usr/bin/rsnapshot alpha


Contents of /etc/periodic/daily/rsnapshot

#!/bin/sh exec /usr/bin/rsnapshot beta


Contents of /etc/periodic/weekly/rsnapshot

#!/bin/sh exec /usr/bin/rsnapshot gamma


Contents of /etc/periodic/monthly/rsnapshot

#!/bin/sh exec /usr/bin/rsnapshot delta


Remember to make the scripts executable:

chmod +x /etc/periodic/*/rsnapshot

After that, test that the scripts work as expected:

run-parts /etc/periodic/hourly
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.