Created
May 8, 2019 08:26
-
-
Save danikaze/aba4c2537a02dafcaf5cea063fec0895 to your computer and use it in GitHub Desktop.
Backup a wordpress database and/or filesystem into one (or two) compressed files based on the input options
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
#!/usr/bin/env bash | |
# | |
# Wordpress site backup | |
# | |
# Backup a wordpress database and/or filesystem into one (or two) | |
# compressed files based on the input options | |
# | |
# Author: @danikaze <[email protected]> | |
# | |
###################### | |
# Print help message # | |
###################### | |
function help { | |
echo "Backup a wordpress site" | |
echo "Usage: $(basename $0) -s source -t target -p prefix -f timeFormat [-F] [-D [-d mysql|pgsql] [-o dbopts]] [-n nFilesSql] [-m nFilesData] [-u owner] [-a attr] [-x] [-v]" | |
echo | |
echo "Options:" | |
echo "-s source Folder where wordpress is installed (i.e. 'wp-config.php' should be here)" | |
echo "-t target Folder where store the resulting dump files" | |
echo "-p filename prefix Prefix of the filenames (date and file extension will be added)" | |
echo "-f date format Date function format (as `date +{format}`)" | |
echo "-d DB gestor manager [mysql|pgsql] Database gestor manager used (mysql by default)" | |
echo "-o db opts Extra options to pass to the database when creating the dump" | |
echo "-D Create db dump file" | |
echo "-F Create file system dump file" | |
echo "-u owner User to chown the resulting backup files (current one by default)" | |
echo "-a attr Attributes to chmod the resulting backup files (600 by default)" | |
echo "-x Do not compress resulting files" | |
echo "-v Enable verbose mode" | |
} | |
# default parameter values | |
DBTYPE=mysql | |
DBOPTS= | |
DBDUMP=0 | |
FILEDUMP=0 | |
OWNER=$(whoami) | |
ATTR=600 | |
VERBOSE=0 | |
DATEFORMAT="%Y-%m-%d_%H-%M-%S" | |
PREFIX="backup-" | |
COMPRESS=1 | |
################################### | |
# Echo parameters only if VERBOSE # | |
################################### | |
function verbose { | |
if [[ $VERBOSE = 1 ]]; then | |
echo $@ | |
fi | |
} | |
# Print errors to stderr | |
function errorEcho { | |
(>&2 echo $@) | |
} | |
##################### | |
# parameter parsing # | |
##################### | |
SETVARS=0 | |
while getopts "s:t:p:f:d:o:DFu:a:xv" OPTION | |
do | |
case $OPTION in | |
s) SOURCE="$OPTARG" | |
SETVARS=$(($SETVARS | 1)) | |
;; | |
t) TARGET="$OPTARG" | |
SETVARS=$(($SETVARS | 2)) | |
;; | |
p) PREFIX="$OPTARG" | |
;; | |
f) DATEFORMAT="$OPTARG" | |
;; | |
d) DBTYPE="$OPTARG" | |
;; | |
o) DBOPTS="$OPTARG" | |
;; | |
D) DBDUMP=1 | |
;; | |
F) FILEDUMP=1 | |
;; | |
u) OWNER="$OPTARG" | |
;; | |
a) ATTR="$OPTARG" | |
;; | |
x) COMPRESS=0 | |
;; | |
v) VERBOSE=1 | |
;; | |
?) help | |
exit 1 | |
;; | |
esac | |
done | |
# input checking | |
if [[ $DBTYPE != mysql && $DBTYPE != pgsql ]]; then | |
errorEcho "Database type [-d] must be mysql or pgsql. [$DBTYPE]" | |
exit 2 | |
fi | |
# check that the specified user exists in the system | |
if [[ $OWNER != "" ]]; then | |
if [[ `cat /etc/passwd | cut -d":" -f1 | grep "${OWNER}" | wc -l` != 1 ]]; then | |
errorEcho "Error: user $OWNER doesn't exist." | |
exit 3 | |
fi | |
fi | |
# check that all required variables are set | |
if [[ ${SETVARS} != 3 ]]; then | |
help | |
exit 4 | |
fi | |
# shift already parsed parameters | |
shift $((${OPTIND} -1)) | |
# create target directory if it doesn't exist | |
if [[ ! -d "${TARGET}" ]]; then | |
mkdir -p "${TARGET}" | |
fi | |
########### | |
# db dump # | |
########### | |
if [[ ${DBDUMP} = 1 ]]; then | |
verbose "Creating database dump" | |
FILE=${TARGET}/${PREFIX}`date +${DATEFORMAT}`.sql | |
# get the settings of the db | |
DBUSER=`cat ${SOURCE}/wp-config.php | grep DB_USER | cut -d "'" -f 4` | |
DBPASS=`cat ${SOURCE}/wp-config.php | grep DB_PASSWORD | cut -d "'" -f 4` | |
DBNAME=`cat ${SOURCE}/wp-config.php | grep DB_NAME | cut -d "'" -f 4` | |
# dump it | |
if [[ ${DBTYPE} = mysql ]]; then | |
nice mysqldump ${DBOPTS} --user=${DBUSER} --password=${DBPASS} ${DBNAME} > ${FILE} | |
else | |
nice pg_dump ${DBNAME} ${DBOPTS} -U ${DBUSER} > ${FILE} | |
fi | |
# change permissions | |
verbose "- Setting file attributes to ${ATTR}" | |
chmod ${ATTR} ${FILE} | |
# change owner | |
if [[ ${OWNER} != "" ]]; then | |
verbose "- Setting file owner to ${OWNER}" | |
chown ${OWNER} ${FILE} | |
fi | |
# compress it | |
if [[ ${COMPRESS} = 1 ]]; then | |
verbose "- Compressing the file" | |
gzip -f9 ${FILE} | |
FILE="${FILE}.gz" | |
fi | |
verbose "- Database dump created in ${FILE}" | |
fi | |
#################### | |
# file system dump # | |
#################### | |
if [[ ${FILEDUMP} = 1 ]]; then | |
verbose "Creating file system dump" | |
if [[ ${COMPRESS} = 1 ]]; then | |
EXT="tgz" | |
OPT="cfz" | |
else | |
EXT="tar" | |
OPT="cf" | |
fi | |
FILE=${TARGET}/${PREFIX}`date +${DATEFORMAT}`.${EXT} | |
nice tar ${OPT} "${FILE}" "${SOURCE}" 2>/dev/null | |
# change permissions | |
verbose "- Setting file attributes to ${ATTR}" | |
chmod ${ATTR} ${FILE} | |
# change owner | |
if [[ ${OWNER} != "" ]]; then | |
verbose "- Setting file owner to ${OWNER}" | |
chown ${OWNER} ${FILE} | |
fi | |
verbose "- File system dump created in ${FILE}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment