Understanding Rsnapshot Architecture
Rsnapshot operates on a simple but powerful concept: it creates incremental backups using hard links. When you run a backup, rsnapshot compares the current state of your data with the previous snapshot. If files have not changed, it creates a hard link to the existing file rather than copying it again. This means that subsequent backups take very little time and storage space, provided most of your data remains static.
The tool relies heavily on rsync for the actual data transfer and ssh for remote connections. Because it uses standard UNIX utilities, it is highly reliable and transparent. You can inspect any backup point directly as if it were a regular directory structure. This makes troubleshooting straightforward because you do not need special software to restore or view old versions of your files.
Installation and Initial Configuration
Before configuring backups, you must install the package appropriate for your distribution. On Debian-based systems, use apt-get install rsnapshot. For Red Hat Enterprise Linux or CentOS, use yum install rsnapshot. Once installed, the main configuration file is typically located at /etc/rsnapshot.conf.
Edit this file to define your backup intervals and targets. The configuration syntax is strict, so ensure you use tabs instead of spaces for indentation. Key directives include config_version 1.2, which ensures compatibility with modern features. You should also set snapshot_root to specify where all backup data will be stored, such as /backup/.
Another critical setting is cmd_rsync. By default, it points to /usr/bin/rsync. If your system uses a different path, update this line accordingly. Similarly, verify the paths for cmd_ssh and cmd_logger. Properly configuring these commands ensures that rsnapshot can communicate with remote hosts and log its activities correctly.
Defining Backup Intervals and Sources
Rsnapshot manages backups through intervals, commonly named daily, weekly, and monthly. In the configuration file, you define how many snapshots of each type to retain. For example, retain daily 7 keeps seven days of daily backups. This rotation policy ensures that older backups are automatically removed when the limit is reached, preventing uncontrolled disk usage.
Next, define the sources and destinations. A typical entry looks like this:
backup /home/ localhost/
backup /etc/ localhost/
This tells rsnapshot to back up the local /home and /etc directories into the snapshot root. If you need to back up remote servers, you can specify an SSH user and host:
backup user@remotehost:/var/www/ remote_www/
Ensure that the SSH key authentication is set up correctly so that rsnapshot can connect without password prompts. This is essential for automated cron jobs. Without proper key exchange, the backup process will fail or hang waiting for manual input.
Handling Large Data Sets
If you are backing up large directories, consider using the rsh_command or optimizing rsync options. Adding --delete to the rsync command ensures that files deleted from the source are also removed from the latest snapshot, keeping the backup clean. However, exercise caution with this option, as it permanently deletes data in the backup if misconfigured.
Scheduling and Execution
Once configured, test your setup manually before scheduling it. Run rsnapshot configtest to validate the configuration file for syntax errors. Then, perform a dry run with rsnapshot -t daily to see what actions would be taken without actually modifying any files. This step helps identify potential issues with permissions or network connectivity.
After verifying the configuration, execute the first real backup with rsnapshot daily. Monitor the output for errors. If successful, add a cron job to automate the process. For example, to run daily backups at 2 AM, add the following line to your crontab:
0 2 * * * /usr/bin/rsnapshot daily
You can schedule weekly and monthly backups similarly, ensuring they run less frequently to save resources. Remember that cron jobs run as the user who owns them, so ensure that user has read access to all source directories and write access to the snapshot root.
Restoring Data
Restoring data from rsnapshot is intuitive because the backup structure mirrors the original file system. Navigate to the snapshot root directory, then to the specific interval folder (e.g., /backup/daily.0/). Copy the required files back to their original locations or to a new location for analysis.
For example, to restore a deleted file from yesterday's backup:
cp /backup/daily.1/home/user/document.txt /home/user/
This simplicity is one of rsnapshot's greatest strengths. Users do not need to learn complex restoration tools; standard UNIX commands suffice. Always verify the integrity of restored files to ensure no corruption occurred during the backup process.
Frequently Asked Questions
Can I backup to a remote server?
Yes, rsnapshot supports remote backups via SSH. You simply specify the remote user and hostname in the configuration file, similar to how you would use rsync over SSH. Ensure that SSH keys are properly configured for passwordless login to automate the process.
How does rsnapshot handle deleted files?
Rsnapshot preserves deleted files in older snapshots due to its hard-linking mechanism. Only the latest snapshot reflects the current state of the source. Older snapshots remain intact until they are rotated out according to your retention policy, allowing you to recover files that were recently deleted.
Is rsnapshot suitable for databases?
While rsnapshot can copy database files, it is not ideal for live databases because it may capture inconsistent states. It is recommended to flush and lock the database before running the backup, or use database-specific dump tools and then back up the resulting SQL files with rsnapshot.
What happens if a backup fails midway?
If a backup fails, rsnapshot leaves the partial snapshot in place. You should manually remove the incomplete snapshot directory before retrying to avoid confusion. Checking the logs for error messages will help you diagnose why the interruption occurred.
How much disk space does rsnapshot use?
Rsnapshot is highly space-efficient because it uses hard links for unchanged files. The initial backup consumes full disk space, but subsequent backups only use space for modified or new files. The total space required is roughly equivalent to one full backup plus the changes since the last backup.