2015-07-14 00:30:49 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2017-05-17 13:00:51 +02:00
|
|
|
#echo "`date` mysql DUMP temporarly excluded (by Tom)"
|
|
|
|
#exit 1
|
|
|
|
|
2015-07-14 00:30:49 +02:00
|
|
|
RETVAL=0
|
|
|
|
|
|
|
|
MY_BACKUP_USE_NAGIOS="False"
|
|
|
|
MY_BACKUP_DIR=/var/lib/mysql-backup
|
|
|
|
MY_DATA_DIR=/var/lib/mysql
|
|
|
|
N_DAYS_TO_SPARE=7
|
2015-08-05 19:14:03 +02:00
|
|
|
# Exclude list
|
|
|
|
EXCLUDE_LIST='performance_schema'
|
2015-07-14 00:30:49 +02:00
|
|
|
|
|
|
|
if [ -f /etc/default/mysql_backup ] ; then
|
|
|
|
. /etc/default/mysql_backup
|
|
|
|
fi
|
2015-05-29 19:42:43 +02:00
|
|
|
|
|
|
|
if [ ! -f /root/.my.cnf ] ; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
umask 0077
|
2015-07-14 00:30:49 +02:00
|
|
|
# Year month day - hour minute second
|
|
|
|
SAVE_TIME=$( date +%Y%m%d-%H%M%S )
|
|
|
|
TIMESTAMP=
|
|
|
|
TIMESTAMP_LOG=$MY_BACKUP_DIR/.timestamp
|
2015-05-29 19:42:43 +02:00
|
|
|
|
|
|
|
if [ ! -d $MY_BACKUP_DIR ] ; then
|
|
|
|
mkdir -p $MY_BACKUP_DIR
|
|
|
|
fi
|
2015-07-14 00:30:49 +02:00
|
|
|
if [ ! -d $MY_BACKUP_LOG_DIR ] ; then
|
|
|
|
mkdir -p $MY_BACKUP_LOG_DIR
|
2015-05-29 19:42:43 +02:00
|
|
|
fi
|
2015-07-14 00:30:49 +02:00
|
|
|
if [ ! -d $MY_BACKUP_DIR/history ] ; then
|
|
|
|
mkdir -p $MY_BACKUP_DIR/history
|
2015-05-29 19:42:43 +02:00
|
|
|
fi
|
2015-07-20 15:25:33 +02:00
|
|
|
chmod 700 $MY_BACKUP_DIR
|
2015-07-14 00:30:49 +02:00
|
|
|
LOCKFILE=$MY_DATA_DIR/.mysqldump.lock
|
|
|
|
NAGIOS_LOG=$MY_BACKUP_DIR/.nagios-status
|
2015-05-29 19:42:43 +02:00
|
|
|
|
|
|
|
if [ ! -f $LOCKFILE ] ; then
|
|
|
|
touch $LOCKFILE
|
2015-07-14 00:30:49 +02:00
|
|
|
if [ "${MY_BACKUP_USE_NAGIOS}" == "True" ] ; then
|
|
|
|
> $NAGIOS_LOG
|
|
|
|
fi
|
2017-05-17 13:00:51 +02:00
|
|
|
for db in $( mysql -Bse "show databases;" | egrep -v $EXCLUDE_LIST ) ; do
|
|
|
|
if [ "$db" == "information_schema" ]; then
|
|
|
|
mysqldump --single-transaction -f --flush-privileges --opt $db > $MY_BACKUP_DIR/history/${db}.sql.${SAVE_TIME} 2> $MY_BACKUP_LOG_DIR/$db.log
|
|
|
|
DUMP_RESULT=$?
|
|
|
|
else
|
|
|
|
mysqldump -f --flush-privileges --opt $db > $MY_BACKUP_DIR/history/${db}.sql.${SAVE_TIME} 2> $MY_BACKUP_LOG_DIR/$db.log
|
|
|
|
DUMP_RESULT=$?
|
|
|
|
fi
|
2015-08-05 19:14:03 +02:00
|
|
|
chmod 600 $MY_BACKUP_DIR/history/${db}.sql.${SAVE_TIME}
|
|
|
|
if [ "${MY_BACKUP_USE_NAGIOS}" == "True" ] ; then
|
|
|
|
if [ $DUMP_RESULT -ne 0 ] ; then
|
|
|
|
echo "$db:FAILED" >> $NAGIOS_LOG
|
|
|
|
RETVAL=$DUMP_RESULT
|
|
|
|
else
|
|
|
|
echo "$db:OK" >> $NAGIOS_LOG
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
pushd ${MY_BACKUP_DIR}/ >/dev/null 2>&1
|
|
|
|
rm -f $db.sql
|
|
|
|
ln -s $MY_BACKUP_DIR/history/${db}.sql.${SAVE_TIME} ./$db.sql
|
|
|
|
popd >/dev/null 2>&1
|
2015-05-29 19:42:43 +02:00
|
|
|
done
|
|
|
|
# Do a "flush-hosts" after the backup
|
2015-07-14 00:30:49 +02:00
|
|
|
mysqladmin flush-hosts 2> $MY_BACKUP_LOG_DIR/flush-hosts.log
|
|
|
|
TIMESTAMP=$( date +%s )
|
|
|
|
echo "$TIMESTAMP" > $TIMESTAMP_LOG
|
2015-05-29 19:42:43 +02:00
|
|
|
rm -f $LOCKFILE
|
2015-07-14 00:30:49 +02:00
|
|
|
else
|
2016-01-11 11:00:55 +01:00
|
|
|
echo "Old backup still running" > /var/log/mysql-backup.log
|
2015-07-14 00:30:49 +02:00
|
|
|
RETVAL=2
|
|
|
|
if [ "${MY_BACKUP_USE_NAGIOS}" == "True" ] ; then
|
|
|
|
echo "old backup still running:WARNING" >> $NAGIOS_LOG
|
|
|
|
fi
|
2015-05-29 19:42:43 +02:00
|
|
|
fi
|
|
|
|
|
2015-07-14 00:30:49 +02:00
|
|
|
# Remove the old backups
|
|
|
|
find ${MY_BACKUP_DIR}/history -ctime +$N_DAYS_TO_SPARE -exec rm -f {} \;
|
|
|
|
|
|
|
|
exit $RETVAL
|