2 ################################################
 
   3 ### Managed by someone's ansible provisioner ###
 
   4 ################################################
 
   5 # Part of: https://git.somenet.org/root/pub/somesible.git
 
   6 # 2017-2025 by someone <someone@somenet.org>
 
   9 # Copyright 2011 by Jan Vales <jan@jvales.net> (Someone <someone@somenet.org>)
 
  11 #       This program is free software: you can redistribute it and/or modify
 
  12 #       it under the terms of the GNU General Public License as published by
 
  13 #       the Free Software Foundation, version 3 of the License.
 
  15 #       This program is distributed in the hope that it will be useful,
 
  16 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  17 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
 
  18 #       GNU General Public License for more details.
 
  20 #       You should have received a copy of the GNU General Public License
 
  21 #       along with this program.        If not, see <http://www.gnu.org/licenses/>.
 
  25 # Manage BTRFS snapshots
 
  26 # 0 * * * * /root/somescripts/btrfs/snapshot.sh &> /dev/null
 
  29 BTRFS_SNAPSHOT_DELSNAP=10
 
  31 if [ -z "$BTRFS_SNAPSHOT_MAXSNAP" ]; then
 
  32     BTRFS_SNAPSHOT_MAXSNAP=13
 
  37 for fs in $(find /btrfs/ -xdev -mindepth 1 -maxdepth 1 -type d); do
 
  38     STARTTS="`date +'%Y-%m-%d--%H-%M'`"
 
  39     echo "FS: $fs -- $STARTTS"
 
  42     if [ -e "${fs}.automount" ]; then
 
  43         mount $fs &> /dev/null
 
  47     #for every .autosnap subvol...
 
  48     for vol in $(find $fs -xdev -maxdepth 1 -iname '*.autosnap'); do
 
  49         VOLNAME=`echo "${vol}" | sed -e 's/\.autosnap//' `
 
  50         echo "VOL: ${VOLNAME}"
 
  53         for i in $(seq -f %02g $(echo "${BTRFS_SNAPSHOT_MAXSNAP}+${BTRFS_SNAPSHOT_DELSNAP}"|bc) -1 ${BTRFS_SNAPSHOT_MAXSNAP}); do
 
  54             if [ -e "${VOLNAME}.${i}.snap" ]; then
 
  55                 btrfs subvolume delete "${VOLNAME}.${i}.snap"
 
  59         # move existing snapshots
 
  60         for i in $(seq -f %02g ${BTRFS_SNAPSHOT_MAXSNAP} -1 0) ; do
 
  61             if [ -e "${VOLNAME}.${i}.snap" ]; then
 
  62                 mv "${VOLNAME}.${i}.snap" "${VOLNAME}.$(echo "$i"|awk '{printf "%02g",$0+1}').snap"
 
  67         touch -m -a "${VOLNAME}.autosnap"
 
  68         btrfs subvolume snapshot -r "${VOLNAME}.autosnap" "${VOLNAME}.00.snap"
 
  73     if [ -e "${fs}.automount" ]; then
 
  74         umount $fs &> /dev/null || echo "Failed to unmount"
 
  78 echo "my-btrfs-cleaner: $(ps x | grep '\[btrfs-cleaner\]')" > /dev/kmsg