]> git.somenet.org - root/btrfs.git/blob - snapshot.sh
GITOLITE.txt
[root/btrfs.git] / snapshot.sh
1 #!/bin/bash
2 #
3 # Copyright 2011 by Jan Vales <jan@jvales.net> (Someone <someone@somenet.org>)
4 #
5 #       This program is free software: you can redistribute it and/or modify
6 #       it under the terms of the GNU General Public License as published by
7 #       the Free Software Foundation, version 3 of the License.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
12 #       GNU General Public License for more details.
13 #
14 #       You should have received a copy of the GNU General Public License
15 #       along with this program.        If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 #
19 # Take BTRFS snapshots
20 # 0 * * * * /root/somescripts/btrfs/snapshot.sh &> /dev/null
21 # This script will generate: current_$vol.snp/.txt + last_$vol.snp/.txt and date_$date_$vol.snp/.txt
22 #
23
24 MAXSNAPP=25
25 DELSNAPP=5
26
27 STARTTS="`date +'%Y-%m-%d--%H-%M'`"
28 CALLPATH="`pwd`"
29 PATH="${PATH}:/sbin"
30
31 echo $STARTTS
32 mount /mnt/btrfs &> /dev/null
33 cd /mnt/btrfs
34
35 #for every .autosnap subvol...
36 for vol in $(find /mnt/btrfs -xdev -maxdepth 1 -iname '*.autosnap'); do
37         VOLNAME=`echo "${vol}" | sed -e 's/\.autosnap//' `
38         echo "VOL: ${VOLNAME}"
39
40         # delete snap to allow move.
41         if [ -e "${VOLNAME}.$(echo "$MAXSNAPP+1"|bc).snap" ]; then
42                 btrfs subvolume delete "${VOLNAME}.$(echo "$MAXSNAPP+1"|bc).snap"
43         fi
44
45         # move existing snapshots
46         for i in $(seq ${MAXSNAPP} -1 0) ; do
47                 PLUSONE=$(echo "$i+1"|bc)
48                 if [ -e "${VOLNAME}.${i}.snap" ]; then
49                         mv "${VOLNAME}.${i}.snap" "${VOLNAME}.${PLUSONE}.snap"
50                 fi
51         done
52
53         # create new snapshot
54         touch -m -a "${VOLNAME}.autosnap"
55         btrfs subvolume snapshot -r "${VOLNAME}.autosnap" "${VOLNAME}.0.snap"
56
57         # delete snapshots... (also fixes off by one)
58         for i in $(seq 0 ${DELSNAPP}) ; do
59                 if [ -e "${VOLNAME}.$(echo "$MAXSNAPP+$i"|bc).snap" ]; then
60                         btrfs subvolume delete "${VOLNAME}.$(echo "$MAXSNAPP+$i"|bc).snap"
61                 fi
62         done
63 done
64
65 cd $CALLPATH
66 umount /mnt/btrfs &> /dev/null || echo "Failed to unmount"
67
68 echo "my-btrfs-cleaner: $(ps x | grep '\[btrfs-cleaner\]')" > /dev/kmsg
69