Skip to content

Instantly share code, notes, and snippets.

@jsoningram
Last active April 7, 2016 02:36
Show Gist options
  • Save jsoningram/9791a24b7abed495ed90 to your computer and use it in GitHub Desktop.
Save jsoningram/9791a24b7abed495ed90 to your computer and use it in GitHub Desktop.
Shell script to backup WordPress site files and database with option to transfer to remote site
#!/bin/bash
# backup WP db and site files, takes one argument (name of document root)
# name of directory to backup (public_html, httpdocs etc)
dir=$1
cd /path/to/$dir
# get the db details
echo "Enter the db username:"
read dbusername
echo "Enter the db password:"
read -s dbpassword
echo "Enter the db name:"
read dbname
dumpfile=$dbusername`date +%H%M%j`.sql
tarball=$dbusername`date +%H%M%j`.tar
# run the dump
mysqldump --add-drop-table -u $dbusername -p$dbpassword $dbname > $dumpfile
# create the archive, but exclude backupbuddy directories
tar -vcf $tarball --exclude='backupbuddy' --exclude='backupbuddy_backups' .
# clean up
rm $dumpfile
echo "Do you want to transfer the backup to the remote?"
read a
case $a in
yes|y) scp $tarball CHANGETOYOURUSER@CHANGETOYOURDOMAIN:~/PATH/TO/DESTINATION;;
no|n) echo "Ok, we won't transfer the backup";;
*) echo "Please answer yes or no";;
esac
echo "all done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment