# # LVM snapshots # check_lvm () { local path=$1 lvs $path > /dev/null 2>&1 return $? } get_lvm_snapshot () { local path=$1 local devname=${path##*/} local size=$2 [ -z "$size" ] && size=10 lvcreate -s -L ${size}G -n "${devname}-snapshot" $path > /dev/null if [ $? -ne 0 -a $? -ne 5 ]; then echo "ERROR: Failed to create LVM snapshot for: $path" return 1 fi echo "${path%/*}/${devname}-snapshot" return 0 } remove_lvm_snapshot () { local path=$1 sync lvremove -f $path >/dev/null } # # daily/weekly names rotation # get_backup_file () { local day_of_week=$( date +"%w" ) # 0-6 local day_of_month=$( date +"%_d" ) # 1-... local week_of_month=$(( ($day_of_month-1)/7 )) [ $week_of_month -eq 4 ] && week_of_month=3 if [ $day_of_week -eq 0 ]; then echo "weekly/w${week_of_month}-" else echo "daily/d${day_of_week}-" fi } # # RSync # do_rsync () { local localdir=$1 local remotedir=$2 local verbosity="--quiet --no-motd" [ -n "${RSYNC_EXCLUDE}" ] && excludefile="--exclude-from=${RSYNC_EXCLUDE}" [ -n "${RSYNC_VERBOSE}" ] && verbosity="--human-readable --progress" rsync --archive --links --recursive \ --hard-links --perms --owner --group \ --devices --specials --times --sparse \ --compress --compress-level=9 ${excludefile} \ --rsh=ssh --delete --numeric-ids ${verbosity} \ "${localdir}" "${remotedir}" } # # dd_gzip # ddssh_makedirs () { local sessh=$1 local ddofbase=$2 ssh $sessh " mkdir -p ${ddofbase}/{daily,weekly}" } do_ddgzipssh () { local ddif=$1 local sessh=$2 local ddofbase=$3 local imgfile=${ddif##*/} imgfile=${imgfile%-snapshot} imgfile="$( get_backup_file )${imgfile}.img.gz" GZIP_CMD=${GZIP_CMD:-gzip} dd if=$ddif | $GZIP_CMD | ssh $sessh " cat > \"${ddofbase}/${imgfile}\"" }