]> git.somenet.org - root/pub/somesible.git/blob - roles/base/btrfs-helpers/files/default/btrfs-snapshot.sh
[roles/util/letsencrypt-cert] request letsencrypt-cert helper role
[root/pub/somesible.git] / roles / base / btrfs-helpers / files / default / btrfs-snapshot.sh
1 #!/bin/bash
2 ################################################
3 ### Managed by someone's ansible provisioner ###
4 ################################################
5 # Part of: https://git.somenet.org/root/pub/somesible.git
6 # 2017-2024 by someone <someone@somenet.org>
7 #
8
9 # Copyright 2011 by Jan Vales <jan@jvales.net> (Someone <someone@somenet.org>)
10 #
11 #       This program is free software: you can redistribute it and/or modify
12 #       it under the terms of the GNU General Public License as published by
13 #       the Free Software Foundation, version 3 of the License.
14 #
15 #       This program is distributed in the hope that it will be useful,
16 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
18 #       GNU General Public License for more details.
19 #
20 #       You should have received a copy of the GNU General Public License
21 #       along with this program.        If not, see <http://www.gnu.org/licenses/>.
22 #
23
24 #
25 # Manage BTRFS snapshots
26 # 0 * * * * /root/somescripts/btrfs/snapshot.sh &> /dev/null
27 #
28
29 BTRFS_SNAPSHOT_DELSNAP=10
30
31 if [ -z "$BTRFS_SNAPSHOT_MAXSNAP" ]; then
32     BTRFS_SNAPSHOT_MAXSNAP=13
33 fi
34
35 CALLPATH="`pwd`"
36
37 for fs in $(find /btrfs/ -xdev -mindepth 1 -maxdepth 1 -type d); do
38     STARTTS="`date +'%Y-%m-%d--%H-%M'`"
39     echo "FS: $fs -- $STARTTS"
40     chmod u=rwX,go=X $fs
41
42     if [ -e "${fs}.automount" ]; then
43         mount $fs &> /dev/null
44     fi
45     cd $fs
46
47     #for every .autosnap subvol...
48     for vol in $(find $fs -xdev -maxdepth 1 -iname '*.autosnap'); do
49         VOLNAME=`echo "${vol}" | sed -e 's/\.autosnap//' `
50         echo "VOL: ${VOLNAME}"
51
52         # delete snapshots...
53         for i in $(seq -f %02g $(echo "${BTRFS_SNAPSHOT_MAXSNAP}+${BTRFS_SNAPSHOT_DELSNAP}"|bc) -1 ${BTRFS_SNAPSHOT_MAXSNAP}); do
54             if [ -e "${VOLNAME}.${i}.snap" ]; then
55                 btrfs subvolume delete "${VOLNAME}.${i}.snap"
56             fi
57         done
58
59         # move existing snapshots
60         for i in $(seq -f %02g ${BTRFS_SNAPSHOT_MAXSNAP} -1 0) ; do
61             if [ -e "${VOLNAME}.${i}.snap" ]; then
62                 mv "${VOLNAME}.${i}.snap" "${VOLNAME}.$(echo "$i"|awk '{printf "%02g",$0+1}').snap"
63             fi
64         done
65
66         # create new snapshot
67         touch -m -a "${VOLNAME}.autosnap"
68         btrfs subvolume snapshot -r "${VOLNAME}.autosnap" "${VOLNAME}.00.snap"
69     done
70
71     cd $CALLPATH
72
73     if [ -e "${fs}.automount" ]; then
74         umount $fs &> /dev/null || echo "Failed to unmount"
75     fi
76 done
77
78 echo "my-btrfs-cleaner: $(ps x | grep '\[btrfs-cleaner\]')" > /dev/kmsg