diff --git a/backup.sh b/backup.sh index 6b85a9c..bb6c19a 100644 --- a/backup.sh +++ b/backup.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # === CONFIG === BCKDIR='/var/backups/mysql' @@ -59,37 +59,41 @@ backup() query="SHOW databases;" local skip=( - 'information_schema' - 'performance_schema' - 'mysql' + 'information_schema' + 'performance_schema' ) array_skip=( ${skip[@]} ${DATABASES_SKIP[@]} ) skip_reg=`array_join "${array_skip[@]}"` f_log "Skip databases: $skip_reg" - - for BDD in `mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | egrep -v "$skip_reg"`; do - f_log "* Processing BDD $BDD" + for BDD in $(mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | egrep -v "$skip_reg"); do mkdir -p $DST/$BDD 2>/dev/null 1>&2 chown mysql:mysql $DST/$BDD query="SHOW CREATE DATABASE \`$BDD\`;" - mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql - f_log " > Export create" + mysql --defaults-extra-file=$MYCNF --skip-column-names -B -e "$query" | \ + awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql + if [ -f $DST/$BDD/__create.sql ]; then + f_log " > Export create" + fi query="SHOW FULL TABLES WHERE Table_type = 'VIEW';" - for viewName in `mysql --defaults-extra-file=$MYCNF $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'` - do + for viewName in $(mysql --defaults-extra-file=$MYCNF $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do mysqldump --defaults-file=$MYCNF $BDD $viewName >> $DST/$BDD/__views.sql done - f_log " > Exports views" + if [ -f $DST/$BDD/__views.sql ]; then + f_log " > Exports views" + fi - mysqldump --defaults-file=$MYCNF --routines --no-create-info --no-data --no-create-db --skip-opt $BDD | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql - f_log " > Exports Routines" + mysqldump --defaults-file=$MYCNF --routines --no-create-info --no-data --no-create-db --skip-opt $BDD | \ + sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql + if [ -f $DST/$BDD/__routines.sql ]; then + f_log " > Exports Routines" + fi query="SHOW TABLES;" - for TABLE in `mysql --defaults-extra-file=$MYCNF --skip-column-names -B $BDD -e "$query" | grep -v slow_log | grep -v general_log`; do + for TABLE in $(mysql --defaults-extra-file=$MYCNF --skip-column-names -B $BDD -e "$query" | grep -v slow_log | grep -v general_log); do f_log " ** Dump $BDD.$TABLE" mysqldump --defaults-file=$MYCNF -T $DST/$BDD/ $BDD $TABLE @@ -104,6 +108,9 @@ backup() if [ -f "$DST/$BDD/$TABLE.txt" ]; then f_log " ** bzip2 $BDD/$TABLE.txt in background" + if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then + rm $DST/$BDD/$TABLE.txt.bz2 + fi bzip2 $DST/$BDD/$TABLE.txt & else f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found" @@ -116,15 +123,15 @@ backup() f_log "** END **" } -while getopts ":e:r:" opt; +while getopts ":e:r:s" opt; do case ${opt} in - e) + e) exclude=${OPTARG} IFS=, read -r -a DATABASES_SKIP <<< "$exclude" ;; - *) - usage + *) + usag exit 1 ;; esac diff --git a/restore.sh b/restore.sh index 7b9eeb4..7be407f 100644 --- a/restore.sh +++ b/restore.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/sh # === CONFIG === DIR_PWD=$(pwd) @@ -7,9 +7,19 @@ MYCNF='/etc/mysql/debian.cnf' MYDATA='/var/lib/mysql' # === FUNCTIONS === +database_exists() +{ + RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@` + if [ "$RESULT" == "$@" ]; then + echo YES + else + echo NO + fi +} + f_log() { - logger "$RESTORE: $@" + logger "RESTORE: $@" } usage() @@ -21,6 +31,7 @@ This script restore databases. OPTIONS: -e Exclude databases + -s Selected databases -c Check innochecksum of table after import EOF } @@ -44,14 +55,8 @@ restore() exit 1 fi - f_log "Create databases" - for i in $(ls -1 $DIR_PWD/*/__create.sql); do - if [ -f "$i" ]; then - mysql --defaults-extra-file=$MYCNF < $i 2>/dev/null - fi - done - - for i in `ls -1 -d $DIR_PWD/*/`; do + for i in $(ls -1 -d $DIR_PWD/*); do + BDD=$(basename $i) for skip in "${DATABASES_SKIP[@]}"; do @@ -62,79 +67,107 @@ restore() fi done - f_log "Import tables into $BDD" + for select in "${DATABASES_SELECTED[@]}"; do + if [ $BDD != $select ]; then + f_log "Skip database $BDD" + unset BDD + break + fi + done + + if [ $BDD ]; then - if [ $BDD ]; then - for TABLE in `ls -1 $i | grep -v __ | awk -F. '{print $1}' | sort | uniq`; do + if [ -f $DIR_PWD/$BDD/__create.sql ]; then + f_log "Create database $BDD" + mysql --defaults-extra-file=$MYCNF < $DIR_PWD/$BDD/__create.sql 2>/dev/null + fi - f_log "Create table: $TABLE" - mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0; - DROP TABLE IF EXISTS $TABLE; - SOURCE $DIR_PWD/$BDD/$TABLE.sql; - SET foreign_key_checks = 1;" - - if [ -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then - f_log "< $TABLE" - if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then - rm $DIR_PWD/$BDD/$TABLE.txt - fi - bunzip2 $DIR_PWD/$BDD/$TABLE.txt.bz2 - fi - - if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then - f_log "+ $TABLE" + if [ $(database_exists $BDD) != "YES" ]; then + f_log "Error: Database $BDD dose not exists"; + else + + tables=$(ls -1 $i | grep -v __ | awk -F. '{print $1}' | sort | uniq) + + f_log "Create tables in $BDD" + for TABLE in $tables; do + f_log "Create table: $BDD/$TABLE" mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0; - LOAD DATA INFILE '$DIR_PWD/$BDD/$TABLE.txt' - INTO TABLE $TABLE; + DROP TABLE IF EXISTS $TABLE; + SOURCE $DIR_PWD/$BDD/$TABLE.sql; SET foreign_key_checks = 1;" - - if [ ! -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then - f_log "> $TABLE" - bzip2 $DIR_PWD/$BDD/$TABLE.txt + done + + f_log "Import data into $BDD" + for TABLE in $tables; do + f_log "Import data into $BDD/$TABLE" + + if [ -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then + f_log "< $TABLE" + if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then + rm $DIR_PWD/$BDD/$TABLE.txt + fi + bunzip2 $DIR_PWD/$BDD/$TABLE.txt.bz2 fi - fi - - if [ $check ]; then - # Check INNODB table - if [ -f "$MYDATA/$BDD/$TABLE.ibd" ]; then - if [ ! `innochecksum $MYDATA/$BDD/$TABLE.ibd` ]; then - f_log "$TABLE [OK]" - else - f_log "$TABLE [ERR]" + + if [ -f "$DIR_PWD/$BDD/$TABLE.txt" ]; then + f_log "+ $TABLE" + mysql --defaults-extra-file=$MYCNF $BDD -e "SET foreign_key_checks = 0; + LOAD DATA INFILE '$DIR_PWD/$BDD/$TABLE.txt' + INTO TABLE $TABLE; + SET foreign_key_checks = 1;" + + if [ ! -f "$DIR_PWD/$BDD/$TABLE.txt.bz2" ]; then + f_log "> $TABLE" + bzip2 $DIR_PWD/$BDD/$TABLE.txt fi fi - fi - - done - - f_log "Import routines into $BDD" - if [ -f "$DIR_PWD/$BDD/__routines.sql" ]; then - mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__routines.sql 2>/dev/null + + if [ $DATABASES_TABLE_CHECK ]; then + # Check INNODB table + if [ -f "$MYDATA/$BDD/$TABLE.ibd" ]; then + if [ ! $(innochecksum $MYDATA/$BDD/$TABLE.ibd) ]; then + f_log "$TABLE [OK]" + else + f_log "$TABLE [ERR]" + fi + fi + fi + done + + if [ -f "$DIR_PWD/$BDD/__routines.sql" ]; then + f_log "Import routines into $BDD" + mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__routines.sql 2>/dev/null + fi + + if [ -f "$DIR_PWD/$BDD/__views.sql" ]; then + f_log "Import views into $BDD" + mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__views.sql 2>/dev/null + fi + fi - - f_log "Import views into $BDD" - if [ -f "$DIR_PWD/$BDD/__views.sql" ]; then - mysql --defaults-extra-file=$MYCNF $BDD < $DIR_PWD/$BDD/__views.sql 2>/dev/null - fi - fi done + f_log "Flush privileges;" mysql --defaults-extra-file=$MYCNF -e "flush privileges;" f_log "** END **" } # === EXECUTE === -while getopts ":e:c:" opt; +while getopts ":e:c:s:" opt; do case ${opt} in e) exclude=${OPTARG} IFS=, read -r -a DATABASES_SKIP <<< "$exclude" ;; + s) + selected=${OPTARG} + IFS=, read -r -a DATABASES_SELECTED <<< "$selected" + ;; c) - check=1 + DATABASES_TABLE_CHECK=1 ;; *) usage @@ -142,8 +175,7 @@ do ;; esac done - -shift "$((OPTIND - 1))" +#shift "$((OPTIND - 1))" # === AUTORUN === restore \ No newline at end of file