Don't like ads? PRO users don't see any ads ;-)

uni backup script in progress

By: DanozDotNet on Apr 15th, 2012  |  syntax: Bash  |  size: 3.57 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #1/bin/bash
  2.  
  3. # usage statement to tell users how to use this script.
  4. USAGE=$(
  5.   cat <<EOF
  6. $0 [OPTIONS]
  7. -h      Display the first 5 lines of a file to be backed up.
  8. -c      To count the files backed up
  9. -v      Enable verbosity
  10. -m      The Month to start backups from
  11. -y      The Year to start backups from
  12. -d      The Date to start backups from
  13. -b      The target directory to backup
  14. EOF
  15. )
  16.  
  17. # variables, set these ones to zero so we have a baseline to work with.
  18. HEAD="0"
  19. COUNT="0"
  20. VERBOSE="0"
  21. GDATE="0"
  22. MONTH="0"
  23. YEAR="0"
  24.  
  25. # set these variables to something useful
  26. DIR=`pwd`
  27. TARGET="$HOME/.backup/"
  28. CURMONTH=`date '+%y%m'`
  29.  
  30. # used to set bold/normal fonts in terminal.
  31. BOLD=`tput bold`
  32. NORM=`tput sgr0`
  33.  
  34. # show usage if nothing was specifed.
  35. if [ $# -eq 0 ]
  36.   then
  37.     echo "$USAGE"
  38.     exit 1
  39. fi
  40.  
  41. # get runtime options
  42. while getopts ":hcvxm:y:d:b:" OPTION; do
  43.   case "$OPTION" in
  44.     h) HEAD=1;;
  45.     c) COUNT=1;;
  46.        # verbosity isn't used yet.
  47.     v) VERBOSE=1;;
  48.     x) echo "$USAGE"
  49.        exit 1;;
  50.        # if statement to check that the month string given is numeric, starts with valid numbers and is 2 digits long.
  51.     m) if (( `echo "$OPTARG" | grep -E '^[0-1][[:digit:]]{1}'` ))
  52.          then
  53.            MONTH="$OPTARG"
  54.        else
  55.          echo "${BOLD}Month must be entered in the correct format - please try again :)${NORM}"
  56.          exit 1
  57.        fi ;;
  58.        # if statement to check that the year string given is numeric, starts with valid numbers and is 4 digits long.
  59.     y) if (( `echo "$OPTARG" | grep -E '^2[[:digit:]]{3}'` ))
  60.           then
  61.             YEAR="$OPTARG"
  62.        else
  63.          echo "${BOLD}Year must be entered in the correct format - please try again :)${NORM}"
  64.          exit 1
  65.        fi ;;
  66.        # need to add error checking to date specified to make sure it's a real date.
  67.     d) GDATE="$OPTARG";;
  68.     b) TARGET="$OPTARG";;
  69.   esac
  70. done
  71.  
  72. # set backup date. can we use the other date for this?
  73. if (( "$MONTH" != "0" )) && (( "$YEAR" != "0" ))
  74.   then
  75.     # the user specified a month and year, so we'll select the 1st day of the month.
  76.     BDATE=`echo "$YEAR$MONTH""01" |  sed 's ..  '`
  77. elif (( "$GDATE" == "0" ))
  78.   then
  79.     # because a day isn't set, we'll choose the 1st day of the month.
  80.     BDATE="$CURMONTH""01"
  81. else
  82.     # we'll use the date specified by the user
  83.     BDATE="$GDATE"
  84. fi
  85.  
  86. # print the header of each file to be backed up, if specfiied
  87. if (( "$HEAD" == "1" ))
  88.   then
  89.     # find searches the pwd for all files newer than the date specified based on modification timestamp and pipes it to head to display the first 5 lines.
  90.     HEADERS=`find "$DIR" -newermt "$BDATE" -print0 | xargs -0 head -n 5 -`
  91.     echo "${BOLD}Printing out first 5 lines of EVERY file to be backed up.${NORM}"
  92.     echo "$HEADERS"
  93. fi
  94.  
  95. # print how many files are to be backed up
  96. if (( "$COUNT" == "1" ))
  97.   then
  98.     # find searches the pwd for all files newer than the date specified based on modification timestamp and pipes it to wc to count how many there are.
  99.     NUMFILES=`find "$DIR" -newermt "$BDATE" -print | wc -l`
  100.     echo "${BOLD}Files to be backed up: $NUMFILES ${NORM}"
  101. fi
  102.  
  103. # backup files modified after a certain date
  104. echo "${BOLD}Backing up specified files now.${NORM}"
  105. # find searches the pwd for all files newer than the date specified based on modification timestamp, these are then sent to tar to backup and bzip2.
  106. `find "$DIR" -newermt "$BDATE" -print0 | xargs -0 tar -cjf "$TARGET"backup-"$GDATE".tar.bz2 > /dev/null 2>&1`
  107. echo "${BOLD}Files have been successfully backed up.${NORM}"
  108.  
  109. exit 0
  110. #END OF FILE