]> git.somenet.org - root/btrfs.git/blob - snapshot.sh
new snapshotter to have numbered snapshots back.
[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 # Timestamp of creation.
24 STARTTS="`date +'%Y-%m-%d--%H-%M'`"
25 CALLPATH="`pwd`"
26 MAXSNAPP=25
27 DELSNAPP=5
28
29 echo $STARTTS
30 mount /mnt/btrfs &> /dev/null
31 cd /mnt/btrfs
32 #exit 0
33
34 #for every .autosnap subvol...
35 for vol in $(find /mnt/btrfs -xdev -maxdepth 1 -iname '*.autosnap'); do
36         VOLNAME=`echo "${vol}" | sed -e 's/\.autosnap//' `
37         echo "VOL: ${VOLNAME}"
38
39         # delete snap to allow move.
40         if [ -e "${VOLNAME}.$(echo "$MAXSNAPP+1"|bc).snap" ]; then
41                 /sbin/btrfs subvolume delete "${VOLNAME}.$(echo "$MAXSNAPP+1"|bc).snap"
42         fi
43
44         # move existing snapshots
45         for i in $(seq ${MAXSNAPP} -1 0) ; do
46                 PLUSONE=$(echo "$i+1"|bc)
47                 if [ -e "${VOLNAME}.${i}.snap" ]; then
48                         mv "${VOLNAME}.${i}.snap" "${VOLNAME}.${PLUSONE}.snap"
49                 fi
50         done
51
52         # create new snapshot
53         touch -m -a "${VOLNAME}.autosnap"
54         /sbin/btrfs subvolume snapshot -r "${VOLNAME}.autosnap" "${VOLNAME}.0.snap"
55
56         # delete snapshots... (also fixes off by one)
57         for i in $(seq 0 ${DELSNAPP}) ; do
58                 if [ -e "${VOLNAME}.$(echo "$MAXSNAPP+$i"|bc).snap" ]; then
59                         /sbin/btrfs subvolume delete "${VOLNAME}.$(echo "$MAXSNAPP+$i"|bc).snap"
60                 fi
61         done
62 done
63
64 cd $CALLPATH
65 umount /mnt/btrfs &> /dev/null || echo "Failed to unmount"
66