Last active
September 9, 2019 13:58
-
-
Save fmtarif/37f2171f7bfeaa48ce07097d5a914a76 to your computer and use it in GitHub Desktop.
#bash #cli #wp DB backup script, parse DB credentials from wp-config.php or .env (ini format) files
This file contains hidden or 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
#!/bin/bash | |
#change these variables as necessary for current deployment | |
CONFIG_FILE_PATH=/path/to/config_file | |
MYCONF_EXTRA_FILE=./.mysqlpwd | |
BACKUP_FILE_PATH=./dbdump-$(date '+%F_time_%I%M%p').sql | |
##for wp-config.php file format | |
DBHOST=`cat $CONFIG_FILE_PATH | grep DB_HOST | cut -d \' -f 4` | |
DBNAME=`cat $CONFIG_FILE_PATH | grep DB_NAME | cut -d \' -f 4` | |
DBUSER=`cat $CONFIG_FILE_PATH | grep DB_USER | cut -d \' -f 4` | |
DBPASS=`cat $CONFIG_FILE_PATH | grep DB_PASSWORD | cut -d \' -f 4` | |
##for ini like files, for example, .env file use the following block | |
#DBHOST=`cat $CONFIG_FILE_PATH | grep DB_HOST | cut -d \= -f 2` | |
#DBNAME=`cat $CONFIG_FILE_PATH | grep DB_DATABASE | cut -d \= -f 2` | |
#DBUSER=`cat $CONFIG_FILE_PATH | grep DB_USERNAME | cut -d \= -f 2` | |
#DBPASS=`cat $CONFIG_FILE_PATH | grep DB_PASSWORD | cut -d \= -f 2` | |
#writing to file helps when we want to manually use mysql and mysqldump from CLI, dont need to use -u -p anymore | |
cat > "$MYCONF_EXTRA_FILE" <<EOL | |
[mysqldump] | |
host=$DBHOST | |
user=$DBUSER | |
password=$DBPASS | |
[client] | |
host=$DBHOST | |
user=$DBUSER | |
password=$DBPASS | |
EOL | |
mysqldump --defaults-extra-file=$MYCONF_EXTRA_FILE $DBNAME > $BACKUP_FILE_PATH && \ | |
ls -lah $BACKUP_FILE_PATH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment