Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Show instructions
- function show_usage {
- echo "Backup account and database, assuming /home/USER filesystem setup."
- echo "Usage:"
- echo " -h Help menu - See this information"
- echo " Files: "
- echo " -a Account user name, for use in /home/USER backup. If no path is set via -p flag, the /home/USER path will be backed up. "
- echo " -f Filepath to Backup "
- echo " Database: "
- echo " -d Name of Database to backup "
- echo " -h Host of database, if not localhost "
- echo " -p Database Password to use for backing up "
- echo " -u Database User to use for backup "
- echo " Backup File "
- echo " -b Name and path of backup file to create. '.tar.bz2' will be appended to the filename "
- exit 1
- }
- #Sanity Check - are there no arguments?
- if [ $# -eq 0 ]; then
- show_usage
- fi
- #Set variables to be used
- user=''
- filepath=''
- database=''
- dbfile=''
- dbuser=''
- dbpass=''
- dbhost='127.0.0.1'
- bkpath=''
- finalbkpath=''
- finalbkfile=''
- #Parse flags
- while getopts “ha:f:d:p:u:b:” OPTION; do
- case $OPTION in
- h)
- show_usage
- ;;
- a)
- user=$OPTARG
- ;;
- f)
- filepath=$OPTARG
- ;;
- d)
- database=$OPTARG
- dbfile="$database-`date +%Y%m%d`.sql"
- ;;
- h)
- dbhost=$OPTARG
- ;;
- p)
- dbpass=$OPTARG
- ;;
- u)
- dbuser=$OPTARG
- ;;
- b)
- bkpath=$OPTARG
- ;;
- *)
- show_usage
- ;;
- esac
- done
- #Backup based on filepath instead of /home/USER if used
- if [ "$filepath" != '' ] && [ -d $filepath ]; then
- finalbkpath="$filepath"
- elif [ -d /home/$user ]; then
- finalbkpath="/home/$user"
- else
- echo 'Invalid file path or no user specified'
- show_usage
- fi
- #Get database dump, if database is defined
- if [ "$dbuser" != "" ] && [ "$database" != "" ]; then
- mysqldump -u ${dbuser} -p${dbpass} -h ${dbhost} ${database} > ./${dbfile}
- elif [ "$database" != "" ]; then
- mysqldump -h ${dbhost} ${database} > ./${dbfile}
- fi
- #Create backup
- if [ -f ./${dbfile} ]; then
- tar -cjf backup-`date +%Y%m%d`.tar.bz2 ${finalbkpath} ./${dbfile}
- else
- tar -cjf backup-`date +%Y%m%d`.tar.bz2 ${finalbkpath}
- fi
Add Comment
Please, Sign In to add comment