Support #85
Updated by Daniel Curtis almost 12 years ago
This howto will show you how to install and set up Rsnapshot, enable archiving of snapshots and how to back up MySQL databases on Debian.
h2. 1. Install rsnapshop
The following guide will be ran as @root@:
<pre>
sudo su
apt-get install rsnapshot
</pre>
h2. 2. Edit config file:
<pre>
nano /etc/rsnapshot.conf
</pre>
* Set snapshot_root to path where you want to keep the backups
<pre>
snapshot_root /var/cache/rsnapshot/
</pre>
* Set up the list of directories/files to back up
<pre>
backup /etc/ localhost/
</pre>
h3. Backup Intervals
This section of the configuration file is used only to define labels for intervals and how many snapshots of each level to keep. How often the snapshots are made is configured and run via cron. The keyword interval is followed by an alphanumeric label, followed by a number, signifying how many intervals (snapshots) to keep. The interval labels must be unique and in ascending order, smallest interval must be listed first.
In following example, after 6 "@hourly@" snapshots the oldest "@hourly@" is deleted. The top entry (in this case "@hourly@") is copied from the source, while the remaining entries simply link to the latest snapshot from one level above.
In other words, to keep six backups a day (four-hour interval), seven daily backups (one week), and four weekly backups (one month), specify:
<pre>
interval hourly 6
interval daily 7
interval weekly 4
</pre>
The interval labels "@hourly@", "@daily@", "@weekly@" can be changed to suit your needs, for example "@daysago@", "@weeksago@" and "@monthsago@".
Each time rsnapshot hourly is executed, by hand or by cron, it will create a new snapshot, rotate the old ones, and retain the 6 most recent (@hourly.0@ - @hourly.5@).
h3. Backup Points
Example of a backup point inside configuration file:
<pre>
backup /etc/ localhost/
</pre>
Backup points begin with the word backup, following /etc/ is the full path of the directory that will be backed up and localhost is a directory inside snapshot_root. You can change localhost to anything associating to the server like server's fully qualified domain name.
In addition to full paths on the local filesystem, you can also backup remote systems using rsync over ssh. If you have ssh installed and enabled (via the cmd_ssh parameter), you can specify a path like:
<pre>
backup root@example.com:/etc/ example.com/
</pre>
Have in mind that you must have key-based logins enabled for the root user at example.com, without passphrases in order this to function properly.
h3. Backup Scripts
You can find many examples in the utils directory which on Debian is located at @/usr/share/doc/rsnapshot/examples/utils/@.
Backup scripts are executed with each lowest interval.
h3. Executing Backups Via Cron
The cron file is located at
* @/etc/cron.d/rsnapshot@
The default contents:
<pre>
# 0 */4 * * * root /usr/bin/rsnapshot hourly
# 30 3 * * * root /usr/bin/rsnapshot daily
# 0 3 * * 1 root /usr/bin/rsnapshot weekly
# 30 2 1 * * root /usr/bin/rsnapshot monthly
</pre>
You have to uncomment lines to activate the backups.
The first tells cron to execute @cron-apt@ every 4 hours which matches the hourly interval settings in the rsnapshot.conf.
h2. Test configuration
+Every time you make a change in the config file+ do a @configtest@
<pre>
rsnapshot configtest
</pre>
PLEASE BE AWARE OF THE FOLLOWING RULES:
* Configuration file requires tabs between elements, spaces represent argument for scripts
* Directories require a trailing slash, example:
> right: /home/
>
> wrong: /home
h2. Archiving snapshots
<pre>
cp /usr/share/doc/rsnapshot/examples/utils/rsnaptar /usr/local/bin/
</pre>
Make sure your backup scripts are owned by @root@, and +*not* writable by anyone else+.
<pre>
chown root.root /usr/local/bin/rsnaptar
chmod o-w /usr/local/bin/rsnaptar
</pre>
h3. Edit script and set directory paths
<pre>
nano /usr/local/bin/rsnaptar
</pre>
Set @TAR_DIR@ to path where snapshot will be archived, and @SNAPSHOT_DIR@ to where the daily snapshot is located:
<pre>
TAR_DIR="/home/user/dvd_backup"
SNAPSHOT_DIR="/var/cache/rsnapshot/daily.0"
</pre>
Please note that the hourly cycle needs to complete in order for the daily snapshot to be created.
This script supports gpg encrupting of files, to disable it comment out following fline in @/usr/local/bin/rsnaptar@:
<pre>
GPG="/usr/bin/gpg"
</pre>
h2. Scheduling Execution Via Cron
<pre>
nano /etc/cron.daily/rsnaptar
</pre>
Add:
<pre>
#!/usr/bin/env bash
/usr/local/bin/rsnaptar root@example.com
</pre>
h3. Make /etc/cron.daily/rsnaptar executable
<pre>
chmod +x /etc/cron.daily/rsnaptar
</pre>
Notes:
- This howto does not use the GPG option to encrypt files, if this is a necessity than I can update the howto to include file encryption for better security,
- By default script is run standalone, but it could be modified to run as a backup_script via rsnapshot itself,
h2. Rsnapshot: Backing Up MySQL Databases
Install @mysql-client@, if not installed, this will provide the @mysqldump@ utility:
<pre>
apt-get install mysql-client
cp /usr/share/doc/rsnapshot/examples/utils/backup_mysql.sh /usr/local/bin/
</pre>
Make sure your backup scripts are owned by @root@, and not writable by anyone else.
<pre>
chown root.root /usr/local/bin/backup_mysql.sh
chmod o-w /usr/local/bin/backup_mysql.sh
</pre>
This script is designed only to back up all databases to a single file.
Edit script:
<pre>
nano /usr/local/bin/backup_mysql.sh
</pre>
Replace
> /usr/bin/mysqldump --all-databases > mysqldump_all_databases.sql
with
> /usr/bin/mysqldump --defaults-file=/etc/mysql/debian.cnf --all-databases > mysqldump_all_databases.sql
h3. Create debian-sys-maint user
* Log into MySQL as root MySQL user
<pre>
mysql -u root -p
</pre>
Enter password
* Create debian-sys-maint user
<pre>
GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY 'SuperSecretPassword';
</pre>
+Relacing SuperSecretPassword with an appropriate password.+
* Match the password in @/etc/mysql/debian.cnf@
<pre>
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = SuperSecretPassword
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
user = debian-sys-maint
password = SuperSecretPassword
socket = /var/run/mysqld/mysqld.sock
basedir = /usr
</pre>
+Replacing SuperSecretPassword with the password used on the MySQL server+
If you need to dump a single database use line below:
> /usr/bin/mysqldump --defaults-file=/etc/mysql/debian.cnf DATABASE > DATABASE.SQL
h2. Add Script To Rsnapshot
Edit rsnapshot config:
<pre>
nano /etc/rsnapshot.conf
</pre>
Under *BACKUP POINTS / SCRIPTS* add the following:
<pre>
backup_script /usr/local/bin/backup_mysql.sh localhost/mysqldump
</pre>
h2. Test config:
<pre>
rsnapshot configtest
</pre>
h2. Resources:
http://www.howtoforge.com/set-up-rsnapshot-archiving-of-snapshots-and-backup-of-mysql-databases-on-debian