Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ampp3d Wordpress sync script - production to dev
#########################################################
# Remote MySQL Backup and Download
# Author: Brian Strickland
#
# 1) Log into a remote server
# 2) Run a MySQL dump on a database
# 3) Copies the dump to the local machine.
#
# You can use this for any database/server, just change
# the top parameters.
#########################################################
# Remote server info
remoteUser=william
remoteHost=servername
# DB Info
dbUser=ampp3d
dbPass=databasepassword
dbHost=thedatabase.server.com
dbName=ampp3d
# Backup options
backupName="$dbName"-$(date +"%Y%m%d-%H%M").sql
backupPath=/home/william/"$backupName"
downloadName=.
# Login to remote computer and run backup.
echo "Logging into $remoteHost"
ssh -l "$remoteUser" "$remoteHost" "
# Run the mysql backup
echo "Running MySQL backup. This may take a while…"
mysqldump --opt "$dbName" > "$backupPath"
gzip "$backupPath"
echo "Backup complete."
# Close the session
exit;
"
# Copy the new database backup from remote host to current computer.
echo "Downloading $backupName.gz to $downloadName"
scp "$remoteUser"@"$remoteHost":"$backupPath".gz "$downloadName"
echo "Download complete."
gzip -cd "$backupName".gz | mysql ampp3d
echo "Local DB overwritten."
mysql ampp3d < ampp3d-fetchdb-patch.sql
echo "Production to Dev SQL commands run"
echo "Finished ampped import - remember to turn off minify and varnish plugins"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment