Add config file
Merge backup_db.sh in backup.sh Merge restore_db.sh in restore.sh Update README
This commit is contained in:
parent
cf65af4066
commit
4f3dcf663a
|
@ -4,7 +4,8 @@
|
||||||
## (unreleased)
|
## (unreleased)
|
||||||
|
|
||||||
### Add
|
### Add
|
||||||
* Add changelog and config file for [gitchangelog](https://github.com/vaab/gitchangelog) (commit 6160c99 by Knah Tsaeb)
|
* Add config file Merge backup_db.sh in backup.sh Merge restore_db.sh in restore.sh Update README. (commit 7cd8f65 by Knah Tsaeb)
|
||||||
|
* Add changelog and config file for [gitchangelog](https://github.com/vaab/gitchangelog) (commit cf65af4 by Knah Tsaeb)
|
||||||
|
|
||||||
|
|
||||||
## 0.0.6 (2019-02-21)
|
## 0.0.6 (2019-02-21)
|
||||||
|
|
52
README.md
52
README.md
|
@ -1,46 +1,59 @@
|
||||||
mysql_utils for Debian or Other OS
|
DiBaReMy for Arch or Other OS
|
||||||
=======================================
|
=======================================
|
||||||
|
|
||||||
Russian documentation: http://docs.mirocow.com/doku.php?id=mysql:mysql_utils
|
DiBaReMy is a fork of [mysql_utils](https://github.com/Mirocow/mysql_utils) by [Mirocow](https://github.com/Mirocow).
|
||||||
|
|
||||||
Backup and Restore data from MySql tables
|
Backup and Restore data from MySql/MariaDB tables.
|
||||||
|
|
||||||
Install
|
Install
|
||||||
======
|
======
|
||||||
|
|
||||||
cd ~
|
git clone https://forge.leslibres.org/Knah-Tsaeb/DiBaReMy.git
|
||||||
git clone https://github.com/Mirocow/mysql_utils.git
|
cd DiBaReMy
|
||||||
cd mysql_utils
|
|
||||||
|
|
||||||
|
Configuration
|
||||||
|
======
|
||||||
|
|
||||||
|
Edit config for DiBaReMy.
|
||||||
|
|
||||||
|
cp etc/config.cfg.example etc/config.cfg
|
||||||
|
nano etc/config.cfg
|
||||||
|
|
||||||
|
Edit config for mySQL/MariaDb conection.
|
||||||
|
|
||||||
|
cp etc/mysql/debian.cnf.example etc/mysql/local.cnf
|
||||||
|
nano etc/mysql/local.cnf
|
||||||
|
|
||||||
|
|
||||||
Backup all databases
|
Backup all databases
|
||||||
======
|
======
|
||||||
|
|
||||||
cd ~
|
sh backup.sh
|
||||||
cd mysql_utils
|
|
||||||
bash backup.sh
|
|
||||||
|
|
||||||
Backup selected database
|
Backup selected database
|
||||||
======
|
======
|
||||||
|
|
||||||
cd ~
|
sh backup.sh -d=[database-name]
|
||||||
cd mysql_utils
|
|
||||||
bash backup_db.sh <[database-name]>
|
|
||||||
|
|
||||||
Restore for selected date
|
Restore for selected date
|
||||||
=======
|
=======
|
||||||
|
|
||||||
cd /var/backups/mysql/[some date]
|
sh backups/[yyyy.mm.dd]/restore.sh
|
||||||
bash ~/mysql_utils/restore.sh
|
|
||||||
|
|
||||||
Restore selected DB
|
Restore selected DB
|
||||||
=======
|
=======
|
||||||
|
|
||||||
cd /var/backups/mysql/[some date]/[some db name]
|
sh backups/[yyyy.mm.dd]/restore.sh -d=[database-name]
|
||||||
bash ~/mysql_utils/restore_db.sh
|
|
||||||
|
|
||||||
Automation backup with Cron
|
Automation backup with Cron
|
||||||
===========================
|
===========================
|
||||||
|
|
||||||
|
|
||||||
nano /etc/default/db_backup
|
nano /etc/default/db_backup
|
||||||
|
|
||||||
START=yes
|
START=yes
|
||||||
|
@ -74,9 +87,7 @@ Check work
|
||||||
Tested on
|
Tested on
|
||||||
==========
|
==========
|
||||||
|
|
||||||
Debiad
|
Arch Linux
|
||||||
FreeBsd
|
|
||||||
Ubuntu
|
|
||||||
|
|
||||||
Help
|
Help
|
||||||
===========
|
===========
|
||||||
|
@ -85,11 +96,12 @@ Help
|
||||||
# bash backup.sh --help
|
# bash backup.sh --help
|
||||||
usage: backup.sh options
|
usage: backup.sh options
|
||||||
|
|
||||||
This script buckup all databases.
|
This script backup all databases.
|
||||||
|
|
||||||
Usage: backup.sh <[options]>
|
Usage: backup.sh <[options]>
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-d= | --database Database name
|
||||||
-e= | --exclude= Exclude databases
|
-e= | --exclude= Exclude databases
|
||||||
--exclude-tables= Exclude tables
|
--exclude-tables= Exclude tables
|
||||||
-c= | --compress= Compress with gzip or bzip2
|
-c= | --compress= Compress with gzip or bzip2
|
||||||
|
|
364
backup.sh
364
backup.sh
|
@ -1,15 +1,16 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# === CONFIG ===
|
if test -f etc/config.cfg ; then
|
||||||
VERBOSE=0
|
. etc/config.cfg
|
||||||
COMPRESS='bzip2'
|
else
|
||||||
USER='mysql'
|
printf "\n You need edit etc/config.cfg.example and save it to etc/config.cfg \n"
|
||||||
GROUP='mysql'
|
exit
|
||||||
DIRECTORYATTRIBUTES=0770
|
fi
|
||||||
FILEATTRIBUTES=640
|
|
||||||
TIME_REMOVED_DUMP_FILES='1 week ago'
|
if test ! -f $CONFIG_FILE ; then
|
||||||
BACKUP_DIR='/var/backups/mysql'
|
printf "\n You need edit etc/mysql/debian.cnf.example and save it to $CONFIG_FILE \n"
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
# === DO NOT EDIT BELOW THIS LINE ===
|
# === DO NOT EDIT BELOW THIS LINE ===
|
||||||
|
|
||||||
|
@ -23,10 +24,10 @@ f_log()
|
||||||
local red=$(tput setf 4)
|
local red=$(tput setf 4)
|
||||||
local green=$(tput setf 2)
|
local green=$(tput setf 2)
|
||||||
local reset=$(tput sgr0)
|
local reset=$(tput sgr0)
|
||||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
||||||
|
|
||||||
logger "BACKUP: $@"
|
logger "BACKUP: $@"
|
||||||
|
|
||||||
if [ $VERBOSE -eq 1 ]; then
|
if [ $VERBOSE -eq 1 ]; then
|
||||||
echo "BACKUP: $@"
|
echo "BACKUP: $@"
|
||||||
fi
|
fi
|
||||||
|
@ -48,132 +49,250 @@ prepaire_skip_expression()
|
||||||
backup()
|
backup()
|
||||||
{
|
{
|
||||||
f_log " START "
|
f_log " START "
|
||||||
|
|
||||||
query="SHOW databases;"
|
query="SHOW databases;"
|
||||||
|
|
||||||
local default_databases_exclude=(
|
local default_databases_exclude=(
|
||||||
'information_schema'
|
'information_schema'
|
||||||
'performance_schema'
|
'performance_schema'
|
||||||
)
|
)
|
||||||
|
|
||||||
local array_views=()
|
local array_views=()
|
||||||
|
|
||||||
database_exclude=( ${default_databases_exclude[@]} ${EXCLUDE_DATABASES[@]} )
|
if [ -z ${DATABASE} ]; then
|
||||||
database_exclude_expression=`prepaire_skip_expression "${database_exclude[@]}"`
|
database_exclude=( ${default_databases_exclude[@]} ${EXCLUDE_DATABASES[@]} )
|
||||||
f_log "Exclude databases: $database_exclude_expression"
|
database_exclude_expression=`prepaire_skip_expression "${database_exclude[@]}"`
|
||||||
|
f_log "Exclude databases: $database_exclude_expression"
|
||||||
for BDD in $(mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | egrep -v "$database_exclude_expression"); do
|
|
||||||
|
for BDD in $(mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | egrep -v "$database_exclude_expression"); do
|
||||||
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
|
||||||
chown $USER:$GROUP $DST/$BDD
|
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
||||||
chmod $DIRECTORYATTRIBUTES $DST/$BDD
|
chown $USER $DST/$BDD
|
||||||
touch $DST/$BDD/error.log
|
chmod $DIRECTORYATTRIBUTES $DST/$BDD
|
||||||
|
touch $DST/$BDD/_error.log
|
||||||
query="SHOW CREATE DATABASE \`$BDD\`;"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql
|
query="SHOW CREATE DATABASE \`$BDD\`;"
|
||||||
if [ -f $DST/$BDD/__create.sql ]; then
|
mysql --defaults-file=$CONFIG_FILE --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-file=$CONFIG_FILE $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||||
|
mysqldump --defaults-file=$CONFIG_FILE $BDD $viewName >> $DST/$BDD/__views.sql 2>> $DST/$BDD/_error.log
|
||||||
|
array_views+=($viewName)
|
||||||
|
done
|
||||||
|
if [ -f $DST/$BDD/__views.sql ]; then
|
||||||
|
f_log " > Exports views"
|
||||||
|
fi
|
||||||
|
|
||||||
|
mysqldump --defaults-file=$CONFIG_FILE --routines --no-create-info --no-data --no-create-db --skip-opt $BDD 2>> $DST/$BDD/_error.log | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql
|
||||||
|
if [ -f $DST/$BDD/__routines.sql ]; then
|
||||||
|
f_log " > Exports Routines"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local default_tables_exclude=(
|
||||||
|
'slow_log'
|
||||||
|
'general_log'
|
||||||
|
)
|
||||||
|
|
||||||
|
tables_exclude=( ${default_tables_exclude[@]} ${array_views[@]} ${EXCLUDE_TABLES[@]} )
|
||||||
|
tables_exclude_expression=`prepaire_skip_expression "${tables_exclude[@]}"`
|
||||||
|
f_log "Exclude tables: $tables_exclude_expression"
|
||||||
|
|
||||||
|
data_tables_exclude=( ${EXCLUDE_DATA_TABLES[@]} )
|
||||||
|
data_tables_exclude_expression=`prepaire_skip_expression "${data_tables_exclude[@]}"`
|
||||||
|
f_log "Exclude data tables: $data_tables_exclude_expression"
|
||||||
|
|
||||||
|
query="SHOW TABLES;"
|
||||||
|
for TABLE in $(mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $BDD -e "$query" | egrep -v "$tables_exclude_expression"); do
|
||||||
|
f_log " ** Dump $BDD.$TABLE"
|
||||||
|
|
||||||
|
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
|
||||||
|
f_log "Exclude data from table $TABLE"
|
||||||
|
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/_error.log
|
||||||
|
else
|
||||||
|
# If fields has geospatial types
|
||||||
|
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $BDD -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
|
||||||
|
hasGeo=$(eval $checkGeo)
|
||||||
|
if [ ! -z "$hasGeo" ]; then
|
||||||
|
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --result-file=$DST/$BDD/$TABLE.sql $BDD $TABLE 2>> $DST/$BDD/_error.log
|
||||||
|
else
|
||||||
|
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/_error.log
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "$DST/$BDD/$TABLE.sql" ]; then
|
||||||
|
chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.sql
|
||||||
|
chown $USER $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
|
||||||
|
|
||||||
|
if [ $COMPRESS ]; then
|
||||||
|
|
||||||
|
f_log " ** $COMPRESS $BDD/$TABLE.txt in background"
|
||||||
|
|
||||||
|
if [ $COMPRESS == 'bzip2' ]; then
|
||||||
|
|
||||||
|
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
|
||||||
|
rm $DST/$BDD/$TABLE.txt.bz2
|
||||||
|
fi
|
||||||
|
|
||||||
|
($COMPRESS $DST/$BDD/$TABLE.txt && chown $USER $DST/$BDD/$TABLE.txt.bz2 && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.bz2) &
|
||||||
|
|
||||||
|
elif [ $COMPRESS == 'gzip' ]; then
|
||||||
|
|
||||||
|
if [ -f "$DST/$BDD/$TABLE.txt.gz" ]; then
|
||||||
|
rm $DST/$BDD/$TABLE.txt.gz
|
||||||
|
fi
|
||||||
|
|
||||||
|
($COMPRESS $DST/$BDD/$TABLE.txt && chown $USER $DST/$BDD/$TABLE.txt.gz && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.gz) &
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
else
|
||||||
|
f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found"
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
done
|
||||||
|
else
|
||||||
|
mkdir -p $DST/$DATABASE 2>/dev/null 1>&2
|
||||||
|
chown $USER $DST/$DATABASE
|
||||||
|
chmod $DIRECTORYATTRIBUTES $DST/$DATABASE
|
||||||
|
touch $DST/$DATABASE/_error.log
|
||||||
|
|
||||||
|
query="SHOW CREATE DATABASE \`$DATABASE\`;"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$DATABASE/__create.sql
|
||||||
|
if [ -f $DST/$DATABASE/__create.sql ]; then
|
||||||
f_log " > Export create"
|
f_log " > Export create"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
|
query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
|
||||||
for viewName in $(mysql --defaults-file=$CONFIG_FILE $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
for viewName in $(mysql --defaults-file=$CONFIG_FILE $DATABASE -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||||
mysqldump --defaults-file=$CONFIG_FILE $BDD $viewName >> $DST/$BDD/__views.sql 2>> $DST/$BDD/error.log
|
mysqldump --defaults-file=$CONFIG_FILE $DATABASE $viewName >> $DST/$DATABASE/__views.sql 2>> $DST/$DATABASE/_error.log
|
||||||
array_views+=($viewName)
|
array_views+=($viewName)
|
||||||
done
|
done
|
||||||
if [ -f $DST/$BDD/__views.sql ]; then
|
if [ -f $DST/$DATABASE/__views.sql ]; then
|
||||||
f_log " > Exports views"
|
f_log " > Exports views"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --routines --no-create-info --no-data --no-create-db --skip-opt $BDD 2>> $DST/$BDD/error.log | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql
|
mysqldump --defaults-file=$CONFIG_FILE --routines --no-create-info --no-data --no-create-db --skip-opt $DATABASE 2>> $DST/$DATABASE/_error.log | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$DATABASE/__routines.sql
|
||||||
if [ -f $DST/$BDD/__routines.sql ]; then
|
if [ -f $DST/$DATABASE/__routines.sql ]; then
|
||||||
f_log " > Exports Routines"
|
f_log " > Exports Routines"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local default_tables_exclude=(
|
local default_tables_exclude=(
|
||||||
'slow_log'
|
'slow_log'
|
||||||
'general_log'
|
'general_log'
|
||||||
)
|
)
|
||||||
|
|
||||||
tables_exclude=( ${default_tables_exclude[@]} ${array_views[@]} ${EXCLUDE_TABLES[@]} )
|
tables_exclude=( ${default_tables_exclude[@]} ${array_views[@]} ${EXCLUDE_TABLES[@]} )
|
||||||
tables_exclude_expression=`prepaire_skip_expression "${tables_exclude[@]}"`
|
tables_exclude_expression=$(prepaire_skip_expression "${tables_exclude[@]}")
|
||||||
f_log "Exclude tables: $tables_exclude_expression"
|
f_log "Exclude tables: $tables_exclude_expression"
|
||||||
|
|
||||||
data_tables_exclude=( ${EXCLUDE_DATA_TABLES[@]} )
|
data_tables_exclude=( ${EXCLUDE_DATA_TABLES[@]} )
|
||||||
data_tables_exclude_expression=`prepaire_skip_expression "${data_tables_exclude[@]}"`
|
data_tables_exclude_expression=$(prepaire_skip_expression "${data_tables_exclude[@]}")
|
||||||
f_log "Exclude data tables: $data_tables_exclude_expression"
|
f_log "Exclude data tables: $data_tables_exclude_expression"
|
||||||
|
|
||||||
|
tables=( ${TABLES[@]} )
|
||||||
|
tables_expression=$(prepaire_skip_expression "${tables[@]}")
|
||||||
|
f_log "Only tables: $tables_expression"
|
||||||
|
|
||||||
query="SHOW TABLES;"
|
query="SHOW TABLES;"
|
||||||
for TABLE in $(mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $BDD -e "$query" | egrep -v "$tables_exclude_expression"); do
|
command="mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $DATABASE -e \"$query\""
|
||||||
f_log " ** Dump $BDD.$TABLE"
|
|
||||||
|
if [ $tables_exclude_expression ]; then
|
||||||
|
command=" $command | egrep -v \"$tables_exclude_expression\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $tables_expression ]; then
|
||||||
|
command=" $command | egrep \"$tables_expression\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
f_log "Command: $command"
|
||||||
|
|
||||||
|
for TABLE in $(eval $command); do
|
||||||
|
|
||||||
|
f_log " ** Dump $DATABASE.$TABLE"
|
||||||
|
|
||||||
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
|
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
|
||||||
f_log "Exclude data from table $TABLE"
|
f_log "Exclude data from table $TABLE"
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/error.log
|
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/_error.log
|
||||||
else
|
else
|
||||||
# If fields has geospatial types
|
# If fields has geospatial types
|
||||||
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $BDD -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
|
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $DATABASE -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
|
||||||
hasGeo=$(eval $checkGeo)
|
hasGeo=$(eval $checkGeo)
|
||||||
if [ ! -z "$hasGeo" ]; then
|
if [ ! -z "$hasGeo" ]; then
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --result-file=$DST/$BDD/$TABLE.sql $BDD $TABLE 2>> $DST/$BDD/error.log
|
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --result-file=$DST/$DATABASE/$TABLE.sql $DATABASE $TABLE 2>> $DST/$DATABASE/_error.log
|
||||||
else
|
else
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/error.log
|
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/_error.log
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$DST/$BDD/$TABLE.sql" ]; then
|
|
||||||
chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.sql
|
|
||||||
chown $USER:$GROUP $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
|
|
||||||
|
|
||||||
if [ $COMPRESS ]; then
|
|
||||||
|
|
||||||
f_log " ** $COMPRESS $BDD/$TABLE.txt in background"
|
|
||||||
|
|
||||||
if [ $COMPRESS == 'bzip2' ]; then
|
|
||||||
|
|
||||||
if [ -f "$DST/$BDD/$TABLE.txt.bz2" ]; then
|
|
||||||
rm $DST/$BDD/$TABLE.txt.bz2
|
|
||||||
fi
|
|
||||||
|
|
||||||
($COMPRESS $DST/$BDD/$TABLE.txt && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.bz2 && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.bz2) &
|
|
||||||
|
|
||||||
elif [ $COMPRESS == 'gzip' ]; then
|
|
||||||
|
|
||||||
if [ -f "$DST/$BDD/$TABLE.txt.gz" ]; then
|
|
||||||
rm $DST/$BDD/$TABLE.txt.gz
|
|
||||||
fi
|
|
||||||
|
|
||||||
($COMPRESS $DST/$BDD/$TABLE.txt && chown $USER:$GROUP $DST/$BDD/$TABLE.txt.gz && chmod $FILEATTRIBUTES $DST/$BDD/$TABLE.txt.gz) &
|
|
||||||
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
|
||||||
f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -f "$DST/$DATABASE/$TABLE.sql" ]; then
|
||||||
|
chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.sql
|
||||||
|
chown $USER $DST/$DATABASE/$TABLE.sql
|
||||||
|
f_log " ** set perm on $DATABASE/$TABLE.sql"
|
||||||
|
else
|
||||||
|
f_log " ** WARNING : $DST/$DATABASE/$TABLE.sql not found"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "$DST/$DATABASE/$TABLE.txt" ]; then
|
||||||
|
|
||||||
|
if [ $COMPRESS ]; then
|
||||||
|
|
||||||
|
f_log " ** $COMPRESS $DATABASE/$TABLE.txt in background"
|
||||||
|
|
||||||
|
if [ $COMPRESS == 'bzip2' ]; then
|
||||||
|
|
||||||
|
if [ -f "$DST/$DATABASE/$TABLE.txt.bz2" ]; then
|
||||||
|
rm $DST/$DATABASE/$TABLE.txt.bz2
|
||||||
|
fi
|
||||||
|
|
||||||
|
($COMPRESS $DST/$DATABASE/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.txt.bz2 && chown $USER $DST/$DATABASE/$TABLE.txt.bz2) &
|
||||||
|
|
||||||
|
elif [ $COMPRESS == 'gzip' ]; then
|
||||||
|
|
||||||
|
if [ -f "$DST/$DATABASE/$TABLE.txt.gz" ]; then
|
||||||
|
rm $DST/$DATABASE/$TABLE.txt.gz
|
||||||
|
fi
|
||||||
|
|
||||||
|
($COMPRESS $DST/$DATABASE/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.txt.gz && chown $USER $DST/$DATABASE/$TABLE.txt.gz) &
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
else
|
||||||
|
f_log " ** WARNING : $DST/$DATABASE/$TABLE.txt not found"
|
||||||
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
fi
|
||||||
done
|
|
||||||
|
cp restore.sh $DST/
|
||||||
|
|
||||||
f_log " END "
|
f_log " END "
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
cat << EOF
|
cat << EOF
|
||||||
|
|
||||||
This mysql backup engine.
|
This script backup all database in backups directory or single database.
|
||||||
|
|
||||||
Usage: $0 <[options]> or bash $0 <[options]>
|
Usage: $0 <[options]> or bash $0 <[options]>
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-d= | --database Database name
|
||||||
-e= | --exclude= Exclude databases
|
-e= | --exclude= Exclude databases
|
||||||
--exclude-tables= Exclude tables
|
--exclude-tables= Exclude tables
|
||||||
--exclude-data-tables= Exclude data tables
|
--exclude-data-tables= Exclude data tables
|
||||||
|
@ -194,16 +313,13 @@ Examples:
|
||||||
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --exclude="mysql" --lifetime="1 day ago"
|
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --exclude="mysql" --lifetime="1 day ago"
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --lifetime="1 day ago"
|
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --lifetime="1 day ago"
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
backup.sh --verbose --dir="/home/backups/mysql" --exclude="mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
||||||
|
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ $# = 0 ]; then
|
DATABASE=''
|
||||||
usage;
|
TABLES=''
|
||||||
exit;
|
|
||||||
fi
|
|
||||||
|
|
||||||
EXCLUDE_DATABASES=''
|
EXCLUDE_DATABASES=''
|
||||||
EXCLUDE_TABLES=''
|
EXCLUDE_TABLES=''
|
||||||
EXCLUDE_DATA_TABLES=''
|
EXCLUDE_DATA_TABLES=''
|
||||||
|
@ -218,15 +334,17 @@ for BIN in $BIN_DEPS; do
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
for i in "$@"
|
for i in "$@"
|
||||||
do
|
do
|
||||||
case $i in
|
case $i in
|
||||||
|
-d=* | --database=*)
|
||||||
|
DATABASE=( "${i#*=}" )
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
|
-t=* | --tables=*)
|
||||||
|
TABLES=( "${i#*=}" )
|
||||||
|
shift # past argument=value
|
||||||
|
;;
|
||||||
-e=* | --exclude=*)
|
-e=* | --exclude=*)
|
||||||
EXCLUDE_DATABASES=( "${i#*=}" )
|
EXCLUDE_DATABASES=( "${i#*=}" )
|
||||||
shift # past argument=value
|
shift # past argument=value
|
||||||
|
@ -238,7 +356,7 @@ do
|
||||||
--exclude-data-tables=*)
|
--exclude-data-tables=*)
|
||||||
EXCLUDE_DATA_TABLES=( "${i#*=}" )
|
EXCLUDE_DATA_TABLES=( "${i#*=}" )
|
||||||
shift # past argument=value
|
shift # past argument=value
|
||||||
;;
|
;;
|
||||||
-c=* | --compress=*)
|
-c=* | --compress=*)
|
||||||
COMPRESS=( "${i#*=}" )
|
COMPRESS=( "${i#*=}" )
|
||||||
shift # past argument=value
|
shift # past argument=value
|
||||||
|
@ -264,7 +382,9 @@ do
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# unknown option
|
printf "\n Unknown parameter => $i \n\n"
|
||||||
|
exit
|
||||||
|
# unknown option
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
@ -277,11 +397,11 @@ DSTOLD=$BACKUP_DIR/$DATEOLD
|
||||||
if [ ! -d "$DST" ]; then
|
if [ ! -d "$DST" ]; then
|
||||||
mkdir -p $DST;
|
mkdir -p $DST;
|
||||||
chmod $DIRECTORYATTRIBUTES $DST;
|
chmod $DIRECTORYATTRIBUTES $DST;
|
||||||
chown $USER:$GROUP $DST;
|
chown $USER $DST;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -d "$DSTOLD" ]; then
|
if [ -d "$DSTOLD" ]; then
|
||||||
rm -fr $DSTOLD;
|
rm -r $DSTOLD;
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# === SETTINGS ===
|
# === SETTINGS ===
|
||||||
|
@ -290,6 +410,8 @@ f_log "Dump into: $DST"
|
||||||
f_log "Config file: $CONFIG_FILE"
|
f_log "Config file: $CONFIG_FILE"
|
||||||
f_log "Verbose: $VERBOSE"
|
f_log "Verbose: $VERBOSE"
|
||||||
f_log "Compress: $COMPRESS"
|
f_log "Compress: $COMPRESS"
|
||||||
|
f_log "Database: $DATABASE"
|
||||||
|
f_log "Tables: $TABLES"
|
||||||
f_log "Exclude databases: $EXCLUDE_DATABASES"
|
f_log "Exclude databases: $EXCLUDE_DATABASES"
|
||||||
f_log "Exclude tables: $EXCLUDE_TABLES"
|
f_log "Exclude tables: $EXCLUDE_TABLES"
|
||||||
f_log "Life time: $TIME_REMOVED_DUMP_FILES"
|
f_log "Life time: $TIME_REMOVED_DUMP_FILES"
|
||||||
|
|
312
backup_db.sh
312
backup_db.sh
|
@ -1,312 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
VERBOSE=0
|
|
||||||
COMPRESS='bzip2'
|
|
||||||
USER='mysql'
|
|
||||||
GROUP='mysql'
|
|
||||||
DIRECTORYATTRIBUTES=0770
|
|
||||||
FILEATTRIBUTES=640
|
|
||||||
TIME_REMOVED_DUMP_FILES='1 week ago'
|
|
||||||
BACKUP_DIR='/var/backups/mysql'
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
|
|
||||||
# === DO NOT EDIT BELOW THIS LINE ===
|
|
||||||
|
|
||||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
|
||||||
|
|
||||||
# === FUNCTIONS ===
|
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
# === FUNCTION ===
|
|
||||||
f_log()
|
|
||||||
{
|
|
||||||
local bold=$(tput bold)
|
|
||||||
local yellow=$(tput setf 6)
|
|
||||||
local red=$(tput setf 4)
|
|
||||||
local green=$(tput setf 2)
|
|
||||||
local reset=$(tput sgr0)
|
|
||||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
|
||||||
|
|
||||||
logger "BACKUP: $@"
|
|
||||||
|
|
||||||
if [ $VERBOSE -eq 1 ]; then
|
|
||||||
echo "BACKUP: $@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
prepaire_skip_expression()
|
|
||||||
{
|
|
||||||
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 default_databases_exclude=(
|
|
||||||
'information_schema'
|
|
||||||
'performance_schema'
|
|
||||||
)
|
|
||||||
|
|
||||||
local array_views=()
|
|
||||||
|
|
||||||
mkdir -p $DST/$DATABASE 2>/dev/null 1>&2
|
|
||||||
chown $USER:$GROUP $DST/$DATABASE
|
|
||||||
chmod $DIRECTORYATTRIBUTES $DST/$DATABASE
|
|
||||||
touch $DST/$DATABASE/error.log
|
|
||||||
|
|
||||||
query="SHOW CREATE DATABASE \`$DATABASE\`;"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$DATABASE/__create.sql
|
|
||||||
if [ -f $DST/$DATABASE/__create.sql ]; then
|
|
||||||
f_log " > Export create"
|
|
||||||
fi
|
|
||||||
|
|
||||||
query="SHOW FULL TABLES WHERE Table_type = 'VIEW';"
|
|
||||||
for viewName in $(mysql --defaults-file=$CONFIG_FILE $DATABASE -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
|
||||||
mysqldump --defaults-file=$CONFIG_FILE $DATABASE $viewName >> $DST/$DATABASE/__views.sql 2>> $DST/$DATABASE/error.log
|
|
||||||
array_views+=($viewName)
|
|
||||||
done
|
|
||||||
if [ -f $DST/$DATABASE/__views.sql ]; then
|
|
||||||
f_log " > Exports views"
|
|
||||||
fi
|
|
||||||
|
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --routines --no-create-info --no-data --no-create-db --skip-opt $DATABASE 2>> $DST/$DATABASE/error.log | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$DATABASE/__routines.sql
|
|
||||||
if [ -f $DST/$DATABASE/__routines.sql ]; then
|
|
||||||
f_log " > Exports Routines"
|
|
||||||
fi
|
|
||||||
|
|
||||||
local default_tables_exclude=(
|
|
||||||
'slow_log'
|
|
||||||
'general_log'
|
|
||||||
)
|
|
||||||
|
|
||||||
tables_exclude=( ${default_tables_exclude[@]} ${array_views[@]} ${EXCLUDE_TABLES[@]} )
|
|
||||||
tables_exclude_expression=$(prepaire_skip_expression "${tables_exclude[@]}")
|
|
||||||
f_log "Exclude tables: $tables_exclude_expression"
|
|
||||||
|
|
||||||
data_tables_exclude=( ${EXCLUDE_DATA_TABLES[@]} )
|
|
||||||
data_tables_exclude_expression=$(prepaire_skip_expression "${data_tables_exclude[@]}")
|
|
||||||
f_log "Exclude data tables: $data_tables_exclude_expression"
|
|
||||||
|
|
||||||
tables=( ${TABLES[@]} )
|
|
||||||
tables_expression=$(prepaire_skip_expression "${tables[@]}")
|
|
||||||
f_log "Only tables: $tables_expression"
|
|
||||||
|
|
||||||
query="SHOW TABLES;"
|
|
||||||
command="mysql --defaults-file=$CONFIG_FILE --skip-column-names -B $DATABASE -e \"$query\""
|
|
||||||
|
|
||||||
if [ $tables_exclude_expression ]; then
|
|
||||||
command=" $command | egrep -v \"$tables_exclude_expression\""
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $tables_expression ]; then
|
|
||||||
command=" $command | egrep \"$tables_expression\""
|
|
||||||
fi
|
|
||||||
|
|
||||||
f_log "Command: $command"
|
|
||||||
|
|
||||||
for TABLE in $(eval $command); do
|
|
||||||
|
|
||||||
f_log " ** Dump $DATABASE.$TABLE"
|
|
||||||
|
|
||||||
if [ $(echo $data_tables_exclude_expression| grep $TABLE) ]; then
|
|
||||||
f_log "Exclude data from table $TABLE"
|
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
|
||||||
else
|
|
||||||
# If fields has geospatial types
|
|
||||||
checkGeo="mysql --defaults-file=$CONFIG_FILE -B $DATABASE -e \"SHOW COLUMNS FROM $TABLE WHERE Type IN ('point', 'polygon', 'geometry', 'linestring')\""
|
|
||||||
hasGeo=$(eval $checkGeo)
|
|
||||||
if [ ! -z "$hasGeo" ]; then
|
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --result-file=$DST/$DATABASE/$TABLE.sql $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
|
||||||
else
|
|
||||||
mysqldump --defaults-file=$CONFIG_FILE --flush-logs --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$DST/$DATABASE/$TABLE.sql" ]; then
|
|
||||||
chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.sql
|
|
||||||
chown $USER:$GROUP $DST/$DATABASE/$TABLE.sql
|
|
||||||
f_log " ** set perm on $DATABASE/$TABLE.sql"
|
|
||||||
else
|
|
||||||
f_log " ** WARNING : $DST/$DATABASE/$TABLE.sql not found"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$DST/$DATABASE/$TABLE.txt" ]; then
|
|
||||||
|
|
||||||
if [ $COMPRESS ]; then
|
|
||||||
|
|
||||||
f_log " ** $COMPRESS $DATABASE/$TABLE.txt in background"
|
|
||||||
|
|
||||||
if [ $COMPRESS == 'bzip2' ]; then
|
|
||||||
|
|
||||||
if [ -f "$DST/$DATABASE/$TABLE.txt.bz2" ]; then
|
|
||||||
rm $DST/$DATABASE/$TABLE.txt.bz2
|
|
||||||
fi
|
|
||||||
|
|
||||||
($COMPRESS $DST/$DATABASE/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.txt.bz2 && chown $USER:$GROUP $DST/$DATABASE/$TABLE.txt.bz2) &
|
|
||||||
|
|
||||||
elif [ $COMPRESS == 'gzip' ]; then
|
|
||||||
|
|
||||||
if [ -f "$DST/$DATABASE/$TABLE.txt.gz" ]; then
|
|
||||||
rm $DST/$DATABASE/$TABLE.txt.gz
|
|
||||||
fi
|
|
||||||
|
|
||||||
($COMPRESS $DST/$DATABASE/$TABLE.txt && chmod $FILEATTRIBUTES $DST/$DATABASE/$TABLE.txt.gz && chown $USER:$GROUP $DST/$DATABASE/$TABLE.txt.gz) &
|
|
||||||
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
|
||||||
|
|
||||||
else
|
|
||||||
f_log " ** WARNING : $DST/$DATABASE/$TABLE.txt not found"
|
|
||||||
fi
|
|
||||||
|
|
||||||
done
|
|
||||||
|
|
||||||
f_log " END "
|
|
||||||
}
|
|
||||||
|
|
||||||
usage()
|
|
||||||
{
|
|
||||||
cat << EOF
|
|
||||||
|
|
||||||
This mysql backup engine.
|
|
||||||
|
|
||||||
Usage: $0 <[database-name]> <[options]> or bash $0 <[database-name]> <[options]>
|
|
||||||
|
|
||||||
Options:
|
|
||||||
--tables= Dump only such tables
|
|
||||||
--exclude-tables= Exclude tables
|
|
||||||
--exclude-data-tables= Exclude data tables
|
|
||||||
-c= | --compress= Compress with gzip or bzip2
|
|
||||||
-v | --verbose Add verbose into output
|
|
||||||
-l | --lifetime= Lifetime for dump files
|
|
||||||
--config= Config file of Debian format
|
|
||||||
--dir= Backup directory
|
|
||||||
-h | --help This text
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
backup.sh --verbose --compress=
|
|
||||||
backup.sh --verbose --compress=gzip
|
|
||||||
backup.sh --verbose --compress=bzip2
|
|
||||||
backup.sh --verbose --compress=
|
|
||||||
backup.sh --verbose --compress= --lifetime="3 day ago"
|
|
||||||
backup.sh --verbose --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/var/backups/mysql" --config="/etc/mysql/debian.cnf" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
|
||||||
backup.sh --verbose --dir="/home/backups/mysql" --tables="tbl_template tbl_template1 tbl_template2"
|
|
||||||
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
if [ $# = 0 ]; then
|
|
||||||
usage;
|
|
||||||
exit;
|
|
||||||
fi
|
|
||||||
|
|
||||||
DATABASE=''
|
|
||||||
TABLES=''
|
|
||||||
EXCLUDE_TABLES=''
|
|
||||||
EXCLUDE_DATA_TABLES=''
|
|
||||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
|
||||||
|
|
||||||
DATABASE="$1"
|
|
||||||
|
|
||||||
for BIN in $BIN_DEPS; do
|
|
||||||
which $BIN 1>/dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Required command file not be found: $BIN"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
for i in "$@"
|
|
||||||
do
|
|
||||||
case $i in
|
|
||||||
-t=* | --tables=*)
|
|
||||||
TABLES=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
--exclude-tables=*)
|
|
||||||
EXCLUDE_TABLES=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
--exclude-data-tables=*)
|
|
||||||
EXCLUDE_DATA_TABLES=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
-c=* | --compress=*)
|
|
||||||
COMPRESS=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
-l=* | --lifetime=*)
|
|
||||||
TIME_REMOVED_DUMP_FILES=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
--dir=*)
|
|
||||||
BACKUP_DIR=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
--config=*)
|
|
||||||
CONFIG_FILE=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
-v | --verbose)
|
|
||||||
VERBOSE=1
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
-h | --help)
|
|
||||||
usage
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
# unknown option
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
DATE=`date '+%Y.%m.%d'`
|
|
||||||
DATEOLD=`date --date="$TIME_REMOVED_DUMP_FILES" +%Y.%m.%d`
|
|
||||||
DST=$BACKUP_DIR/$DATE
|
|
||||||
DSTOLD=$BACKUP_DIR/$DATEOLD
|
|
||||||
|
|
||||||
if [ ! -d "$DST" ]; then
|
|
||||||
mkdir -p $DST;
|
|
||||||
chmod $DIRECTORYATTRIBUTES $DST;
|
|
||||||
chown $USER:$GROUP $DST;
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -d "$DSTOLD" ]; then
|
|
||||||
rm -fr $DSTOLD;
|
|
||||||
fi
|
|
||||||
|
|
||||||
# === SETTINGS ===
|
|
||||||
f_log "============================================"
|
|
||||||
f_log "Dump into: $DST"
|
|
||||||
f_log "Config file: $CONFIG_FILE"
|
|
||||||
f_log "Verbose: $VERBOSE"
|
|
||||||
f_log "Compress: $COMPRESS"
|
|
||||||
f_log "Database: $DATABASE"
|
|
||||||
f_log "Tables: $TABLES"
|
|
||||||
f_log "Exclude tables: $EXCLUDE_TABLES"
|
|
||||||
f_log "Life time: $TIME_REMOVED_DUMP_FILES"
|
|
||||||
f_log "============================================"
|
|
||||||
f_log ""
|
|
||||||
|
|
||||||
# === AUTORUN ===
|
|
||||||
backup
|
|
|
@ -1 +0,0 @@
|
||||||
*
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
# === CONFIG ===
|
||||||
|
# Set 1 to active verbose log
|
||||||
|
VERBOSE=0
|
||||||
|
|
||||||
|
# Change default compression
|
||||||
|
COMPRESS='bzip2'
|
||||||
|
|
||||||
|
# Owner of backup
|
||||||
|
USER=$(whoami)
|
||||||
|
|
||||||
|
# Default directory attribute
|
||||||
|
DIRECTORYATTRIBUTES=0770
|
||||||
|
|
||||||
|
# Default file attribute
|
||||||
|
FILEATTRIBUTES=640
|
||||||
|
|
||||||
|
# Deleted backup older than
|
||||||
|
TIME_REMOVED_DUMP_FILES='1 week ago'
|
||||||
|
|
||||||
|
# Backups directory
|
||||||
|
BACKUP_DIR='backups'
|
||||||
|
|
||||||
|
# File config for mysql socket
|
||||||
|
CONFIG_FILE='etc/mysql/local.cfg'
|
||||||
|
|
||||||
|
# Size of chunk
|
||||||
|
CONFIG_CHUNK=1000000
|
|
@ -1,12 +0,0 @@
|
||||||
# Automatically generated for Debian scripts. DO NOT TOUCH!
|
|
||||||
[client]
|
|
||||||
host = localhost
|
|
||||||
user = root
|
|
||||||
password = password
|
|
||||||
socket = /usr/local/var/run/mysqld.sock
|
|
||||||
[mysql_upgrade]
|
|
||||||
host = localhost
|
|
||||||
user = debian-sys-maint
|
|
||||||
password =
|
|
||||||
socket = /var/run/mysqld/mysqld.sock
|
|
||||||
basedir = /usr
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
[client]
|
||||||
|
host = localhost
|
||||||
|
user = root
|
||||||
|
password = password
|
||||||
|
socket = /usr/local/var/run/mysqld.sock
|
435
restore.sh
435
restore.sh
|
@ -1,9 +1,16 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# === CONFIG ===
|
if test -f etc/config.cfg ; then
|
||||||
CONFIG_CHUNK=1000000
|
. etc/config.cfg
|
||||||
VERBOSE=0
|
else
|
||||||
|
printf "\n You need edit etc/config.cfg.example and save it to etc/config.cfg \n"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
if test ! -f $CONFIG_FILE ; then
|
||||||
|
printf "\n You need edit etc/mysql/debian.cnf.example and save it to $CONFIG_FILE \n"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
# === DO NOT EDIT BELOW THIS LINE ===
|
# === DO NOT EDIT BELOW THIS LINE ===
|
||||||
|
|
||||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
||||||
|
@ -11,178 +18,274 @@ if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
||||||
# === FUNCTIONS ===
|
# === FUNCTIONS ===
|
||||||
database_exists()
|
database_exists()
|
||||||
{
|
{
|
||||||
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
|
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
|
||||||
if [ "$RESULT" == "$@" ]; then
|
if [ "$RESULT" == "$@" ]; then
|
||||||
echo YES
|
echo YES
|
||||||
else
|
else
|
||||||
echo NO
|
echo NO
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
f_log()
|
f_log()
|
||||||
{
|
{
|
||||||
local bold=$(tput bold)
|
local bold=$(tput bold)
|
||||||
local yellow=$(tput setf 6)
|
local yellow=$(tput setf 6)
|
||||||
local red=$(tput setf 4)
|
local red=$(tput setf 4)
|
||||||
local green=$(tput setf 2)
|
local green=$(tput setf 2)
|
||||||
local reset=$(tput sgr0)
|
local reset=$(tput sgr0)
|
||||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
||||||
|
|
||||||
logger "RESTORE: $@"
|
logger "RESTORE: $@"
|
||||||
|
|
||||||
if [ $VERBOSE -eq 1 ]; then
|
if [ $VERBOSE -eq 1 ]; then
|
||||||
echo "RESTORE: $@"
|
echo "RESTORE: $@"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
restore()
|
restore()
|
||||||
{
|
{
|
||||||
DIR=$@
|
f_log "** START **"
|
||||||
|
|
||||||
f_log "Check path $DIR"
|
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
|
||||||
|
|
||||||
f_log "** START **"
|
f_log "Check path $DIR"
|
||||||
|
|
||||||
f_log "Check runtime"
|
f_log "Database $DATABASE"
|
||||||
for BIN in $BIN_DEPS; do
|
|
||||||
which $BIN 1>/dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
f_log "Check runtime"
|
||||||
f_log "Error: Required file could not be found: $BIN"
|
for BIN in $BIN_DEPS; do
|
||||||
exit 1
|
which $BIN 1>/dev/null 2>&1
|
||||||
fi
|
if [ $? -ne 0 ]; then
|
||||||
done
|
f_log "Error: Required file could not be found: $BIN"
|
||||||
|
exit 1
|
||||||
f_log "Check backups folder"
|
fi
|
||||||
if [ "$(ls -1 $DIR/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
|
done
|
||||||
f_log "Your must run script from backup directory"
|
|
||||||
exit 1
|
f_log "Check backups folder"
|
||||||
fi
|
if [ "$(ls -1 $DIR/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
|
||||||
|
f_log "Your must run script from backup directory, like backups/YYYY.mm.dd/restore.sh"
|
||||||
for i in $(ls -1 -d $DIR/*); do
|
exit 1
|
||||||
|
fi
|
||||||
BDD=$(basename $i)
|
|
||||||
|
if [ -z ${DATABASE} ]; then
|
||||||
for skip in "${DATABASES_SKIP[@]}"; do
|
for i in $(ls -1 -d $DIR/*); do
|
||||||
if [ $BDD = $skip ]; then
|
|
||||||
f_log "Skip database $BDD"
|
BDD=$(basename $i)
|
||||||
unset BDD
|
|
||||||
break
|
for skip in "${DATABASES_SKIP[@]}"; do
|
||||||
fi
|
if [ $BDD = $skip ]; then
|
||||||
done
|
f_log "Skip database $BDD"
|
||||||
|
unset BDD
|
||||||
for select in "${DATABASES_SELECTED[@]}"; do
|
break
|
||||||
if [ $BDD != $select ]; then
|
fi
|
||||||
f_log "Skip database $BDD"
|
done
|
||||||
unset BDD
|
|
||||||
break
|
if [ $BDD ]; then
|
||||||
fi
|
|
||||||
done
|
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||||
|
f_log "Create database $BDD"
|
||||||
if [ $BDD ]; then
|
mysql --defaults-file=$CONFIG_FILE < $DIR/$BDD/__create.sql 2>/dev/null
|
||||||
|
fi
|
||||||
if [ -f $DIR/$BDD/__create.sql ]; then
|
|
||||||
f_log "Create database $BDD"
|
if [ $(database_exists $BDD) != "YES" ]; then
|
||||||
mysql --defaults-file=$CONFIG_FILE < $DIR/$BDD/__create.sql 2>/dev/null
|
f_log "Error: Database $BDD dose not exists";
|
||||||
fi
|
else
|
||||||
|
|
||||||
if [ $(database_exists $BDD) != "YES" ]; then
|
tables=$(ls -1 $DIR/$BDD | grep -v __ | grep .sql | awk -F. '{print $1}' | sort | uniq)
|
||||||
f_log "Error: Database $BDD dose not exists";
|
|
||||||
else
|
f_log "Create tables in $BDD"
|
||||||
|
for TABLE in $tables; do
|
||||||
tables=$(ls -1 $DIR/$BDD | grep -v __ | grep .sql | awk -F. '{print $1}' | sort | uniq)
|
f_log "Create table: $BDD/$TABLE"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||||
f_log "Create tables in $BDD"
|
|
||||||
for TABLE in $tables; do
|
|
||||||
f_log "Create table: $BDD/$TABLE"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
|
||||||
DROP TABLE IF EXISTS $TABLE;
|
DROP TABLE IF EXISTS $TABLE;
|
||||||
SOURCE $DIR/$BDD/$TABLE.sql;
|
SOURCE $DIR/$BDD/$TABLE.sql;
|
||||||
SET foreign_key_checks = 1;"
|
SET foreign_key_checks = 1;"
|
||||||
done
|
done
|
||||||
|
|
||||||
f_log "Import data into $BDD"
|
f_log "Import data into $BDD"
|
||||||
for TABLE in $tables; do
|
for TABLE in $tables; do
|
||||||
f_log "Import data into $BDD/$TABLE"
|
f_log "Import data into $BDD/$TABLE"
|
||||||
|
|
||||||
if [ -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
||||||
f_log "< $TABLE"
|
f_log "< $TABLE"
|
||||||
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||||
rm $DIR/$BDD/$TABLE.txt
|
rm $DIR/$BDD/$TABLE.txt
|
||||||
fi
|
fi
|
||||||
bunzip2 -k $DIR/$BDD/$TABLE.txt.bz2
|
bunzip2 -k $DIR/$BDD/$TABLE.txt.bz2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -s "$DIR/$BDD/$TABLE.txt" ]; then
|
if [ -s "$DIR/$BDD/$TABLE.txt" ]; then
|
||||||
|
|
||||||
f_log "+ $TABLE"
|
f_log "+ $TABLE"
|
||||||
|
|
||||||
split -l $CONFIG_CHUNK "$DIR/$BDD/$TABLE.txt" "$DIR/$BDD/${TABLE}_part_"
|
split -l $CONFIG_CHUNK "$DIR/$BDD/$TABLE.txt" "$DIR/$BDD/${TABLE}_part_"
|
||||||
for segment in "$DIR/$BDD/${TABLE}"_part_*; do
|
for segment in "$DIR/$BDD/${TABLE}"_part_*; do
|
||||||
f_log "Restore from $segment"
|
f_log "Restore from $segment"
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
mysql --defaults-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||||
SET character_set_database=utf8;
|
SET character_set_database=utf8;
|
||||||
LOAD DATA LOCAL INFILE '$segment'
|
LOAD DATA LOCAL INFILE '$segment'
|
||||||
INTO TABLE $TABLE;
|
INTO TABLE $TABLE;
|
||||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||||
|
|
||||||
if [ -f "$segment" ]; then
|
if [ -f "$segment" ]; then
|
||||||
f_log "Delete segment $segment"
|
f_log "Delete segment $segment"
|
||||||
rm "$segment"
|
rm "$segment"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||||
f_log "Delete source: $TABLE.txt"
|
f_log "Delete source: $TABLE.txt"
|
||||||
rm $DIR/$BDD/$TABLE.txt
|
rm $DIR/$BDD/$TABLE.txt
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $DATABASES_TABLE_CHECK ]; then
|
if [ $DATABASES_TABLE_CHECK ]; then
|
||||||
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||||
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
||||||
f_log "$TABLE [OK]"
|
f_log "$TABLE [OK]"
|
||||||
else
|
else
|
||||||
f_log "$TABLE [ERR]"
|
f_log "$TABLE [ERR]"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
||||||
f_log "Import routines into $BDD"
|
f_log "Import routines into $BDD"
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__routines.sql 2>/dev/null
|
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__routines.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||||
f_log "Import views into $BDD"
|
f_log "Import views into $BDD"
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__views.sql 2>/dev/null
|
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__views.sql 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
|
done
|
||||||
f_log "Flush privileges;"
|
else
|
||||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
BDD=$DATABASE
|
||||||
|
DIR=$DIR/$DATABASE
|
||||||
f_log "** END **"
|
|
||||||
|
if [ $DIR ]; then
|
||||||
|
|
||||||
|
f_log "Found backup files $DIR"
|
||||||
|
|
||||||
|
if [ -f $DIR/__create.sql ]; then
|
||||||
|
f_log "Create database $BDD"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE < $DIR/__create.sql 2>/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
tables=$(ls -1 $DIR | grep -v __ | grep .sql | 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-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||||
|
DROP TABLE IF EXISTS $TABLE;
|
||||||
|
SOURCE $DIR/$TABLE.sql;
|
||||||
|
SET foreign_key_checks = 1;"
|
||||||
|
done
|
||||||
|
|
||||||
|
f_log "Import data into $BDD"
|
||||||
|
for TABLE in $tables; do
|
||||||
|
|
||||||
|
f_log "Import data into $BDD/$TABLE"
|
||||||
|
if [ -f "$DIR/$TABLE.txt.bz2" ]; then
|
||||||
|
f_log "< $TABLE"
|
||||||
|
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||||
|
f_log "rm $DIR/$TABLE.txt"
|
||||||
|
rm $DIR/$TABLE.txt
|
||||||
|
fi
|
||||||
|
bunzip2 -k $DIR/$TABLE.txt.bz2
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -s "$DIR/$TABLE.txt" ]; then
|
||||||
|
|
||||||
|
f_log "+ $TABLE"
|
||||||
|
|
||||||
|
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" "$DIR/${TABLE}_part_"
|
||||||
|
for segment in "$DIR/${TABLE}"_part_*; do
|
||||||
|
f_log "Restore from $segment"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||||
|
SET character_set_database=utf8;
|
||||||
|
LOAD DATA LOCAL INFILE '$segment'
|
||||||
|
INTO TABLE $TABLE;
|
||||||
|
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
||||||
|
|
||||||
|
if [ -f "$segment" ]; then
|
||||||
|
f_log "Delete segment $segment"
|
||||||
|
rm "$segment"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||||
|
f_log "Delete source file: $TABLE.txt"
|
||||||
|
rm $DIR/$TABLE.txt
|
||||||
|
fi
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $DATABASES_TABLE_CHECK ]; then
|
||||||
|
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||||
|
if [ ! $(innochecksum $DIR/$TABLE.ibd) ]; then
|
||||||
|
f_log "$TABLE [OK]"
|
||||||
|
else
|
||||||
|
f_log "$TABLE [ERR]"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -f "$DIR/__routines.sql" ]; then
|
||||||
|
f_log "Import routines into $BDD"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__routines.sql 2>/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "$DIR/__views.sql" ]; then
|
||||||
|
f_log "Import views into $BDD"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
f_log "Flush privileges;"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||||
|
|
||||||
|
f_log "** END **"
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
f_log "Database not found"
|
||||||
|
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
f_log "Flush privileges;"
|
||||||
|
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||||
|
|
||||||
|
f_log "** END **"
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
cat << EOF
|
cat << EOF
|
||||||
|
|
||||||
|
This script restore all databases in backups directory or single database.
|
||||||
|
|
||||||
usage: $0 options
|
usage: $0 options
|
||||||
|
|
||||||
This script restore databases.
|
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
-e Exclude databases
|
-d= | --database Database name
|
||||||
-s Selected databases
|
-e Exclude databases
|
||||||
-c Check innochecksum of table after import
|
-s Selected databases
|
||||||
|
-c Check innochecksum of table after import
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
backup.sh -v -d=databaseName
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,12 +295,6 @@ BACKUP_DIR=$(pwd)
|
||||||
|
|
||||||
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
||||||
|
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
for BIN in $BIN_DEPS; do
|
for BIN in $BIN_DEPS; do
|
||||||
which $BIN 1>/dev/null 2>&1
|
which $BIN 1>/dev/null 2>&1
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
|
@ -209,18 +306,18 @@ done
|
||||||
for i in "$@"
|
for i in "$@"
|
||||||
do
|
do
|
||||||
case $i in
|
case $i in
|
||||||
-e)
|
-e)
|
||||||
DATABASES_SKIP=( "${i#*=}" )
|
DATABASES_SKIP=( "${i#*=}" )
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-s)
|
-d=* | --database=*)
|
||||||
DATABASES_SELECTED=( "${i#*=}" )
|
DATABASE=( "${i#*=}" )
|
||||||
shift
|
shift # past argument=value
|
||||||
;;
|
;;
|
||||||
-c)
|
-c)
|
||||||
DATABASES_TABLE_CHECK=1
|
DATABASES_TABLE_CHECK=1
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--config=*)
|
--config=*)
|
||||||
CONFIG_FILE=( "${i#*=}" )
|
CONFIG_FILE=( "${i#*=}" )
|
||||||
shift # past argument=value
|
shift # past argument=value
|
||||||
|
@ -228,7 +325,7 @@ do
|
||||||
--chunk=*)
|
--chunk=*)
|
||||||
CONFIG_CHUNK=( "${i#*=}" )
|
CONFIG_CHUNK=( "${i#*=}" )
|
||||||
shift # past argument=value
|
shift # past argument=value
|
||||||
;;
|
;;
|
||||||
-v | --verbose)
|
-v | --verbose)
|
||||||
VERBOSE=1
|
VERBOSE=1
|
||||||
shift # past argument=value
|
shift # past argument=value
|
||||||
|
@ -238,7 +335,7 @@ do
|
||||||
exit
|
exit
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# unknown option
|
# unknown option
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
@ -248,9 +345,11 @@ f_log "============================================"
|
||||||
f_log "Restore from: $BACKUP_DIR"
|
f_log "Restore from: $BACKUP_DIR"
|
||||||
f_log "Config file: $CONFIG_FILE"
|
f_log "Config file: $CONFIG_FILE"
|
||||||
f_log "Verbose: $VERBOSE"
|
f_log "Verbose: $VERBOSE"
|
||||||
f_log "Selected databases: $DATABASES_SELECTED"
|
f_log "Selected databases: $DATABASE"
|
||||||
f_log "============================================"
|
f_log "============================================"
|
||||||
f_log ""
|
f_log ""
|
||||||
|
|
||||||
# === AUTORUN ===
|
# === AUTORUN ===
|
||||||
restore $(pwd)
|
time restore
|
||||||
|
|
||||||
|
f_log "$@"
|
||||||
|
|
203
restore_db.sh
203
restore_db.sh
|
@ -1,203 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# === CONFIG ===
|
|
||||||
CONFIG_CHUNK=1000000
|
|
||||||
VERBOSE=0
|
|
||||||
|
|
||||||
# === DO NOT EDIT BELOW THIS LINE ===
|
|
||||||
|
|
||||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
|
||||||
|
|
||||||
# === FUNCTIONS ===
|
|
||||||
f_log()
|
|
||||||
{
|
|
||||||
local bold=$(tput bold)
|
|
||||||
local yellow=$(tput setf 6)
|
|
||||||
local red=$(tput setf 4)
|
|
||||||
local green=$(tput setf 2)
|
|
||||||
local reset=$(tput sgr0)
|
|
||||||
local toend=$(tput hpa $(tput cols))$(tput cub 6)
|
|
||||||
|
|
||||||
logger "RESTORE: $@"
|
|
||||||
|
|
||||||
if [ $VERBOSE -eq 1 ]; then
|
|
||||||
echo "RESTORE: $@"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
restore()
|
|
||||||
{
|
|
||||||
DIR=$@
|
|
||||||
|
|
||||||
f_log "Check path $DIR"
|
|
||||||
|
|
||||||
f_log "** START **"
|
|
||||||
|
|
||||||
BDD=${DIR##*/}
|
|
||||||
|
|
||||||
if [ $BDD ]; then
|
|
||||||
|
|
||||||
f_log "Found backup files $DIR"
|
|
||||||
|
|
||||||
if [ -f $DIR/__create.sql ]; then
|
|
||||||
f_log "Create database $BDD"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE < $DIR/__create.sql 2>/dev/null
|
|
||||||
fi
|
|
||||||
|
|
||||||
tables=$(ls -1 $DIR | grep -v __ | grep .sql | 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-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
|
||||||
DROP TABLE IF EXISTS $TABLE;
|
|
||||||
SOURCE $DIR/$TABLE.sql;
|
|
||||||
SET foreign_key_checks = 1;"
|
|
||||||
done
|
|
||||||
|
|
||||||
f_log "Import data into $BDD"
|
|
||||||
for TABLE in $tables; do
|
|
||||||
|
|
||||||
f_log "Import data into $BDD/$TABLE"
|
|
||||||
if [ -f "$DIR/$TABLE.txt.bz2" ]; then
|
|
||||||
f_log "< $TABLE"
|
|
||||||
if [ -f "$DIR/$TABLE.txt" ]; then
|
|
||||||
f_log "rm $DIR/$TABLE.txt"
|
|
||||||
rm $DIR/$TABLE.txt
|
|
||||||
fi
|
|
||||||
bunzip2 -k $DIR/$TABLE.txt.bz2
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -s "$DIR/$TABLE.txt" ]; then
|
|
||||||
|
|
||||||
f_log "+ $TABLE"
|
|
||||||
|
|
||||||
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" "$DIR/${TABLE}_part_"
|
|
||||||
for segment in "$DIR/${TABLE}"_part_*; do
|
|
||||||
f_log "Restore from $segment"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
|
||||||
SET character_set_database=utf8;
|
|
||||||
LOAD DATA LOCAL INFILE '$segment'
|
|
||||||
INTO TABLE $TABLE;
|
|
||||||
SET foreign_key_checks = 1; SET unique_checks = 1; SET sql_log_bin = 1;"
|
|
||||||
|
|
||||||
if [ -f "$segment" ]; then
|
|
||||||
f_log "Delete segment $segment"
|
|
||||||
rm "$segment"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ -f "$DIR/$TABLE.txt" ]; then
|
|
||||||
f_log "Delete source file: $TABLE.txt"
|
|
||||||
rm $DIR/$TABLE.txt
|
|
||||||
fi
|
|
||||||
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $DATABASES_TABLE_CHECK ]; then
|
|
||||||
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
|
||||||
if [ ! $(innochecksum $DIR/$TABLE.ibd) ]; then
|
|
||||||
f_log "$TABLE [OK]"
|
|
||||||
else
|
|
||||||
f_log "$TABLE [ERR]"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ -f "$DIR/__routines.sql" ]; then
|
|
||||||
f_log "Import routines into $BDD"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__routines.sql 2>/dev/null
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -f "$DIR/__views.sql" ]; then
|
|
||||||
f_log "Import views into $BDD"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
|
||||||
fi
|
|
||||||
|
|
||||||
f_log "Flush privileges;"
|
|
||||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
|
||||||
|
|
||||||
f_log "** END **"
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
f_log "Database not found"
|
|
||||||
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
usage()
|
|
||||||
{
|
|
||||||
cat << EOF
|
|
||||||
usage: $0 options
|
|
||||||
|
|
||||||
This script restore databases.
|
|
||||||
|
|
||||||
OPTIONS:
|
|
||||||
-e Exclude databases
|
|
||||||
-s Selected databases
|
|
||||||
-c Check innochecksum of table after import
|
|
||||||
|
|
||||||
|
|
||||||
EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
BACKUP_DIR=$(pwd)
|
|
||||||
|
|
||||||
BIN_DEPS="ls grep awk sort uniq bunzip2 bzip2 mysql"
|
|
||||||
|
|
||||||
if [ -f '/etc/debian_version' ]; then
|
|
||||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
|
||||||
else
|
|
||||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
|
||||||
fi
|
|
||||||
|
|
||||||
for BIN in $BIN_DEPS; do
|
|
||||||
which $BIN 1>/dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
echo "Error: Required command file not be found: $BIN"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
for i in "$@"
|
|
||||||
do
|
|
||||||
case $i in
|
|
||||||
--config=*)
|
|
||||||
CONFIG_FILE=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
--chunk=*)
|
|
||||||
CONFIG_CHUNK=( "${i#*=}" )
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
-c)
|
|
||||||
DATABASES_TABLE_CHECK=1
|
|
||||||
shift
|
|
||||||
;;
|
|
||||||
-v | --verbose)
|
|
||||||
VERBOSE=1
|
|
||||||
shift # past argument=value
|
|
||||||
;;
|
|
||||||
-h | --help)
|
|
||||||
usage
|
|
||||||
exit
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
# unknown option
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
# === SETTINGS ===
|
|
||||||
f_log "============================================"
|
|
||||||
f_log "Restore from: $BACKUP_DIR"
|
|
||||||
f_log "Config file: $CONFIG_FILE"
|
|
||||||
f_log "Verbose: $VERBOSE"
|
|
||||||
f_log "============================================"
|
|
||||||
f_log ""
|
|
||||||
|
|
||||||
# === AUTORUN ===
|
|
||||||
restore $BACKUP_DIR
|
|
Loading…
Reference in New Issue