tbak - scripts - various script and utils
 (HTM) git clone git://z3bra.org/scripts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       tbak (2148B)
       ---
            1 #!/bin/sh
            2 #
            3 # z3bra - (c) wtfpl 2014
            4 # Backup a file, and rotate backups : file.0.BAK - file.1.BAK, ...
            5 #
            6 # Based on a original idea from Ypnose. Thanks mate !
            7 # <http://ypnose.org/blog/2014/bakup-snippet.html>
            8 
            9 EXT=${EXT:-BAK} # extension used for backup
           10 LIM=${LIM:-9}   # maximum number of version to keep
           11 PAD=${PAD:-0}   # number to start with
           12 
           13 usage() {
           14     cat <<EOF
           15 usage: `basename $0` [-hrv] <file>
           16         -h          : print this help
           17         -r <num>    : perform <num> rotations if \$LIM is reached
           18 EOF
           19 }
           20 
           21 # report action performed in verbose mode
           22 log() {
           23     # do not log anything if not in $VERBOSE mode
           24     test -z $VERBOSE && return
           25 
           26     # add a timestamp to the message 
           27     echo "[$(date +%Y-%m-%d\ %H:%M)] - $*"
           28 }
           29 
           30 # rotate backups to leave moar room
           31 rotate() {
           32     # do not rotate if the rotate flags wasn't provided
           33     test -z $ROTATE && return
           34 
           35     # delete the oldest backup
           36     rm ${FILE}.${PAD}.${EXT}
           37 
           38     # move every file down one place
           39     for N1 in `seq $PAD $LIM`; do
           40         N2=$(( N1 + ROTATE ))
           41 
           42         # don't go any further
           43         test -f ${FILE}.${N2}.${EXT} || return
           44 
           45         # move file down $ROTATE place
           46         log "${FILE}.${N2}.${EXT} -> ${FILE}.${N1}.${EXT}"
           47         mv ${FILE}.${N2}.${EXT} ${FILE}.${N1}.${EXT}
           48     done
           49 }
           50 
           51 # actually archive files
           52 archive() {
           53     # test the presence of each version, and create one that doesn't exists
           54     for N in `seq $PAD $LIM`; do
           55         if test ! -f ${FILE}.${N}.${EXT}; then
           56 
           57             # cope the file under it's new name
           58             log "Created: ${FILE}.${N}.${EXT}"
           59             cp ${FILE} ${FILE}.${N}.${EXT}
           60 
           61             exit 0
           62         fi
           63     done
           64 }
           65 
           66 while getopts "hrv" opt; do
           67     case $opt in
           68         h) usage; exit 0 ;;
           69         r) ROTATE=1 ;;
           70         v) VERBOSE=1 ;;
           71         *) usage; exit 1 ;;
           72     esac
           73 done
           74 
           75 shift $((OPTIND - 1))
           76 
           77 test $# -lt 1 && usage && exit 1
           78 
           79 FILE=$1
           80 
           81 # in case limit is reach, remove the oldest backup
           82 test -f ${FILE}.${LIM}.${EXT} && rotate
           83 
           84 # if rotation wasn't performed, we'll not archive anything
           85 test -f ${FILE}.${LIM}.${EXT} || archive
           86 
           87 echo "Limit of $LIM .$EXT files reached run with -r to force rotation"
           88 exit 1