MysqlBackupAll

From campisano.org
Jump to navigation Jump to search

Questo script fa un backup completo di MySQL e lo invia all'email configurato. E' possibile restaurare il backup con le istruzioni dell'articolo MySQL.


Requirements

Il percorso /srv/backup/mysql/alldatabases/ deve esistere e essere di proprietà dell'utente mysql, e i comandi mail, mysqldump, gzip, ftp e mutt devono essere accessibili.


#!/bin/bash
#
# NAME      mysqlBackupAll.sh
# VERSION   1.1
# REQUIRED  mysqldump, mail, mutt, gzip, ftp
#



# Var

BACKUP_DIR="/srv/backup/mysql/alldatabases/";
BACKUP_FILE="bkp-mysql_alldatabases-`date +%Y.%m.%d_%H.%M.%S`-dump.sql.gz";
BACKUP_COMMAND="mysqldump --user=root --single-transaction --all-databases --flush-logs --lock-tables --flush-privileges | gzip > ${BACKUP_DIR}${BACKUP_FILE}";
BACKUP_USER="root";
ROTATE_USER="mysql";
ROTATE_DAYS="8";

SEND_BY_FTP="y";
FTP_HOST="[DESTDOMSIN.WHR]";
FTP_DIR="mysql/alldatabases";
FTP_USER="[USER]";
FTP_PASS="[PASS]";

SEND_BY_EMAIL="n";
EMAIL="[WHO@MAILSERVER.WHR]";
SUBJECT="[YOURDOMAIN.WHR] MySQL backup";



# Test

if test ! `which mail`
then
   echo "Command mail not found, exit.";
   exit 1;
fi;

if test ! `which mysqldump`
then
   echo "Command mysqldump not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if test ! `which gzip`
then
   echo "Command gzip not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if test ! `which ftp`
then
   echo "Command ftp not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if test ! `which mutt`
then
   echo "Command mutt not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if ! test -d "${BACKUP_DIR}";
then
   echo "Directory ${BACKUP_DIR} don't exist, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if test -f "${BACKUP_DIR}${BACKUP_FILE}";
then
    echo "File ${BACKUP_DIR}${BACKUP_FILE} alredy exist, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
    exit 1;
fi;

if ! su - "${BACKUP_USER}" -c "touch ${BACKUP_DIR}${BACKUP_FILE}";
then
    echo "User ${BACKUP_USER} cannot write in ${BACKUP_DIR}${BACKUP_FILE}, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
    exit 1;
fi;

su - "${BACKUP_USER}" -c "rm -f ${BACKUP_DIR}${BACKUP_FILE}";



# Backup

if ! su - "${BACKUP_USER}" -s /bin/sh -c "${BACKUP_COMMAND}";
then
    echo "Backup ${BACKUP_COMMAND} failure, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
    exit 1;
fi;

chown "${ROTATE_USER}":"${ROTATE_USER}" "${BACKUP_DIR}${BACKUP_FILE}";



# Rotate

if ! su - "${ROTATE_USER}" -s /bin/sh -c "ls -t ${BACKUP_DIR}*-dump.sql.gz | tail -n +${ROTATE_DAYS} | xargs -d '\n' rm -f";
then
    echo "Rotate ${BACKUP_DIR} failure, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
    exit 1;
fi;



# Upload

if test ${SEND_BY_FTP} = "y";
then
    ftp -i -n ${FTP_HOST} << EOF
user ${FTP_USER} ${FTP_PASS}
binary
put ${BACKUP_DIR}${BACKUP_FILE} ${FTP_DIR}${BACKUP_FILE}
quit
EOF

    if test $? -ne 0;
    then
        echo "Upload to ${FTP_HOST} failure, exit." | mail -s "${SUBJECT} error." ${EMAIL};
        exit 1;
    fi;
fi;



# Exit

if test ${SEND_BY_EMAIL} = "y";
then
    ls -l "${BACKUP_DIR}"  | mutt -n -e 'set copy=no' -s "${SUBJECT} success." -a "${BACKUP_DIR}${BACKUP_FILE}" -- "${EMAIL}";
    exit 0;
else
    ls -l "${BACKUP_DIR}"  | mail -s "${SUBJECT} success." "${EMAIL}";
fi;



# End