#1/bin/bash
# usage statement to tell users how to use this script.
USAGE=$(
cat <<EOF
$0 [OPTIONS]
-h Display the first 5 lines of a file to be backed up.
-c To count the files backed up
-v Enable verbosity
-m The Month to start backups from
-y The Year to start backups from
-d The Date to start backups from
-b The target directory to backup
EOF
)
# variables, set these ones to zero so we have a baseline to work with.
HEAD="0"
COUNT="0"
VERBOSE="0"
GDATE="0"
MONTH="0"
YEAR="0"
# set these variables to something useful
DIR=`pwd`
TARGET="$HOME/.backup/"
CURMONTH=`date '+%y%m'`
# used to set bold/normal fonts in terminal.
BOLD=`tput bold`
NORM=`tput sgr0`
# show usage if nothing was specifed.
if [ $# -eq 0 ]
then
echo "$USAGE"
exit 1
fi
# get runtime options
while getopts ":hcvxm:y:d:b:" OPTION; do
case "$OPTION" in
h) HEAD=1;;
c) COUNT=1;;
# verbosity isn't used yet.
v) VERBOSE=1;;
x) echo "$USAGE"
exit 1;;
# if statement to check that the month string given is numeric, starts with valid numbers and is 2 digits long.
m) if (( `echo "$OPTARG" | grep -E '^[0-1][[:digit:]]{1}'` ))
then
MONTH="$OPTARG"
else
echo "${BOLD}Month must be entered in the correct format - please try again :)${NORM}"
exit 1
fi ;;
# if statement to check that the year string given is numeric, starts with valid numbers and is 4 digits long.
y) if (( `echo "$OPTARG" | grep -E '^2[[:digit:]]{3}'` ))
then
YEAR="$OPTARG"
else
echo "${BOLD}Year must be entered in the correct format - please try again :)${NORM}"
exit 1
fi ;;
# need to add error checking to date specified to make sure it's a real date.
d) GDATE="$OPTARG";;
b) TARGET="$OPTARG";;
esac
done
# set backup date. can we use the other date for this?
if (( "$MONTH" != "0" )) && (( "$YEAR" != "0" ))
then
# the user specified a month and year, so we'll select the 1st day of the month.
BDATE=`echo "$YEAR$MONTH""01" | sed 's .. '`
elif (( "$GDATE" == "0" ))
then
# because a day isn't set, we'll choose the 1st day of the month.
BDATE="$CURMONTH""01"
else
# we'll use the date specified by the user
BDATE="$GDATE"
fi
# print the header of each file to be backed up, if specfiied
if (( "$HEAD" == "1" ))
then
# 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.
HEADERS=`find "$DIR" -newermt "$BDATE" -print0 | xargs -0 head -n 5 -`
echo "${BOLD}Printing out first 5 lines of EVERY file to be backed up.${NORM}"
echo "$HEADERS"
fi
# print how many files are to be backed up
if (( "$COUNT" == "1" ))
then
# 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.
NUMFILES=`find "$DIR" -newermt "$BDATE" -print | wc -l`
echo "${BOLD}Files to be backed up: $NUMFILES ${NORM}"
fi
# backup files modified after a certain date
echo "${BOLD}Backing up specified files now.${NORM}"
# 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.
`find "$DIR" -newermt "$BDATE" -print0 | xargs -0 tar -cjf "$TARGET"backup-"$GDATE".tar.bz2 > /dev/null 2>&1`
echo "${BOLD}Files have been successfully backed up.${NORM}"
exit 0
#END OF FILE