Last active
September 23, 2020 11:39
-
-
Save rmpel/84e34445f4039be3f22d8832e93e2ce1 to your computer and use it in GitHub Desktop.
Backup a WordPress database to a GIT repository to track changes over time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
### | |
# This script creates a backup using the WP-CLI tool and then stores it in a GIT repository. | |
# As GIT only saves the changes, you have a nice history of stuff that happened on your website. | |
# This is mostly an investigation tool and I have yet to determine the usefulness :) | |
# | |
# p.s., this does not work if git or wp-cli is installed; ergo; does NOT work with Local by Flywheel (unless you ssh into the docker and `sudo apt-get install git`. | |
## | |
# default: if not given on commandline, use wp found in the $PATH | |
[ "" = "$WP" ] && WP=$(which wp 2>/dev/null) | |
# if no WP-CLI foound in path nor given on command line, complaint and gie suggestion :) | |
[ "$WP" = "" ] && echo Could not detect presence of WP-CLI executable && echo Please set environment variable \$WP to full path to WP-CLI executable && echo example: WP=\~/scripts/wpcli.phar \"$0\" && exit 1; | |
# resolve symlinks | |
cd "$(pwd -P)" | |
# assume wordpress in current location | |
[ ! -f wp-load.php ] && echo "ERROR: Please execute script in the WordPress ROOT folder" && exit 1; | |
# try to create the database folder outside the web-root. | |
[ ! -d ../database ] && { mkdir ../database || { echo "Failed to create database folder, please create ../database yourself and give ample permissions" && exit 1; }; } | |
# test presence of git (the poor-man's way) and if not, create and config an empty repository | |
[ ! -d ../database/.git ] && cd ../database && git init && git config user.email "database-backup@your-email-domain.nl" && git config user.name "Database Backup Script for user $(whoami)" && cd - 2>&1 > /dev/null | |
# get the database name so the SQL file has a relevant name. | |
DB=$(${WP} config get DB_NAME) | |
# but default do 'database' if it cannot be determined | |
[ "" = "$DB" ] && DB=database | |
# extension | |
DB="${DB}.sql" | |
# file path | |
DBF=../database/${DB} | |
# does a file already exist? try to create a file and if that failed, complaint and give suggestion | |
[ ! -f "$DBF" ] && echo "nothing here yet" && touch "$DBF" && [ ! -f "$DBF" ] && echo "Failed to create database file. Please make sure user $(whoami) has WRITE ACCESS to ${DBF}" && exit; | |
# export the database | |
${WP} db export --skip-extended-insert "$DBF" | |
# commit the changes | |
cd ../database && git add "$DB" && git commit -m "$(date)" | |
# if you wish to push to a remote, configure a remote with | |
# git remote add origin git@github.com:username/repository.git | |
# and add | |
# git push | |
# here. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment