Last active
February 6, 2019 11:08
-
-
Save prochor666/119c529ff6c2d8027fa09f4fd92dd085 to your computer and use it in GitHub Desktop.
Backup web hosts and MySQL databases
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 | |
# --------------------------------------------------------------------- | |
# Web hosts backup script | |
# [email protected] | |
# Full backup few days (7 is default) | |
# This script is licensed under GNU GPL version 2.0 or above | |
# --------------------------------------------------------------------- | |
clear | |
# Setup | |
BACKUPROOT="/var/webserver-backup" | |
MYSQLBACKUPROOT="$BACKUPROOT/mysql" | |
WEBBACKUPROOT="$BACKUPROOT/webhosts" | |
WEBROOT="/var/www" | |
MUSER="root" | |
MPASS="mysqlpassword" | |
MHOST="localhost" | |
MYSQL="$(which mysql)" | |
MYSQLDUMP="$(which mysqldump)" | |
GZIP="$(which gzip)" | |
TAR="$(which tar)" | |
LOGFILE="$BACKUPROOT/webserver-backup.log" | |
_cleanup () { | |
find $MYSQLBACKUPROOT -type f -mtime +$1 -print | xargs -I {} rm {} | |
find $WEBBACKUPROOT -type f -mtime +$1 -print | xargs -I {} rm {} | |
} | |
_get_date_suffix () { | |
V=`date +"%Y-%m-%d"` | |
echo "$V" | |
} | |
_get_dir_size () { | |
echo `du -hs $1 | cut -f1` | |
} | |
_log_reset () { | |
echo "" > $LOGFILE | |
} | |
_log () { | |
T=`date +"%d.%m.%Y %T"` | |
echo "$T $1" >> $LOGFILE | |
} | |
# Check dirs | |
if [ ! -d $MYSQLBACKUPROOT ]; then | |
mkdir -p $MYSQLBACKUPROOT | |
fi | |
if [ ! -d $WEBBACKUPROOT ]; then | |
mkdir -p $WEBBACKUPROOT | |
fi | |
# Reset log | |
_log_reset | |
_log "Backup job begin" | |
# First, check old files and delete all files older then $1 days | |
_cleanup 7 | |
# MySQL backup | |
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" | |
for DB in $DBS | |
do | |
if [ $DB != "information_schema" ] && [ $DB != "performance_schema" ] && [ $DB != "mysql" ]; then | |
DFILE="$MYSQLBACKUPROOT/$DB-$(_get_date_suffix).gz" | |
_log "Backup database $DB to $DFILE" | |
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $DB | $GZIP -9 > $DFILE | |
fi | |
done | |
_log "MySQL backup completed, directory size: $(_get_dir_size $MYSQLBACKUPROOT)" | |
# Webhosts ackup | |
for DIR in $WEBROOT/* | |
do | |
if [ -d $DIR ] && [ ${DIR##*/} != "html" ]; then | |
WFILE="$WEBBACKUPROOT/${DIR##*/}-$(_get_date_suffix).gz" | |
_log "Backup directory $DIR to $WFILE" | |
$TAR -zcf $WFILE $DIR | |
fi | |
done | |
_log "Webhosts backup completed, directory size: $(_get_dir_size $WEBBACKUPROOT)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment