Skip to content

Commit

Permalink
Реализовано сохранение дампа таблиц БД в репозиторий
Browse files Browse the repository at this point in the history
Реализована восстановление БД за указанную в хранилище дату
Реализован пропуск сохраняемы и восстанавливаемых БД по ключу -e
  • Loading branch information
Mirocow committed Jun 11, 2013
1 parent 1a98233 commit 0147bdb
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 0 deletions.
136 changes: 136 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bash

# === CONFIG ===
BCKDIR='/var/backups/mysql'
MYCNF='/etc/mysql/debian.cnf'

BIN_DEPS='bzip2 mysql mysqldump'
DATE=$(date '+%Y.%m.%d')
DATEOLD=$(date --date='1 week ago' +%Y.%m.%d)
DST=$BCKDIR/$DATE
DSTOLD=$BCKDIR/$DATEOLD

# === CHECKS ===
for BIN in $BIN_DEPS; do
which $BIN 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: Required file could not be found: $BIN"
exit 1
fi
done


if [ ! -d "$DST" ]; then mkdir -p $DST; fi
if [ -d "$DSTOLD" ]; then rm -fr $DSTOLD; fi

# === FUNCTION ===
f_log() {
logger "BACKUP: $@"
}

usage()
{
cat << EOF
usage: $0 options
This script buckup all databases.
OPTIONS:
-e Exclude databases
EOF
}

array_join()
{
local array_skip=("${@}")
for skip in "${array_skip[@]}"; do
if [ -x $return ]; then
local return="^$skip\$"
else
return="$return|^$skip\$"
fi
done
echo ${return}
}

backup()
{
f_log "** START **"

query="SHOW databases;"
local skip=(
'information_schema'
'performance_schema'
'mysql'
)
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"

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"

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
mysqldump --defaults-file=$MYCNF $BDD $viewName >> $DST/$BDD/__views.sql
done
f_log " > Exports views"

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"

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
f_log " ** Dump $BDD.$TABLE"

mysqldump --defaults-file=$MYCNF -T $DST/$BDD/ $BDD $TABLE

if [ -f "$DST/$BDD/$TABLE.sql" ]; then
chmod 750 $DST/$BDD/$TABLE.sql
chown root:root $DST/$BDD/$TABLE.sql
f_log " ** set perm on $BDD/$TABLE.sql"
else
f_log " ** WARNING : $DST/$BDD/$TABLE.sql not found"
fi

if [ -f "$DST/$BDD/$TABLE.txt" ]; then
f_log " ** bzip2 $BDD/$TABLE.txt in background"
bzip2 $DST/$BDD/$TABLE.txt &
else
f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found"
fi

done

done

f_log "** END **"
}

while getopts ":e:r:" opt;
do
case ${opt} in
e)
exclude=${OPTARG}
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
;;
*)
usage
exit 1
;;
esac
done
#shift "$((OPTIND - 1))"

# === AUTORUN ===
backup

149 changes: 149 additions & 0 deletions restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env bash

# === CONFIG ===
DIR_PWD=$(pwd)
BIN_DEPS='bunzip2 bzip2 mysql'
MYCNF='/etc/mysql/debian.cnf'
MYDATA='/var/lib/mysql'

# === FUNCTIONS ===
f_log()
{
logger "$RESTORE: $@"
}

usage()
{
cat << EOF
usage: $0 options
This script restore databases.
OPTIONS:
-e Exclude databases
-c Check innochecksum of table after import
EOF
}

restore()
{
f_log "** START **"

f_log "Check runtime"
for BIN in $BIN_DEPS; do
which $BIN 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
f_log "Error: Required file could not be found: $BIN"
exit 1
fi
done

f_log "Check backups folder"
if [ "$(ls -1 $DIR_PWD/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
f_log "Your must run script from backup directory"
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
BDD=$(basename $i)

for skip in "${DATABASES_SKIP[@]}"; do
if [ $BDD = $skip ]; then
f_log "Skip database $BDD"
unset BDD
break
fi
done

f_log "Import tables into $BDD"

if [ $BDD ]; then
for TABLE in `ls -1 $i | grep -v __ | awk -F. '{print $1}' | sort | uniq`; do

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"
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

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]"
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
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

mysql --defaults-extra-file=$MYCNF -e "flush privileges;"

f_log "** END **"
}

# === EXECUTE ===
while getopts ":e:c:" opt;
do
case ${opt} in
e)
exclude=${OPTARG}
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
;;
c)
check=1
;;
*)
usage
exit 1
;;
esac
done

shift "$((OPTIND - 1))"

# === AUTORUN ===
restore

0 comments on commit 0147bdb

Please sign in to comment.