Sauvegarde de base mysql

19-Dec-2007 | scripts

Ce script nécessite l’installation des programmes :

  • bzip2
  • mysqldump
  • mail

Voici le script :

#!/bin/bash

# Variables
TARGET='/data/backup/'
MAIL_ADDRESS='bidule@machin.com'
MAIL_LOG='/data/backup/mail_log.txt'
a=$(date +%y)
m=$(date +%m)
d=$(date +%d)
h=$(date +%H)
M=$(date +%M)

# Backuping the database
echo -e $(date +%D)"-"$(date +%T)"--> START of the database backup" > $MAIL_LOG
mysqldump --add-drop-table -h localhost -u user -ppassword database | bzip2 -c \
> "$TARGET"backup_database_$a$m$d$h$M.sql.bz2
echo -e "..." >> $MAIL_LOG
echo -e $(date +%D)"-"$(date +%T)"--> END of the database backup" >> $MAIL_LOG
if [ -r "$TARGET"backup_database_$a$m$d$h$M.sql.bz2 ]; then
    SUBJECT="[Database backup OK - "$(date +%D)"-"$(date +%T)"]"
    mail -s "$SUBJECT" $MAIL_ADDRESS < $MAIL_LOG
else
    SUBJECT="[Database backup ERROR - "$(date +%D)"-"$(date +%T)"]"
    echo -e ""
    echo -e ""
    echo -e "WARNING ! There was an error while backuping the database" >> $MAIL_LOG
    mail -s "$SUBJECT" $MAIL_ADDRESS < $MAIL_LOG
fi
# EOF