#!/bin/bash # remote backup support-script # rotate: renames backups: name.03->name.04, name.02->name.03 # prepare: creates new backup directory name.00 # cleanup: removes all backup directories whose numbers exceed a limit # configuration backupRoot="/data/backups" # --- rotate backups -------- rotate() { remoteHost="$1" backupType="$2" backupDir="$backupRoot/$remoteHost" find "$backupDir/$backupType."* -type d -maxdepth 0 | sort -r | while read dir; do nr="${dir##*.}" nr="${nr#0}" let nr1=nr+1 nr1=`printf "%02d" $nr1` mv "$dir" "$backupDir/$backupType.$nr1" done } # --- prepare backup ------- prepare() { remoteHost="$1" backupType="$2" backupDir="$backupRoot/$remoteHost" targetDir="$backupDir/$backupType.00" mkdir -p "$targetDir" echo "$targetDir" exit 0 } # --- delete old backup ------ cleanup() { remoteHost="$1" backupType="$2" maxGen="$3" backupDir="$backupRoot/$remoteHost" while [ $maxGen -lt 100 ]; do nr=`printf "%02d" $maxGen` if [ ! -d "$backupDir/$backupType.$nr" ]; then exit 0 else rm -fr "$backupDir/$backupType.$nr" let maxGen=maxGen+1 fi done } # --- main program ------- func="$1" # check allowed function grep -qw "$func" <<< "prepare rotate cleanup" || exit 3 shift eval $func "$@"