Guest User

Untitled

a guest
Jun 24th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #!/bin/sh
  2. # betmen
  3. # simple backup script
  4.  
  5. EXT=${EXT:-NOOSCBAK} # extension used for backup
  6. LIM=${LIM:-9} # maximum number of version to keep
  7. PAD=${PAD:-0} # number to start with
  8.  
  9. usage() {
  10. cat <<EOF
  11. usage: `basename $0` [-hrv] <file>
  12. -h : print this help
  13. -r <num> : perform <num> rotations if \$LIM is reached
  14. EOF
  15. }
  16.  
  17. # report action performed in verbose mode
  18. log() {
  19. # do not log anything if not in $VERBOSE mode
  20. test -z $VERBOSE && return
  21.  
  22. # add a timestamp to the message
  23. echo "[$(date +%Y-%m-%d\ %H:%M)] - $*"
  24. }
  25.  
  26. rotate() {
  27. # do not rotate if the rotate flags wasn't provided
  28. test -z $ROTATE && return
  29.  
  30. # delete the oldest backup
  31. rm ${FILE}.${PAD}.${EXT}
  32.  
  33. # move every file down one place
  34. for N1 in `seq $PAD $LIM`; do
  35. N2=$(( N1 + ROTATE ))
  36.  
  37. # don't go any further
  38. test -f ${FILE}.${N2}.${EXT} || return
  39.  
  40. # move file down $ROTATE place
  41. log "${FILE}.${N2}.${EXT} -> ${FILE}.${N1}.${EXT}"
  42. mv ${FILE}.${N2}.${EXT} ${FILE}.${N1}.${EXT}
  43. done
  44. }
  45.  
  46. # actually archive files
  47. archive() {
  48. # test the presence of each version, and create one that doesn't exists
  49. for N in `seq $PAD $LIM`; do
  50. if test ! -f ${FILE}.${N}.${EXT}; then
  51.  
  52. # cope the file under it's new name
  53. log "Created: ${FILE}.${N}.${EXT}"
  54. cp ${FILE} ${FILE}.${N}.${EXT}
  55.  
  56. exit 0
  57. fi
  58. done
  59. }
  60.  
  61. while getopts "hrv" opt; do
  62. case $opt in
  63. h) usage; exit 0 ;;
  64. r) ROTATE=1 ;;
  65. v) VERBOSE=1 ;;
  66. *) usage; exit 1 ;;
  67. esac
  68. done
  69.  
  70. shift $((OPTIND - 1))
  71.  
  72. test $# -lt 1 && usage && exit 1
  73.  
  74. FILE=$1
  75.  
  76. # in case limit is reach, remove the oldest backup
  77. test -f ${FILE}.${LIM}.${EXT} && rotate
  78.  
  79. # if rotation wasn't performed, we'll not archive anything
  80. test -f ${FILE}.${LIM}.${EXT} || archive
  81.  
  82. echo "Limit of $LIM .$EXT files reached run with -r to force rotation"
  83. exit 1
Add Comment
Please, Sign In to add comment