Set Up Rsnapshot, Archiving Of Snapshots And Backup Of MySQL Databases On Debian

By Nedim Hadzimahmutovic <>

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.

Install the program using the apt-get utility:

apt-get install rsnapshot

Edit config file:

nano /etc/rsnapshot.conf

Set snapshot_root to path where you want to keep the backups:

snapshot_root   /var/cache/rsnapshot/

Set up the list of directories/files to back up.

backup  /etc/           localhost/


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:

interval        hourly  6
interval        daily   7
interval        weekly  4

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).


Backup Points

Example of a backup point inside configuration file:

backup      /etc/      localhost/ 

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:


Have in mind that you must have key-based logins enabled for the root user at, without passphrases in order this to function properly./


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.


Executing Backups Via Cron

The cron file is located at


The default contents:

# 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

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.


Test configuration:

Every time you make a change in the config file do a configtest:

rsnapshot configtest


  •  Configuration file requires tabs between elements, spaces represent argument for scripts
  • Directories require a trailing slash, example:

right: /home/

wrong: /home


Archiving snapshots

cp /usr/share/doc/rsnapshot/examples/utils/rsnaptar /usr/local/bin/

Make sure your backup scripts are owned by root, and not writable by anyone else.

chown root.root /usr/local/bin/rsnaptar
chmod o-w /usr/local/bin/rsnaptar

Edit script and set directory paths:

nano /usr/local/bin/rsnaptar

Set TAR_DIR to path where snapshot will be archived, and SNAPSHOT_DIR to where the daily snapshot is located:


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:



Scheduling Execution Via Cron

nano /etc/cron.daily/rsnaptar


#!/usr/bin/env bash


  • - 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,


Rsnapshot: Backing Up MySQL Databases

Install mysql-client if not installed, this will provide the mysqldump utility:

# apt-get install mysql-client

# cp /usr/share/doc/rsnapshot/examples/utils/ /usr/local/bin/

Make sure your backup scripts are owned by root, and not writable by anyone else.

chown root.root /usr/local/bin/
chmod o-w /usr/local/bin/

This script is designed only to back up all databases to a single file.

Edit script:

nano /usr/local/bin/


/usr/bin/mysqldump --all-databases > mysqldump_all_databases.sql


/usr/bin/mysqldump --defaults-file=/etc/mysql/debian.cnf --all-databases > mysqldump_all_databases.sql

If you need to dump a single database use line below:

/usr/bin/mysqldump --defaults-file=/etc/mysql/debian.cnf DATABASE > DATABASE.SQL


Add Script To Rsnapshot

Edit rsnapshot config:

nano /etc/rsnapshot.conf

Under BACKUP POINTS / SCRIPTS add the following:

backup_script		/usr/local/bin/	localhost/mysqldump/

Test config:

rsnapshot configtest

Share this page:

Suggested articles

4 Comment(s)

Add comment


From: Shane

Thanks for the awesome tutorial! I seem to get issues with hard links because my server is setup wtih enfcs ... have you experience that before? I cannot for the life of me figure out how to resolve that?

From: marcnz

the mysql dump rsnapshot entry is wrong. When entered as is, the configtest run fails.

1. There should be no spaces, but only tabs between the segments

2. The last "/" is missing at the end of mysqldump.

There is nothing more annoying than going through short tutorials like this and it is not workign because of the author can't write correct statements! Unless that author does it on purpose to force users to think and find solutions on their own.

Please correct it.

From: till

Thank you for the notice. The tutorial has been updated.

From: Poliman

In my backup, after execute command rsnapshot hourly, I get error below and file "mysqldump_all_databases.sql" is empty:

mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect

I use /root/.my.cnf file:

 [mysqld] user     = root password = my_pass host     = localhost

When I change [mysqld] to [client] I haven't error but .sql file is still empty.