3 # Copyright 2011 by Jan Vales <jan@jvales.net> (Someone <someone@somenet.org>)
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.
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.
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/>.
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
23 # Timestamp of creation.
24 STARTTS="`date +'%Y-%m-%d--%H-%M'`"
30 mount /mnt/btrfs &> /dev/null
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}"
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"
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"
53 touch -m -a "${VOLNAME}.autosnap"
54 /sbin/btrfs subvolume snapshot -r "${VOLNAME}.autosnap" "${VOLNAME}.0.snap"
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"
65 umount /mnt/btrfs &> /dev/null || echo "Failed to unmount"