Creating a custom rsnapshot backup sequence involves configuring the rsnapshot.conf file to define specific retention intervals, such as daily, weekly, and monthly snapshots. By adjusting the retain directives and understanding how rsnapshot utilizes hard links for space efficiency, you can build a robust versioning strategy that prevents data loss while minimizing storage consumption.
Understanding Rsnapshot Retention Policies
The core of any rsnapshot configuration is the definition of your retention schedule. Unlike traditional backup tools that overwrite previous versions, rsnapshot creates incremental backups by using hard links. This means that if a file has not changed since the last backup, it is not copied again; instead, a new hard link is created in the snapshot directory pointing to the same inode on the disk. This mechanism allows you to maintain multiple full-looking backups with minimal additional disk usage. To establish a custom sequence, you must edit the main configuration file, typically located at/etc/rsnapshot.conf. The structure relies on a series of retain lines followed by the corresponding command line arguments. For example, a common setup might look like this:
# Keep 7 daily snapshots
retain daily 7
# Keep 4 weekly snapshots
retain weekly 4
# Keep 3 monthly snapshots
retain monthly 3
This configuration tells rsnapshot to keep seven copies of the most recent daily backups, four weekly backups, and three monthly backups. When you run rsnapshot, it will execute these tasks in order. If you exceed the number of retained snapshots, rsnapshot automatically removes the oldest one to make room for the new one. Understanding this hierarchy is crucial because it dictates which data persists over time. A daily backup might be deleted after seven days, but if that day also corresponds to a weekly retention point, the data remains until the weekly slot is cleared.
For more details on managing these data backups effectively, you can refer to our guide on [How to Manage Data Backup with Rsnapshot](/news/1480036/rsnapshot-data-backup).
Configuring Snapshot Root and Sources
Once your retention policy is set, you need to define where the backups are stored and what data is being backed up. Thesnapshot_root directive specifies the base directory for all snapshots. It is critical that this path exists and is writable by the user running rsnapshot (usually root or a dedicated backup user).
snapshot_root /backup/rsnapshot/
Following this, you define the source directories and their destinations. The syntax is straightforward: backup followed by the source path, then the destination path relative to the snapshot root. You can also exclude files using the exclude_file directive or inline exclusions.
backup /home/ home/ exclude=/home/user/.cache/*
backup /etc/ etc/
backup ssh://user@remotehost//var/log/ remote_logs/
In the example above, we are backing up local /home and /etc directories, as well as logs from a remote server via SSH. Note that when backing up remote hosts, you must ensure that SSH keys are properly configured for passwordless authentication, or that an SSH agent is available. Also, remember that rsnapshot does not support incremental transfers over SSH in the same way rsync does natively without specific flags; it relies on the underlying rsync binary to handle the transfer logic efficiently.
Optimizing Performance and Safety
To ensure your custom backup sequence runs smoothly, you should tune several parameters in the configuration file. One of the most important settings iscmd_rsync, which points to the rsync binary. Ensure this path is correct for your system. Additionally, you can pass arguments to rsync via the rsync_long_args directive.
rsync_long_args --delete --sparse --compress --numeric-ids
The --delete flag ensures that files deleted on the source are also removed from the latest snapshot, keeping your backups clean. However, use caution with this flag, as it can lead to data loss if misconfigured. The --sparse option helps save space by detecting sparse files, while --compress reduces bandwidth usage during network transfers.
Another critical aspect is testing your configuration before relying on it for production data. You can perform a dry run to see what changes would be made without actually executing them.
rsnapshot -t daily
This command simulates the daily backup process. Review the output carefully to ensure that no unexpected deletions or errors occur. If the dry run looks good, you can proceed with the actual execution.
Scheduling Backups with Cron
Finally, automate your custom sequence using cron jobs. You do not need to write complex scripts; simply adding entries to the crontab is sufficient. Here is an example of how to schedule daily, weekly, and monthly backups:# Daily backup at 1 AM
0 1 * * * /usr/bin/rsnapshot daily
# Weekly backup on Sunday at 2 AM
0 2 * * 0 /usr/bin/rsnapshot weekly
# Monthly backup on the 1st at 3 AM
0 3 1 * * /usr/bin/rsnapshot monthly
Ensure that the cron daemon is running and that the cron user has permission to execute rsnapshot. Logging is essential for troubleshooting. You can redirect the output of rsnapshot to a log file to monitor the progress and identify any issues.
0 1 * * * /usr/bin/rsnapshot daily >> /var/log/rsnapshot.log 2>&1
By combining a well-defined retention policy, proper source configuration, performance tuning, and reliable scheduling, you can create a custom rsnapshot backup sequence that is both efficient and secure. Always test your configurations thoroughly and monitor your logs to ensure your data remains safe.