Skip to content

Instantly share code, notes, and snippets.

@joedooley
Created December 26, 2015 07:06
Show Gist options
  • Save joedooley/97f097d9ccab9c065f54 to your computer and use it in GitHub Desktop.
Save joedooley/97f097d9ccab9c065f54 to your computer and use it in GitHub Desktop.
From https://tomjn.com/2014/03/01/wordpress-bash-magic/ Pulling Down a Remote site Copy Into a Vagrant VM I use something similar to this to pull down this website into a Virtual machine automatically. If you use password authentication to log into your server rather than SSH Keys, or your using SFTP details, you’ll be prompted for it. If you us…
#!/bin/sh
### SSH Info ###
SUSER="serverusername";
SHOST="example.com";
SDIR="/srv/www/example.com";
echo 'starting rsync'
rsync -e "/usr/bin/ssh" --compress --stats -rlDHS $SUSER@$SHOST:$SDIR /srv/www/example.com
WPDBHOST=`cat /srv/www/example.com/wp-config.php | grep DB_HOST | cut -d \' -f 4`;
WPDBNAME=`cat /srv/www/tomjn.com/wp-config.php | grep DB_NAME | cut -d \' -f 4`;
WPDBUSER=`cat /srv/www/tomjn.com/wp-config.php | grep DB_USER | cut -d \' -f 4`;
WPDBPASS=`cat /srv/www/tomjn.com/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`;
echo 'finished rsync, grabbing database'
FILE=$SDIR/mysql-$WPDBNAME.sql.gz; # Set the backup filename
#echo "mysqldump -q -u $WPDBUSER -h $WPDBHOST -p$WPDBPASS $WPDBNAME | gzip -9 > $FILE";
ssh $SUSER@$SHOST "mysqldump -q -u $WPDBUSER -h $WPDBHOST -p$WPDBPASS $WPDBNAME | gzip -9 > backup.sql.gz"
scp $SUSER@$SHOST:./backup.sql.gz . # copy all the files to backup server
ssh $SUSER@$SHOST rm ./backup.sql.gz # delete files on db server
gunzip -d backup.sql.gz
mysql -u root -e "CREATE DATABASE IF NOT EXISTS $WPDBNAME"
mysql -u root -e "GRANT ALL PRIVILEGES ON $WPDBNAME.* To 'wp'@'localhost'"
mysql -u wp -pwp $WPDBNAME < backup.sql
sed -i "/DB_HOST/s/'[^']*'/'localhost'/2" wp-config.php
sed -i "/DB_USER/s/'[^']*'/'wp'/2" wp-config.php
sed -i "/DB_PASSWORD/s/'[^']*'/'wp'/2" wp-config.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment