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
27 STARTTS="`date +'%Y-%m-%d--%H-%M'`"
32 mount /mnt/btrfs &> /dev/null
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}"
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"
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"
54 touch -m -a "${VOLNAME}.autosnap"
55 btrfs subvolume snapshot -r "${VOLNAME}.autosnap" "${VOLNAME}.0.snap"
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"
66 umount /mnt/btrfs &> /dev/null || echo "Failed to unmount"
68 echo "my-btrfs-cleaner: $(ps x | grep '\[btrfs-cleaner\]')" > /dev/kmsg