Script de respaldos de DBs

Daniel Eugenin M. -

Un pequeño script para respaldas las bases de datos en forma separada.

 

1. Crear un usuario de MySQL exclusivo para respaldo

grant SHOW DATABASES on *.* to 'backup'@'localhost' identified by 'PASSDEUSUARIOBACKUP';
grant SELECT on *.* to 'backup'@'localhost' identified by 'PASSDEUSUARIOBACKUP';
grant LOCK TABLES on *.* to 'backup'@'localhost' identified by 'PASSDEUSUARIOBACKUP';
grant RELOAD on *.* to 'backup'@'localhost' identified by 'PASSDEUSUARIOBACKUP';
grant SHOW VIEW on *.* to 'backup'@'localhost' identified by 'PASSDEUSUARIOBACKUP';
show grants for 'backup'@'localhost';
flush privileges;

 

2. Script: /usr/local/sbin/respalda_dbs.sh

 

#!/bin/bash

TIMESTAMP=$(date +"%F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL_PASSWORD="PASSDEUSUARIOBACKUP"
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump

mkdir -p "$BACKUP_DIR/mysql"

databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`

for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done

# Delete old backups (3 days)
find /backup -type d -mtime +2 -exec rm -rf {} \; > /dev/null 2>&1

 

3. Cron de respaldo diario: /etc/cron.daily/respalda

#!/bin/bash

/usr/local/sbin/respalda_dbs.sh

0 Comentarios

Inicie sesión para dejar un comentario.
Tecnología de Zendesk