Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrsimonbennett/4636d68405c802a1fe3c880fa1ca1c60 to your computer and use it in GitHub Desktop.
Save mrsimonbennett/4636d68405c802a1fe3c880fa1ca1c60 to your computer and use it in GitHub Desktop.
#!/bin/bash
# © 2017-2021 SnapShooter Limited.
echo "# © 2017-{{date('Y')}} SnapShooter Limited."
echo "# Terms and Conditions https://snapshooter.io/snapshooter-terms-and-conditions"
echo ""
echo "Which Directory do you want the backups to restore to (/var/www/sitename)"
read wp_dir
echo Backups will be restored to $wp_dir
echo ""
echo "Please give us the download url for the MYSQL database"
read wp_mysql_url
echo ""
echo "Please give us the download url for the ASSETS database"
read wp_assets_url
echo ""
rm /tmp/restore.tar.gz
curl $wp_assets_url -o /tmp/restore.tar.gz
tarDirectory=$( cat /tmp/restore.tar.gz | gunzip - | tar -tvf - | head -1 | rev | cut -d " " -f 1 | rev)
strip=$(awk -F"/" '{print NF-1}' <<< "${tarDirectory}")
echo Found the DIR in tar to be $tarDirectory
echo Restoring site now
cd $wp_dir
cp $wp_dir/wp-config.php /tmp/wp-config.php.back
cat /tmp/restore.tar.gz | tar -zxf - --strip=$strip
ls /tmp
rm $wp_dir/wp-config.php
mv /tmp/wp-config.php.back $wp_dir/wp-config.php
echo ""
rm /tmp/restore.tar.gz
echo Restoring Database
MYSQL_USERNAME=$(cat wp-config.php | grep DB_USER | cut -d \' -f 4)
MYSQL_PASSWORD=$(cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4)
MYSQL_HOST=$(cat wp-config.php | grep DB_HOST | cut -d \' -f 4)
MYSQL_PORT=$(php -r "echo explode(':','$MYSQL_HOST')[1] ?? 3306;")
MYSQL_DATABASE=$(cat wp-config.php | grep DB_NAME | cut -d \' -f 4)
echo "mysql -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DATABASE"
curl $wp_mysql_url --output - | gunzip - | mysql -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DATABASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment