Featured image of post Rsnapshot

Rsnapshot

A good utility for incremental backups with auto-rotation

Introduction

Rsnapshot is a great utility that can:

  1. Backup folders in the local system and store them in a backup storage (a designated folder somewhere nearby).
  2. Run scripts that create backups, store them locally, and rsnapshot takes these files and puts them in the same backup folder.
  3. Send backups via sftp somewhere.
  4. Store only diffs between backups, saving space.

Retention

Rsnapshot has Retention Policies (I might not be calling it exactly right), which in the configuration start with retain. These are descriptions of abstract plans about how many recent backup copies to keep. I struggled for a while until I realized that their names don’t affect anything. So if a plan is called daily, rsnapshot won’t necessarily run it every day—you can run it monthly in cron if you want. I find it convenient to use names like daily, weekly, and monthly for configuration (for easier reading and maintenance).

Retention policies written lower in the config are considered to be executed less frequently than those higher up, so the number for those should be smaller. Rsnapshot uses the content from the higher retention i+1 to create the next lower retention i. Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
+ date
Tue May 12 05:58:33 UTC 2020
+ rsnapshot daily
+ ls -la /data/backups
total 48
drwxrwxr-x. 11 y.vlasov y.vlasov 4096 May 12 05:57 .
drwxrwxr-x+  6 root     root     4096 Apr 27 09:43 ..
drwxr-xr-x.  6 root     root     4096 May 12 05:57 daily.0
drwxr-xr-x.  6 root     root     4096 May 12 00:00 daily.1
drwxr-xr-x.  6 root     root     4096 May 11 00:00 daily.2
drwxr-xr-x.  6 root     root     4096 May 10 00:00 daily.3
drwxr-xr-x.  6 root     root     4096 May  9 00:00 daily.4
drwxr-xr-x.  6 root     root     4096 May  8 00:00 daily.5
drwxr-xr-x.  6 root     root     4096 May  7 00:00 daily.6
drwxr-xr-x.  6 root     root     4096 May  4 00:00 weekly.0
drwxr-xr-x.  6 root     root     4096 May  2 00:00 weekly.1
+ rsnapshot weekly
+ ls -la /data/backups
total 48
drwxrwxr-x. 11 y.vlasov y.vlasov 4096 May 12 05:57 .
drwxrwxr-x+  6 root     root     4096 Apr 27 09:43 ..
drwxr-xr-x.  6 root     root     4096 May 12 05:57 daily.0
drwxr-xr-x.  6 root     root     4096 May 12 00:00 daily.1
drwxr-xr-x.  6 root     root     4096 May 11 00:00 daily.2
drwxr-xr-x.  6 root     root     4096 May 10 00:00 daily.3
drwxr-xr-x.  6 root     root     4096 May  9 00:00 daily.4
drwxr-xr-x.  6 root     root     4096 May  8 00:00 daily.5
drwxr-xr-x.  6 root     root     4096 May  7 00:00 weekly.0
drwxr-xr-x.  6 root     root     4096 May  4 00:00 weekly.1
drwxr-xr-x.  6 root     root     4096 May  2 00:00 weekly.2

Backup, backup_script

So how does it perform a backup? Rsnapshot backs up all the steps listed in the configuration each time it is called.

  • backup - you specify a folder path to be backed up, and it copies it completely to the storage.
  • backup_script - you specify the path to a shell script that performs the backup itself, and places the resulting files/folders nearby, then rsnapshot moves them to the storage.
  • other options - man rsnapshot.

Plan

  1. Create a folder at the snapshot_root path.
  2. Write the rsnapshot.conf file (by default located at /etc/rsnapshot.conf).
  3. Add rsnapshot calls to the scheduler - cron.

Example

Let’s create the snapshot_root folder: mkdir -p /data/backups.

Contents of /etc/rsnapshot.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Note 1: You must use tabs to separate arguments on a line
config_version	1.2
snapshot_root	/data/backups
no_create_root	1
## Note 2: Paths may vary between OSes
cmd_rm		/usr/bin/rm
cmd_rsync	/usr/bin/rsync
cmd_logger	/usr/bin/logger
## Retention configuration
retain	daily	7
retain	weekly	4
## Logging settings
verbose		2
loglevel	3
lockfile	/var/run/rsnapshot.pid

## Describing the actions
backup	/data/ghost	ghost/
backup_script	/srv/my_soft/backup.sh	my_soft/

Contents of crontab

1
2
0 0 * * * /usr/bin/rsnapshot daily 2>&1 | systemd-cat -t rsnapshot-daily
10 0 * * 0 /usr/bin/rsnapshot weekly 2>&1 | systemd-cat -t rsnapshot-weekly

Contents of /srv/my_soft/backup.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/env sh
set -e

CMD='''
rm -rf backup; \
pg_basebackup -U my_user -X stream -F plain -D backup 1>/dev/null 2>&1 && \
tar -C backup -czf - $(ls backup)
'''

docker exec \
    -w /tmp \
    my_soft.postgres \
    /bin/sh -c "$CMD 1>pg_basebackup.tgz

Contents of daily.0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/data/backups/daily.0
├── ghost
│   └── data
├── my_soft
│   └── pg_basebackup.tgz
├── proxy
│   ├── certificates.tgz
│   └── haproxy.tgz
└── another_one
    └── another_backup_from_backup_script.tgz

5 directories, 4 files
Licensed under Apache License, Version 2.0
Last updated on Nov 19, 2024 10:58 +0200
All rights reserved
Built with Hugo
Theme Stack designed by Jimmy