Snapper
Contents
About
- Supports btrfs, ext4 and lvm. This example uses btrfs.
- When you are using snapper for the root filesystem, please make sure that "/" and "/var/log" reside on different subvolumes.
Furthermore "/" should not be the root-subvolume with subvolid=5. This prevents the "logging" from being reset to an old status and you will be able to analyse a problem.
You may create and manage snapshots
- based on a timeline
- on boot
- before and after an action (pre/post)
Hooks
Apt
/etc/apt/apt.conf.d/80snapper
1 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=770938
2 DPkg::Pre-Invoke { "if [ -e /etc/default/snapper ]; then . /etc/default/snapper; fi; if [ -x /usr/bin/snapper ] && [ ! x$DISABLE_APT_SNAPSHOT = 'xyes' ] && [ -e /etc/snapper/configs/root ]; then rm -f /var/tmp/snapper-apt || true ; snapper create -d apt -c number -t pre -p > /var/tmp/snapper-apt || true ; snapper cleanup number || true ; fi"; };
3 DPkg::Post-Invoke { "if [ -e /etc/default/snapper ]; then . /etc/default/snapper; fi; if [ -x /usr/bin/snapper ] && [ ! x$DISABLE_APT_SNAPSHOT = 'xyes' ] && [ -e /var/tmp/snapper-apt ]; then snapper create -d apt -c number -t post --pre-number=`cat /var/tmp/snapper-apt` || true ; snapper cleanup number || true ; fi"; };
Systemd on boot
/usr/lib/systemd/system/snapper-boot.service
/usr/lib/systemd/system/snapper-boot.timer
To repair some broken times and services
Systemd#Migration from /lib to /usr/lib
Config
Create config-template
/etc/snapper/config-templates/snapper.template
1 ### SNAPPER-CONFIGS(5)# Filesystem Snapshot Management
2 ### Boolean values must be "yes" or "no".
3
4 SUBVOLUME="/"
5 # Path of the subvolume or mount point.
6 # There is no default value. The value must always be specified.
7
8 FSTYPE=btrfs
9 # Filesystem type for the subvolume.
10 # Default value is "btrfs" but it's recommended to always specify the
11 # filesystem type.
12
13 ALLOW_USERS=""
14 # List of users allowed to operate with the config. The user-names
15 # must be separated by spaces. Spaces in usernames can be escaped with
16 # a "\".
17 # Also see the PERMISSONS section in snapper(8).
18 # Default value is "" but "root" is always implicitly included.
19
20 ALLOW_GROUPS="snapper"
21 # List of groups allowed to operate with the config. The group-names
22 # must be separated by spaces. Spaces in group-names can be escaped
23 # with a "\".
24 # Also see the PERMISSONS section in snapper(8).
25 # Default value is "".
26
27 SYNC_ACL=yes
28 # Defines whether snapper will sync the users and groups from
29 # ALLOW_USERS and ALLOW_GROUPS to the ACL of the .snapshots directory.
30 # Also see the PERMISSONS section in snapper(8).
31 # Default value is "no".
32 # New in version 0.2.0.
33
34 BACKGROUND_COMPARISON=yes
35 # Defines whether pre and post snapshots should be compared in the
36 # background after creation.
37 # Default value is "yes".
38
39 ### CLEANUP ALGORITHMS
40 ######################
41
42 ### NUMBER
43 NUMBER_CLEANUP=no
44 # Defines whether the number cleanup algorithm should be run for the
45 # config.
46 # Default value is "no".
47
48 NUMBER_MIN_AGE=1800
49 # Minimal age for snapshots to be deleted by the number cleanup
50 # algorithm.
51 # Default value is "1800".
52
53 NUMBER_LIMIT=50
54 # Defines how many snapshots the number cleanup algorithm should
55 # keep. The youngest snapshots will be kept.
56 # Default value is "50".
57
58 NUMBER_LIMIT_IMPORTANT=10
59 # Defines how many important snapshots the number cleanup algorithm
60 # should keep. Important snapshots have important=yes in the userdata.
61 # The youngest important snapshots will be kept.
62 # The number of normal and important snapshots are counted
63 # independently.
64 # Default value is "10".
65 # New in version 0.1.8.
66
67 ### TIMELINE
68 TIMELINE_CREATE=yes
69 # Defines whether hourly snapshots should be created.
70 # Together with the timeline cleanup algorithm this will create a
71 # collection of snapshots with more snapshots is the near past and less
72 # snapshots in the far past.
73 # Default value is "no".
74
75 TIMELINE_CLEANUP=yes
76 # Defines whether the timeline cleanup algorithm should be run for
77 # the config.
78 # Default value is "no".
79
80 TIMELINE_MIN_AGE=14400
81 # Minimal age for snapshots to be deleted by the timeline cleanup
82 # algorithm.
83 # Default value is "1800".
84
85 TIMELINE_LIMIT_HOURLY=23
86 # Defines how many hourly snapshots the timeline cleanup algorithm
87 # should keep. An hourly snapshot is the first snapshot in an hour. The
88 # youngest hourly snapshots will be kept.
89 # Default value is "10".
90
91 TIMELINE_LIMIT_DAILY=14
92 # Defines how many daily snapshots the timeline cleanup algorithm
93 # should keep. A daily snapshot is the first snapshot in a day. The
94 # youngest daily snapshots will be kept.
95 # Default value is "10".
96
97 TIMELINE_LIMIT_MONTHLY=6
98 # Defines how many monthly snapshots the timeline cleanup algorithm
99 # should keep. A monthly snapshot is the first snapshot in a month. The
100 # youngest monthly snapshots will be kept.
101 # Default value is "10".
102
103 TIMELINE_LIMIT_YEARLY=1
104 # Defines how many yearly snapshots the timeline cleanup algorithm
105 # should keep. A yearly snapshot is the first snapshot in a year. The
106 # youngest yearly snapshots will be kept.
107 # Default value is "10".
108
109 ### EMTPY PRE POST
110 EMPTY_PRE_POST_CLEANUP=yes
111 # Defines whether the empty-pre-post cleanup algorithm should be run
112 # for the config.
113 # Default value is "no".
114
115 EMPTY_PRE_POST_MIN_AGE=1800
116 # Minimal age for snapshots to be deleted by the empty-pre-post
117 # cleanup algorithm.
118 # Default value is "1800".
119
Create config
Create snapper config from a template
Delete config
List configs
Query config information
1 # snapper -c rootfs get-config
2 Schlüssel | Wert
3 -----------------------+--------
4 ALLOW_GROUPS | snapper
5 ALLOW_USERS |
6 BACKGROUND_COMPARISON | yes
7 EMPTY_PRE_POST_CLEANUP | yes
8 EMPTY_PRE_POST_MIN_AGE | 1800
9 FSTYPE | btrfs
10 NUMBER_CLEANUP | no
11 NUMBER_LIMIT | 50
12 NUMBER_LIMIT_IMPORTANT | 10
13 NUMBER_MIN_AGE | 1800
14 SUBVOLUME | /
15 SYNC_ACL | yes
16 TIMELINE_CLEANUP | yes
17 TIMELINE_CREATE | yes
18 TIMELINE_LIMIT_DAILY | 14
19 TIMELINE_LIMIT_HOURLY | 23
20 TIMELINE_LIMIT_MONTHLY | 6
21 TIMELINE_LIMIT_YEARLY | 1
22 TIMELINE_MIN_AGE | 14400
Cleanup timelines
This may be useful after changing the snapper config for a filesystem. The cleanup itself happens in the background (lazy space reclaim).
Cleanup timelines
A small script to rename/link snapshots
/usr/local/sbin/snapper_rename.sh
Delete multiple snapshots
Delete multiple snapshots filtered by
1 snapper -c root list \
2 |grep 'Do 19 Aug 2021' \
3 |cut -f 1 -d\ \
4 |xargs -L1 snapper -c root delete
5 ### FILTER AS YOU LIKE
6 DATE="$(date -d 'yesterday' '+%c' \
7 |cut -f 1-4 -d\ )"
8 DATE="$(date -d '3 weeks ago' '+%c' \
9 |cut -f 1-4 -d\ )"
10 [ "$DATE" ] \
11 && snapper -c root list \
12 |grep "$DATE" \
13 |cut -f 1 -d\ \
14 |xargs -L1 snapper -c root delete