Skip to content

Instantly share code, notes, and snippets.

@spicecadet
Created October 7, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spicecadet/db6c1ce4f6ca7be923c3 to your computer and use it in GitHub Desktop.
Save spicecadet/db6c1ce4f6ca7be923c3 to your computer and use it in GitHub Desktop.
This is a quick bash script I built to handle db export and docroot backup for my local vagrant box. Potentially useful if you are using a Vagrant box that does not backup files and db on Vagrant Destroy. Note that you'll need to add your password to run the copy command on line 31.
#! /bin/bash
# create variables for directories
main_backup_dir=$HOME/Development/backups
vagrant_dir=$HOME/Development/hgv/hgv_data
vagrant_root_dir=$HOME/Development/hgv
today="$(date +'%m-%d-%Y')"_daily_backup
daily_backup_dir=$HOME/Development/backup/$today
# print directories to screen for testing
echo ----------: Main backup dir: $main_backup_dir
echo ----------: Vagrant directory: $vagrant_dir
echo ----------: Daily backup directory: $daily_backup_dir
# mysql backup
cd ${vagrant_root_dir}
echo ----------: changed to vagrant directory
pwd
vagrant ssh --command "mysqldump -u root --all-databases > ${today}.sql" &&
echo ----------: sql dump successful || echo ----------: sql dump failed
vagrant ssh --command "sudo mv ${today}.sql /hgv_data/sql-backup" &&
echo ----------: sql dump moved to backup directory || echo ----------: sql dump failed to move into backup directory
# if backup directory does not exist create directory and copy files
if [ -d ${daily_backup_dir} ]
then
echo ----------: $daily_backup_dir exists
echo ----------: backup has alredy run today
echo "This is an automated status message" | mail -s "Local backup: alredy run today" you@yourdomain.com
else
mkdir ${daily_backup_dir}
echo your_password_here | sudo cp -r ${vagrant_dir} /$daily_backup_dir &&
echo ----------: files copied && echo "This is an automated status message" | mail -s "Local backup: successful" you@yourdomain.com ||
echo backup failed && echo "This is an automated status message" | mail -s "Local backup: failed" you@yourdomain.com
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment