Skip to content

Instantly share code, notes, and snippets.

@philcryer
Last active August 29, 2015 13:56
Show Gist options
  • Save philcryer/9210947 to your computer and use it in GitHub Desktop.
Save philcryer/9210947 to your computer and use it in GitHub Desktop.
goose-lift is a strangely named, simple backup script that handles backing up a docroot and a mysql database, copying the files to a remote server via scp and (optionally) emailing a report. It's not doing much error checking, so configure it and run it manually first. I've had to rewrite this enough times over the years to make it worth having …
#!/bin/bash
set -e
#######################################################
## goose-lift is a strangely named simple backup script
## ----------------------------------------------------
## * modify variables below to match your needed values
## * create an ssh key, and transfer it via ssh-copy-id
## * set cron example 00 11 * * 0,2,4,6 ~/goose-lift.sh
## * you are all done, start goose-lifting your server!
## ----------------------------------------------------
#### Creative Commons Zero “No Rights Reserved” License
#### src code https://gist.github.com/philcryer/9210947
#######################################################
#### TODO: setup an automated way to create user jails.
#### (warning next line breaks the line length pattern)
#### http://www.cyberciti.biz/faq/debian-ubuntu-restricting-ssh-user-session-to-a-directory-chrooted-jail/
#### fill out your variables (replace CAPITALIZED TEXT)
## varions bits
project_name=PROJECTNAME
datestamp=`date +'%Y%m%d'`
## local details
backup_dir=~/BACKUPS
www_root=/VAR/WWW/
db_user=USER ## use a readonly user here!
db_pass=PASSWORD
db_name=DATABASE
backups_to_keep="14"
## remote details
remote_host=REMOTE_HOST
remote_port=REMOTE_PORT
remote_user=REMOTE_USER
remote_backup_dir=~BACKUPS
## report specifics
email_report=yes
email_address="NAME@EMAIL"
email_from_address="`whoami`@`hostname`"
#######################################################
#### do the backup thing
if [ ! -d "${backup_dir}" ]; then
mkdir -p ${backup_dir}
fi
cd ${backup_dir}
mysqldump --opt --lock-tables=false -u ${db_user} -p${db_pass} ${db_name} > ${datestamp}_${project_name}_mysql.sql
tar zcfv ${datestamp}_${project_name}_www.tar.gz ${www_root}
tar zcfv ${datestamp}_${project_name}_mysql+www.tar.gz ${datestamp}_${project_name}_mysql.sql ${datestamp}_${project_name}_www.tar.gz
#### transfer the backup to a remote server
scp -P ${remote_port} ${datestamp}_${project_name}_mysql+www.tar.gz ${remote_user}@${remote_host}:~/${remote_backup_dir}
#### clean up unneeded files
rm ${datestamp}_${project_name}_mysql.sql
rm ${datestamp}_${project_name}_www.tar.gz
find * -mtime +${backups_to_keep} -exec rm {} \;
#### email report
if [[ ${email_report} == 'yes' ]]; then
email_subject="[backup] ${project_name} ${datestamp}"
email_body="`ls -lh ${backup_dir}`"
echo ${email_body} | /usr/bin/mail -s "${email_subject}" -r ${email_from_address} ${email_address}
fi
#######################################################
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment