Script Backup MySQL

#!/bin/bash
# Script de Backup MySQL

DIR_BACKUP='/backup_sql'
MYSQL_USER='root'
MYSQL_PASSWD='toor'

DATABASES=$( mysql -u $MYSQL_USER -p$MYSQL_PASSWD -e 'SHOW DATABASES' | tr -d '' | grep -v Database )

DATA=$( date +%F )

for i in ${DATABASES[*]}; do
  if [ $( date +%d ) == 01 ]; then
    echo "[ $DATA ] Backup Mensal e Diario - Database: ( $i )"
    mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD --databases $i --single-transaction > $DIR_BACKUP'/mensal/'$i-$DATA'-mensal.sql'
    cp $DIR_BACKUP'/mensal/'$i-$DATA'-mensal.sql' '/backup_sql/diario/'$i-$DATA'-diario.sql'
  elif [ $( date +%u ) == 7 ]; then
    echo "[ $DATA ] Backup Semanal e Diario - Database: ( $i )"
    mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD --databases $i --single-transaction > $DIR_BACKUP'/semanal/'$i-$DATA'-semanal.sql'
    cp $DIR_BACKUP'/semanal/'$i-$DATA'-semanal.sql' '/backup_sql/diario/'$i-$DATA'-diario.sql'
  else
    echo "[ $DATA ] Backup Diario - Database: ( $i )"
    mysqldump -u $MYSQL_USER -p$MYSQL_PASSWD --databases $i --single-transaction > $DIR_BACKUP'/diario/'$i-$DATA'-diario.sql'
  fi
done

# Rotacao dos arquivos de backup
find $DIR_BACKUP/diario/* -mtime +6 -delete
find $DIR_BACKUP/semanal/* -mtime +28 -delete
find $DIR_BACKUP/mensal/* -mtime +31 -delete
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s