-
-
Save jdani/433e33f1e2c8d492e6b7 to your computer and use it in GitHub Desktop.
Wordpress Backup bash script
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 | |
# Exit if any command fails | |
set -o errexit | |
# Exit if trying to use undeclared var | |
set -o nounset | |
# On piped commands, use the exit status of the last command throwing a non-zero exit code | |
set -o pipefail | |
# Current dir | |
CURRENT_DIR=$(pwd) | |
# Date | |
NOW=$(date +"%Y-%m-%d-%H%M%S") | |
# Wordpress sitename | |
SITENAME="a3sec.com" | |
# Wordpress root path | |
WWW_DIR="/var/www/html" | |
# Wordpress DB Config. Config at wp-config.php should work. | |
DB_USER="wpuser" | |
DB_PASS="pAzas4ufrup5" | |
DB_NAME="A3Sec" | |
DB_HOST="localhost" | |
# Output dir. Will be gziped as the output file too. | |
BACKUP_DIR="${SITENAME}-${NOW}" | |
# Output file with wordpress file system | |
TREE_FILE="${SITENAME}-files-${NOW}.tar.gz" | |
# Output file with wordpress db | |
DB_FILE="${SITENAME}-db.${NOW}.sql.gz" | |
# http://mostlyunixish.franzoni.eu/blog/2013/10/08/quick-log-for-bash-scripts/ | |
LOGFILE=${SITENAME}_${NOW}.log | |
RETAIN_NUM_LINES=10 | |
function logsetup { | |
TMP=$(tail -n ${RETAIN_NUM_LINES} ${LOGFILE} 2>/dev/null) && echo "${TMP}" > ${LOGFILE} | |
exec > >(tee -a ${LOGFILE}) | |
exec 2>&1 | |
} | |
function log { | |
echo "[$(date)]: $*" | |
} | |
# Have to change dir before logsetup.... | |
cd $(dirname $0) | |
# Setting up logfile | |
logsetup | |
log Changed to dir: $(dirname $0) | |
log Creating backup dir: ${BACKUP_DIR} | |
mkdir -p ${BACKUP_DIR} | |
log Compressing Wordpress base dir: ${WWW_DIR} | |
tar -czvf ${BACKUP_DIR}/${TREE_FILE} ${WWW_DIR} &> /dev/null | |
log Dumping database: ${DB_NAME} | |
mysqldump -h${DB_HOST} -u${DB_USER} -p${DB_PASS} ${DB_NAME} | gzip > ${BACKUP_DIR}/${DB_FILE} &> /dev/null | |
log Packing output files to: ${BACKUP_DIR}.tgz | |
tar zcvf ${BACKUP_DIR}.tgz ${BACKUP_DIR} &> /dev/null | |
log Uploading output file to Dropbox: ${BACKUP_DIR}.tgz | |
./dropbox_uploader.sh upload ${BACKUP_DIR}.tgz ${BACKUP_DIR}.tgz &> /dev/null | |
log Deleting backup dir: ${BACKUP_DIR} | |
rm -rf ${BACKUP_DIR} &> /dev/null | |
log Uploading logfile to Dropbox: ${LOGFILE} | |
log Logfile will be trunked here... | |
./dropbox_uploader.sh upload ${LOGFILE} ${LOGFILE} &> /dev/null | |
log Changing to original dir: ${CURRENT_DIR} | |
cd ${CURRENT_DIR} | |
log Everything was OK, exiting.... | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wordpress backup script forked from https://gist.github.com/fcingolani/3839861
It was modified add some features:
Dropbox uploader from https://github.com/andreafabrizi/Dropbox-Uploader has to be configured, placed in the same directory and with exec permissions.