MySQL Datenbank per Skript sichern
Das folgende Skript sichert eine oder mehrer Datenbanken mittels mysqldump als tar.gz-Dateien. Beachten Sie, dass der Datenbank-Benutzer für das Backup-Skript die folgenden Rechte benötigt: SHOW DATABASES, SELECT, LOCK TABLES, RELOAD.
#!/bin/sh
# configure databases
CONF_DB_DATABASE[0]="db1"
CONF_DB_DATABASE[1]="db2"
CONF_DB_DATABASE[2]="db3"
# settings
CONF_DB_USER="backup"
CONF_DB_PASSWORD="password"
CONF_DB_HOST="localhost"
CONF_OUTPUT_FILENAME="_`date +%d%m%Y`"
CONF_OUTPUT_DIR="/mnt/backup/mysqldump/"
CONF_OUTPUT_DIR_CLEAN="true"
CONF_TMP_DIR="/tmp/"
# clean up output dir
if [ $CONF_OUTPUT_DIR_CLEAN = "true" ]
then rm $CONF_OUTPUT_DIR*.tar.gz
fi
# backup databases
for db in "${CONF_DB_DATABASE[@]}";
do
mysqldump -u $CONF_DB_USER -p$CONF_DB_PASSWORD -h $CONF_DB_HOST $db > $CONF_TMP_DIR$db$CONF_OUTPUT_FILENAME.sql
tar cfz $CONF_OUTPUT_DIR$db$CONF_OUTPUT_FILENAME.tar.gz -C $CONF_TMP_DIR $db$CONF_OUTPUT_FILENAME.sql >> /dev/null 2>&1
rm $CONF_TMP_DIR$db$CONF_OUTPUT_FILENAME.sql
done