]> git.somenet.org - root/btrfs.git/blob - snapshot.sh
create readonly snapshots.
[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 # Set volumes to autosnap
24 SNAPLIST="root home homeroot"
25
26 # Timestamp of creation.
27 STARTTS="`date +'%Y-%m-%d--%H-%M'`"
28 CALLPATH="`pwd`"
29
30 echo $STARTTS
31 mount /mnt/btrfs &> /dev/null
32 cd /mnt/btrfs
33
34 for vol in $SNAPLIST; do
35         if [ -e "ahour_curr_${vol}.snp" -a -e "ahour_curr_${vol}.txt" ]; then
36                 if [ -e "ahour_last_${vol}.snp" -a -e "ahour_last_${vol}.txt" ]; then
37                         echo "Autosnap: Moving ahour_last_${vol}.snp to: `cat ahour_last_${vol}.txt`"
38                         mv "ahour_last_${vol}.snp" "`cat ahour_last_${vol}.txt`"
39                 fi
40                 echo "Autosnap: Moving ahour_curr_${vol}.snp to: ahour_last_${vol}.snp"
41                 mv "ahour_curr_${vol}.snp" "ahour_last_${vol}.snp"
42                 mv "ahour_curr_${vol}.txt" "ahour_last_${vol}.txt"
43         fi
44         echo "Autosnap: Snapshotting ahour_curr_${vol}.snp (${STARTTS})"
45         echo "aadate_${STARTTS}_${vol}.snap" > ahour_curr_${vol}.txt
46         btrfs subvolume snapshot -r "${vol}" "ahour_curr_${vol}.snp"
47         touch -m -a "ahour_curr_${vol}.snp"
48         touch -m -a "${vol}"
49 done
50
51 cd $CALLPATH
52 umount /mnt/btrfs &> /dev/null || echo "Failed to unmount"
53