#!/bin/bash ############################### # # # spapshot script for btrfs # # --- daemon --- # # v0.1 # # # ############################### ## settings ## export logfile="../var/log/backsnap.log" function main { #foar each hour while [ true ]; do for volume in `ls ../etc/backsnap/volumes/`; do [ -n "`cat "../etc/backsnap/volumes/$volume" | tr [:upper:] [:lower:] | grep enabled=true`" ] && snap_volume "../etc/backsnap/volumes/$volume" & done sleep 1m done } ## snap_ ## #snap_volume function snap_volume { conf="$1" prof="`basename $conf`" timestamp="../etc/backsnap/stamps/$prof" error= [ -f "$timestamp" ] || touch "$timestamp" chmod +x "$timestamp" . "$timestamp" #getconf for t in years months weeks days hours path_vol path_bak MK MV RM CH; do eval "$t=`cat "$conf" | grep -v "#" | tr [:upper:] [:lower:] | grep "$t=" | sed -s 's/'"$t="'/''/g' | tr -d " "`" [ -z "${!t}" ] && eval "$t=0" done #set defaults MK="btrfs subvolume snapshot -r" MV="mv -v" RM="btrfs subvolume delete" CH="-d" #check conf ... #path_vol & path_bak exist (ar not 0 ...) #min one time number gt 0 for t in years months weeks days hours; do var="`echo $t | tr -d s`" [ "${!t}" -gt 0 ] && [ "`timeget_$var`" != "${!var}" ] && { #eigendliches backupen... logon echo "$prof: SETTINGS: \"years=$years\" \"months=$months\" \"weeks=$weeks\" \"days=$days\" \"hours=$hours\" \"path_vol=$path_vol\" \"path_bak=$path_bak\"" logon "$prof: MODE: $var" i=$((--$t)) #delete oldest if border is reached [ $CH "$path_bak$var.$i" ] && { logon "$prof: SNAP: DEL: \"$path_bak$var.$i\"" error="`eval $RM "$path_bak$var.$i" 3>&1 1>&2 2>&3`" [ -n "$error" ] && { logon "$prof: SNAP: DEL: ERROR: $error"; error=; } sleep 3 } #move all snaps one back while [ $i -ge 1 ]; do [ $CH "$path_bak$var.$((--i))" ] && { logon "$prof: SNAP: MV: \"$path_bak$var.$i\" \"$path_bak$var.$((i+1))\"" error="`eval $MV "$path_bak$var.$i" "$path_bak$var.$((i+1))" 3>&1 1>&2 2>&3`" [ -n "$error" ] && { logon "$prof: SNAP: MV: ERROR: $error"; error=; } sleep 3 } done #mk new snap logon "$prof: SNAP: MK: \"$path_vol\" \"$path_bak$var.$i\"" error="`eval $MK "$path_vol" "$path_bak$var.$i" 3>&1 1>&2 2>&3`" [ -n "$error" ] && { logon "$prof: SNAP: MK: ERROR: $error"; error=; } wr_stamp "$timestamp" "$prof" break } done } function timeget_year { date +%Y; } function timeget_month { date +%m; } function timeget_week { date +%V; } function timeget_day { date +%d; } function timeget_hour { date +%H; } #wr_stamp function wr_stamp { timestamp="$1" echo $1 reason="$2" echo "## timestamp for $reason ##" > "$timestamp" echo "## `date +%Y-%m-%d_%H-%M` ##" >> "$timestamp" echo >> "$timestamp" echo "year=`timeget_year`" >> "$timestamp" echo "month=`timeget_month`" >> "$timestamp" echo "week=`timeget_week`" >> "$timestamp" echo "day=`timeget_day`" >> "$timestamp" echo "hour=`timeget_hour`" >> "$timestamp" } function logon { delimeter="#" echo "`date +%Y%m%d%H%M%S` $delimeter `echo $@ | tr -d "$delimeter"`" >> "$logfile" } main