2015-07-14 00:30:49 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
chmod -R 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
|
|
|
|
# Exclude list
|
|
|
|
EXCLUDE_LIST='performance_schema'
|
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
|
|
|
|
for db in $( /bin/ls -1 /var/lib/mysql/ | grep -v $EXCLUDE_LIST ) ; do
|
|
|
|
if [ -d /var/lib/mysql/$db ] ; then
|
2015-05-29 19:42:43 +02:00
|
|
|
if [ -f $MY_BACKUP_DIR/$db.sql ] ; then
|
2015-07-14 00:30:49 +02:00
|
|
|
mv -f $MY_BACKUP_DIR/$db.sql $MY_BACKUP_DIR/history
|
2015-05-29 19:42:43 +02:00
|
|
|
fi
|
|
|
|
#mysqldump -uroot -f --opt -p$MYSQLPASS $db > $MY_BACKUP_DIR/$db.sql 2> $MY_BACKUP_DIR/log/$db.log
|
2015-07-14 00:30:49 +02:00
|
|
|
mysqldump -f --opt $db > $MY_BACKUP_DIR/history/${db}.sql.${SAVE_TIME} 2> $MY_BACKUP_LOG_DIR/$db.log
|
|
|
|
DUMP_RESULT=$?
|
|
|
|
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
|
|
|
|
fi
|
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
|
|
|
|
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
|