Skip to content

Instantly share code, notes, and snippets.

@sohooo
Last active May 23, 2021 06:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohooo/9101278 to your computer and use it in GitHub Desktop.
Save sohooo/9101278 to your computer and use it in GitHub Desktop.
Simple script to create a backup of your Wordpress database. It uses the credentials from your wp-config.php and mysqldump's the data to wp-content/backups. Place it next to your wp-config.php and adjust $KEEP_DAYS.
#!/bin/bash
# fail on error
set -e
pushd `dirname $0` > /dev/null
SCRIPT=`pwd -P`
popd > /dev/null
KEEP_DAYS=150
CONFIG=${SCRIPT}/wp-config.php
function extract_from_config() {
cat $CONFIG | grep $1 | cut -d',' -f2 | tr -d '[:blank:][:punct:][:cntrl:]'
}
# get credentails from wp-config.php
NAME=$(extract_from_config "DB_NAME")
PASS=$(extract_from_config "DB_PASSWORD")
LABEL=$(date '+%Y%m%d-%H%M')
DEST=${SCRIPT}/wp-content/backups
FILE=${DEST}/dump_${NAME}-${LABEL}.sql.gz
echo "start mysql db backup of $NAME"
echo "dest: $FILE"
# setup backup destination if needed
if [[ ! -d $DEST ]]; then
mkdir $DEST
echo "deny from all" > ${DEST}/.htaccess
fi
# delete dumps older than 150 days
echo "deleting dumps older than ${KEEP_DAYS} days"
find ${DEST}/*.gz -type f -mtime +${KEEP_DAYS} -print
find ${DEST}/*.gz -type f -mtime +${KEEP_DAYS} -delete
# dump database
mysqldump --user=${NAME} --password=${PASS} ${NAME} | gzip -c > $FILE
# show dump size
echo "total db dumps size in ${DEST}:"
du -sh $DEST
@ananich
Copy link

ananich commented Jul 12, 2020

It removes special characters from the password. Try this: sed -E "s/^define\('"$1"', '(.*)'\);/\1/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment