Last active
May 23, 2021 06:17
-
-
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.
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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It removes special characters from the password. Try this:
sed -E "s/^define\('"$1"', '(.*)'\);/\1/"