20 3 * * * /var/www/$USER$/files/backup.sh >& /dev/null
#!/bin/bash
#########################################################
# #
# confixx backup #
# #
#########################################################
# ------------------- variables -------------------
HOMEDIR="$(pwd)"
FILESDIR="$HOMEDIR/files"
LOGFILE="$FILESDIR/backup.log"
DATESTAMP="$(date "+%F %T")"
MySQL_USER="..."
MySQL_PASS="..."
MySQL_HOST="localhost"
MySQL_ignoredb="information_schema"
# ------------------ append log file ----------------
touch $LOGFILE
echo "$DATESTAMP starting backup" >> $LOGFILE
# ----------------- remove old backup --------------
mkdir $FILESDIR/backup
cd $FILESDIR/backup
rm *
mkdir $FILESDIR/backup_tmp
cd $FILESDIR/backup_tmp
rm *
# ------------------- list databases ---------------
DBS="$(mysql -u $MySQL_USER -h $MySQL_HOST -p$MySQL_PASS -Bse 'show databases')"
echo "$DATESTAMP -> list databases" >> $LOGFILE
echo "$DBS" > $FILESDIR/backup_tmp/databases.list
# ------------------- dump databases ---------------
echo "$DATESTAMP -> dumping databases" >> $LOGFILE
for db in $DBS
do
if [ "$db" != "$MySQL_ignoredb" ];
then
echo "$DATESTAMP -> dumping database: $db" >> $LOGFILE
mysqldump --add-drop-table --allow-keywords -q -c -u $MySQL_USER -h $MySQL_HOST -p$MySQL_PASS $db > $FILESDIR/backup_tmp/mysql-$db.sql
fi
done
# ------------ compressing database dumps ----------
echo "$DATESTAMP -> compressing database dumps" >> $LOGFILE
tar -czf $FILESDIR/backup/mysql-$(date +%F).tar.gz -C $FILESDIR/backup_tmp/ *
# ------------------- delete dumps ----------------
echo "$DATESTAMP -> deleting database dumps" >> $LOGFILE
mkdir $FILESDIR/backup_tmp
cd $FILESDIR/backup_tmp
rm *
# -------------- compressing html folder ----------
echo "$DATESTAMP -> compressing html files" >> $LOGFILE
cd $HOMEDIR/html
tar -czf $FILESDIR/backup/html-$(date +%F).tar.gz *
# --------------- final logfile comment ----------
echo "$DATESTAMP backup finished" >> $LOGFILE
echo "" >> $LOGFILE