Created
June 3, 2019 08:38
-
-
Save stirtingale/d68f4a2260f7101a8ce4440d69f9d4d0 to your computer and use it in GitHub Desktop.
Nightly Database Backup 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
#!/bin/bash | |
# | |
# | |
BACKUPPATH=~/dbbackup/sql | |
SITELIST=~/dbbackup/sitelist-wp-sites.sdata | |
# | |
# Common Logging Code | |
# | |
function LogStart | |
{ | |
echo "====== Log Start =========" >> $LF | |
echo "Time: `date`" >> $LF | |
echo " " >> $LF | |
} | |
function LogEnd | |
{ | |
echo " " >> $LF | |
echo "Time: `date`" >> $LF | |
echo "====== Log End =========" >> $LF | |
} | |
function LogMsg | |
{ | |
echo "`date '+%Y-%m-%d|%H:%M:%S|'`$$|OK|$1" >> $LF | |
} | |
function LogError | |
{ | |
echo "`date '+%Y-%m-%d|%H:%M:%S|'`$$|ERROR|$1" >> $LF | |
} | |
function LogCritical | |
{ | |
echo "`date '+%Y-%m-%d|%H:%M:%S|'`$$|CRITICAL|$1" >> $LF | |
} | |
UMASK=002 | |
FILE_DATE=`date '+%Y-%m-%d'` | |
LF_DIR=~/dbbackup/logs | |
LF=$LF_DIR/wp-backup-$FILE_DATE.log | |
mkdir -p $LF_DIR | |
chmod 777 ~/dbbackup | |
touch $LF | |
chmod 644 $LF | |
#---------------------------------------- | |
# | |
# Process any command line parameters | |
# | |
#---------------------------------------- | |
LogStart | |
LogMsg "Backup directory: $TARGET_DIR" | |
LogStart | |
if [ ! -d ${BACKUPPATH} ] | |
then | |
mkdir -p ${BACKUPPATH} | |
LogMsg "Creating DB-Backup Dir" | |
fi | |
find ~/ -type f -name wp-config.php -print > ${SITELIST} | |
cnt=0 | |
while read LINE | |
do | |
FDT=`date '+%Y%m%d%H%M'` | |
FD=`date +'%d'` | |
DBNAME=`cat ${LINE} | grep DB_NAME | cut -d \' -f 4` | |
DBUSER=`cat ${LINE} | grep DB_USER | cut -d \' -f 4` | |
DBPWD=`cat ${LINE} | grep DB_PASSWORD | cut -d \' -f 4` | |
DBBACKUPFILE=${BACKUPPATH}/$DBNAME-${FD}_backup.sql | |
DBBACKUPFILE_GZ=${BACKUPPATH}/$DBNAME-${FD}_backup.sql.gz | |
# Clear existing | |
rm $DBBACKUPFILE | |
rm $DBBACKUPFILE_GZ | |
/usr/bin/mysqldump --opt -u ${DBUSER} -p${DBPWD} $DBNAME > $DBBACKUPFILE | |
gzip $DBBACKUPFILE | |
# echo $DBUSER | |
# echo $DBPWD | |
# echo $DBNAME | |
# echo $DBBACKUPFILE | |
LogMsg " " | |
LogMsg "Database [${DBNAME}]" | |
LogMsg "Virtual Host [${LINE}]" | |
LogMsg "Backup File [${DBBACKUPFILE}]" | |
((cnt++)) | |
done < ${SITELIST} | |
LogMsg "Found ${cnt} sites to backup." | |
ls -la ${BACKUPPATH}/*${FD}* >> $LF | |
rm -f ${SITELIST} | |
LogEnd | |
# | |
# End of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment