Compare commits
152 commits
Author | SHA1 | Date | |
---|---|---|---|
31628857bf | |||
1d4dbd9889 | |||
ba4cee5adb | |||
a33059f6a8 | |||
46405d5594 | |||
8c5a46063d | |||
ac136915d5 | |||
598e20a86e | |||
e06706685a | |||
ced1fbb02f | |||
7ea3f69bfc | |||
2b9ef28316 | |||
f6ab63abce | |||
501b3a5e36 | |||
40266a4d46 | |||
6cd7eb5427 | |||
0586125ab7 | |||
fd432fb80a | |||
33c5791782 | |||
438c05c1a6 | |||
6ecc220823 | |||
c635e3bc29 | |||
e2efabe8e4 | |||
8f54453049 | |||
719abd0f15 | |||
024da4cc60 | |||
d42db146f8 | |||
14041fb59c | |||
dbb56b45b5 | |||
73fcf08477 | |||
60303b706f | |||
cf43e7931d | |||
3dc75d5713 | |||
194b577717 | |||
c308b1b808 | |||
971c2d0b1d | |||
9b0bdb0c35 | |||
3cf1be72da | |||
be23cdb838 | |||
f6678aa588 | |||
1d573a6d47 | |||
afd2c0613d | |||
116511a0a3 | |||
5b668dce39 | |||
1147cce066 | |||
6b9f6fafd7 | |||
2b09c15dab | |||
db9e0568da | |||
faee180325 | |||
fe692084e8 | |||
e331888171 | |||
ea01afbb3d | |||
32d15cc0be | |||
a2215462bc | |||
9c509c674c | |||
e85bdf8668 | |||
15d4283418 | |||
d23f850dc8 | |||
082ebca7fb | |||
c6431c0752 | |||
488c209924 | |||
81fc75d36f | |||
728a1c6607 | |||
7c9b46e560 | |||
ee7c575e57 | |||
f812f1c8ba | |||
8566d75142 | |||
4c8fd7581b | |||
a0b772cc9c | |||
84da1b12c7 | |||
dd2a1b341d | |||
3dd4b7644d | |||
7afff39e74 | |||
b74f33fb52 | |||
2abca96dc6 | |||
05c4948279 | |||
cb498298b8 | |||
29440875ec | |||
37a09e9350 | |||
d76a2ac08f | |||
1f97f5db7e | |||
eee6d2b588 | |||
8f46f814ed | |||
efe163a783 | |||
3d617f68b3 | |||
32447b49ff | |||
4d4fc1a66b | |||
2c98f9ad79 | |||
427c725e45 | |||
2044e320af | |||
e2eda7e50f | |||
c2cb0f8cea | |||
c7a1974134 | |||
169441219c | |||
04a842a39a | |||
73162b0471 | |||
4cd8cfd110 | |||
0fa72501bf | |||
8e0a123074 | |||
eeea0fdcba | |||
ead3f36fa8 | |||
8a478eb411 | |||
f2011dc85a | |||
84f978e61e | |||
a4a86199cf | |||
0b52962577 | |||
bf32098972 | |||
cb334ef6f2 | |||
4812068986 | |||
1e00862ac0 | |||
f9e7d22df5 | |||
17a0d2aeee | |||
6c35e458c1 | |||
c5ca85ff3e | |||
ea20cf199d | |||
6018645562 | |||
727b5706b6 | |||
f6e3e2256b | |||
2df24b40fe | |||
8095c2ce9e | |||
733592b339 | |||
0d79392955 | |||
73e86ddf01 | |||
ae351ae3ed | |||
71b85a7413 | |||
96d2aa720a | |||
fcd576742c | |||
1d7fce8bc1 | |||
c2e87acc5b | |||
f4b3fc8bce | |||
f6e44ddd73 | |||
884f6e9c76 | |||
c2e3bf0d4a | |||
3cbf44e7f7 | |||
80e08c33e0 | |||
fb3f982184 | |||
2ed63fb76c | |||
b1239ebfc3 | |||
8f247f88b6 | |||
86d0ea7076 | |||
3b7b584876 | |||
7dd188f6a4 | |||
95c8ffec35 | |||
68cba63223 | |||
6474de59f4 | |||
c3ff1e2554 | |||
60e672995b | |||
c84c5e1392 | |||
93af490aad | |||
3bb245248d | |||
1c7db087d3 | |||
04fd4e4960 |
112
README.md
112
README.md
|
@ -1,4 +1,112 @@
|
|||
mysql_utils
|
||||
===========
|
||||
mysql_utils for Debian or Other OS
|
||||
=======================================
|
||||
|
||||
Russian documentation: http://docs.mirocow.com/doku.php?id=mysql:mysql_utils
|
||||
|
||||
Backup and Restore data from MySql tables
|
||||
|
||||
Install
|
||||
======
|
||||
|
||||
cd ~
|
||||
git clone https://github.com/Mirocow/mysql_utils.git
|
||||
cd mysql_utils
|
||||
|
||||
Backup all databases
|
||||
======
|
||||
|
||||
cd ~
|
||||
cd mysql_utils
|
||||
bash backup.sh
|
||||
|
||||
Backup selected database
|
||||
======
|
||||
|
||||
cd ~
|
||||
cd mysql_utils
|
||||
bash backup_db.sh <[database-name]>
|
||||
|
||||
Restore for selected date
|
||||
=======
|
||||
|
||||
cd /var/backups/mysql/[some date]
|
||||
bash ~/mysql_utils/restore.sh
|
||||
|
||||
Restore selected DB
|
||||
=======
|
||||
|
||||
cd /var/backups/mysql/[some date]/[some db name]
|
||||
bash ~/mysql_utils/restore_db.sh
|
||||
|
||||
Automation backup with Cron
|
||||
===========================
|
||||
|
||||
nano /etc/default/db_backup
|
||||
|
||||
START=yes
|
||||
|
||||
nano /etc/cron.daily/db_backup
|
||||
|
||||
#!/bin/sh
|
||||
|
||||
. /etc/default/db_backup
|
||||
|
||||
if [ "$START" = "yes" ]; then
|
||||
logger "Start databases backup system..."
|
||||
/bin/bash /root/scripts/mysql_utils/backup.sh --e="some_exclude_database some_else_db"
|
||||
fi
|
||||
|
||||
Check work
|
||||
==========
|
||||
|
||||
# tail -f /var/log/syslog
|
||||
May 23 12:25:34 db1 logger: BACKUP: ** Dump tecdoc.2013.ALI_COORD
|
||||
May 23 12:25:35 db1 logger: BACKUP: ** set perm on tecdoc.2013/AL
|
||||
May 23 12:25:35 db1 logger: BACKUP: ** bzip2 tecdoc.2013/ALI_COOR
|
||||
May 23 12:25:35 db1 logger: BACKUP: ** Dump tecdoc.2013.ARTICLES
|
||||
May 23 12:25:43 db1 logger: BACKUP: ** set perm on tecdoc.2013/AR
|
||||
May 23 12:25:43 db1 logger: BACKUP: ** bzip2 tecdoc.2013/ARTICLES
|
||||
May 23 12:25:43 db1 logger: BACKUP: ** Dump tecdoc.2013.ARTICLES_
|
||||
May 23 12:25:43 db1 logger: BACKUP: ** set perm on tecdoc.2013/AR
|
||||
May 23 12:25:43 db1 logger: BACKUP: ** bzip2 tecdoc.2013/ARTICLES
|
||||
May 23 12:25:43 db1 logger: BACKUP: ** Dump tecdoc.2013.ARTICLE_C
|
||||
|
||||
Tested on
|
||||
==========
|
||||
|
||||
Debiad
|
||||
FreeBsd
|
||||
Ubuntu
|
||||
|
||||
Help
|
||||
===========
|
||||
|
||||
``` sh
|
||||
# bash backup.sh --help
|
||||
usage: backup.sh options
|
||||
|
||||
This script buckup all databases.
|
||||
|
||||
Usage: backup.sh <[options]>
|
||||
|
||||
Options:
|
||||
-e= | --exclude= Exclude databases
|
||||
--exclude-tables= Exclude 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= --exclude="mysql"
|
||||
backup.sh --verbose --compress= --exclude="mysql" --lifetime="3 day ago"
|
||||
backup.sh --verbose --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" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
||||
```
|
||||
|
|
407
backup.sh
Normal file → Executable file
407
backup.sh
Normal file → Executable file
|
@ -1,143 +1,300 @@
|
|||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
# === CONFIG ===
|
||||
BCKDIR='/var/backups/mysql'
|
||||
MYCNF='/etc/mysql/debian.cnf'
|
||||
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'
|
||||
|
||||
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
|
||||
# === DO NOT EDIT BELOW THIS LINE ===
|
||||
|
||||
if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; 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=()
|
||||
|
||||
database_exclude=( ${default_databases_exclude[@]} ${EXCLUDE_DATABASES[@]} )
|
||||
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
|
||||
|
||||
mkdir -p $DST/$BDD 2>/dev/null 1>&2
|
||||
chown $USER:$GROUP $DST/$BDD
|
||||
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
|
||||
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 --flush-logs --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 --flush-logs --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:$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
|
||||
|
||||
else
|
||||
f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found"
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
done
|
||||
|
||||
f_log " END "
|
||||
}
|
||||
|
||||
usage()
|
||||
{
|
||||
cat << EOF
|
||||
|
||||
This mysql backup engine.
|
||||
|
||||
Usage: $0 <[options]> or bash $0 <[options]>
|
||||
|
||||
Options:
|
||||
-e= | --exclude= Exclude databases
|
||||
--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= --exclude="mysql"
|
||||
backup.sh --verbose --compress= --exclude="mysql" --lifetime="3 day ago"
|
||||
backup.sh --verbose --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" --exclude-tables="tbl_template" --lifetime="1 day ago"
|
||||
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
if [ $# = 0 ]; then
|
||||
usage;
|
||||
exit;
|
||||
fi
|
||||
|
||||
EXCLUDE_DATABASES=''
|
||||
EXCLUDE_TABLES=''
|
||||
EXCLUDE_DATA_TABLES=''
|
||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||
|
||||
# === 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"
|
||||
echo "Error: Required command file not be found: $BIN"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
if [ -f '/etc/debian_version' ]; then
|
||||
CONFIG_FILE='/etc/mysql/debian.cnf'
|
||||
else
|
||||
CONFIG_FILE='~/mysql_utils/etc/mysql/debian.cnf'
|
||||
fi
|
||||
|
||||
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'
|
||||
)
|
||||
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
|
||||
|
||||
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
|
||||
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
|
||||
mysqldump --defaults-file=$MYCNF $BDD $viewName >> $DST/$BDD/__views.sql
|
||||
done
|
||||
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
|
||||
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
|
||||
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"
|
||||
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"
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
done
|
||||
|
||||
f_log "** END **"
|
||||
}
|
||||
|
||||
while getopts ":e:r:s" opt;
|
||||
for i in "$@"
|
||||
do
|
||||
case ${opt} in
|
||||
e)
|
||||
exclude=${OPTARG}
|
||||
IFS=, read -r -a DATABASES_SKIP <<< "$exclude"
|
||||
;;
|
||||
*)
|
||||
usag
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
case $i in
|
||||
-e=* | --exclude=*)
|
||||
EXCLUDE_DATABASES=( "${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
|
||||
#shift "$((OPTIND - 1))"
|
||||
|
||||
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 "Exclude databases: $EXCLUDE_DATABASES"
|
||||
f_log "Exclude tables: $EXCLUDE_TABLES"
|
||||
f_log "Life time: $TIME_REMOVED_DUMP_FILES"
|
||||
f_log "============================================"
|
||||
f_log ""
|
||||
|
||||
# === AUTORUN ===
|
||||
backup
|
||||
|
||||
|
|
312
backup_db.sh
Executable file
312
backup_db.sh
Executable file
|
@ -0,0 +1,312 @@
|
|||
#!/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
backups/.gitignore
vendored
Normal file
1
backups/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*
|
12
etc/mysql/debian.cnf
Normal file
12
etc/mysql/debian.cnf
Normal file
|
@ -0,0 +1,12 @@
|
|||
# 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
|
333
restore.sh
333
restore.sh
|
@ -1,15 +1,17 @@
|
|||
#!/bin/sh
|
||||
#!/bin/bash
|
||||
|
||||
# === CONFIG ===
|
||||
DIR_PWD=$(pwd)
|
||||
BIN_DEPS='bunzip2 bzip2 mysql'
|
||||
MYCNF='/etc/mysql/debian.cnf'
|
||||
MYDATA='/var/lib/mysql'
|
||||
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 ===
|
||||
database_exists()
|
||||
{
|
||||
RESULT=`mysqlshow --defaults-extra-file=$MYCNF $@| grep -v Wildcard | grep -o $@`
|
||||
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
|
||||
if [ "$RESULT" == "$@" ]; then
|
||||
echo YES
|
||||
else
|
||||
|
@ -19,25 +21,26 @@ database_exists()
|
|||
|
||||
f_log()
|
||||
{
|
||||
logger "RESTORE: $@"
|
||||
}
|
||||
|
||||
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
|
||||
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 **"
|
||||
|
||||
f_log "Check runtime"
|
||||
|
@ -50,132 +53,204 @@ restore()
|
|||
done
|
||||
|
||||
f_log "Check backups folder"
|
||||
if [ "$(ls -1 $DIR_PWD/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
|
||||
if [ "$(ls -1 $DIR/*/__create.sql 2>/dev/null | wc -l)" -le "0" ]; then
|
||||
f_log "Your must run script from backup directory"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for i in $(ls -1 -d $DIR_PWD/*); do
|
||||
for i in $(ls -1 -d $DIR/*); do
|
||||
|
||||
BDD=$(basename $i)
|
||||
|
||||
for skip in "${DATABASES_SKIP[@]}"; do
|
||||
if [ $BDD = $skip ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
for select in "${DATABASES_SELECTED[@]}"; do
|
||||
if [ $BDD != $select ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
BDD=$(basename $i)
|
||||
|
||||
for skip in "${DATABASES_SKIP[@]}"; do
|
||||
if [ $BDD = $skip ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
for select in "${DATABASES_SELECTED[@]}"; do
|
||||
if [ $BDD != $select ]; then
|
||||
f_log "Skip database $BDD"
|
||||
unset BDD
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ $BDD ]; then
|
||||
|
||||
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||
f_log "Create database $BDD"
|
||||
mysql --defaults-file=$CONFIG_FILE < $DIR/$BDD/__create.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ $(database_exists $BDD) != "YES" ]; then
|
||||
f_log "Error: Database $BDD dose not exists";
|
||||
else
|
||||
|
||||
tables=$(ls -1 $DIR/$BDD | 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/$BDD/$TABLE.sql;
|
||||
SET foreign_key_checks = 1;"
|
||||
done
|
||||
|
||||
if [ $BDD ]; then
|
||||
f_log "Import data into $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Import data into $BDD/$TABLE"
|
||||
|
||||
if [ -f "$DIR/$BDD/$TABLE.txt.bz2" ]; then
|
||||
f_log "< $TABLE"
|
||||
if [ -f "$DIR/$BDD/$TABLE.txt" ]; then
|
||||
rm $DIR/$BDD/$TABLE.txt
|
||||
fi
|
||||
bunzip2 -k $DIR/$BDD/$TABLE.txt.bz2
|
||||
fi
|
||||
|
||||
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
|
||||
if [ -s "$DIR/$BDD/$TABLE.txt" ]; then
|
||||
|
||||
f_log "+ $TABLE"
|
||||
|
||||
if [ $(database_exists $BDD) != "YES" ]; then
|
||||
f_log "Error: Database $BDD dose not exists";
|
||||
else
|
||||
split -l $CONFIG_CHUNK "$DIR/$BDD/$TABLE.txt" "$DIR/$BDD/${TABLE}_part_"
|
||||
for segment in "$DIR/$BDD/${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
|
||||
|
||||
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;
|
||||
DROP TABLE IF EXISTS $TABLE;
|
||||
SOURCE $DIR_PWD/$BDD/$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_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 [ $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/$BDD/$TABLE.txt" ]; then
|
||||
f_log "Delete source: $TABLE.txt"
|
||||
rm $DIR/$BDD/$TABLE.txt
|
||||
fi
|
||||
|
||||
if [ $DATABASES_TABLE_CHECK ]; then
|
||||
if [ -f "$DIR/$BDD/$TABLE.ibd" ]; then
|
||||
if [ ! $(innochecksum $DIR/$BDD/$TABLE.ibd) ]; then
|
||||
f_log "$TABLE [OK]"
|
||||
else
|
||||
f_log "$TABLE [ERR]"
|
||||
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
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__routines.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||
f_log "Import views into $BDD"
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/$BDD/__views.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
f_log "Flush privileges;"
|
||||
mysql --defaults-extra-file=$MYCNF -e "flush privileges;"
|
||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||
|
||||
f_log "** END **"
|
||||
}
|
||||
|
||||
# === EXECUTE ===
|
||||
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)
|
||||
DATABASES_TABLE_CHECK=1
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
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
|
||||
}
|
||||
|
||||
# === CHECKS ===
|
||||
|
||||
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
|
||||
#shift "$((OPTIND - 1))"
|
||||
|
||||
for i in "$@"
|
||||
do
|
||||
case $i in
|
||||
-e)
|
||||
DATABASES_SKIP=( "${i#*=}" )
|
||||
shift
|
||||
;;
|
||||
-s)
|
||||
DATABASES_SELECTED=( "${i#*=}" )
|
||||
shift
|
||||
;;
|
||||
-c)
|
||||
DATABASES_TABLE_CHECK=1
|
||||
shift
|
||||
;;
|
||||
--config=*)
|
||||
CONFIG_FILE=( "${i#*=}" )
|
||||
shift # past argument=value
|
||||
;;
|
||||
--chunk=*)
|
||||
CONFIG_CHUNK=( "${i#*=}" )
|
||||
shift # past argument=value
|
||||
;;
|
||||
-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 "Selected databases: $DATABASES_SELECTED"
|
||||
f_log "============================================"
|
||||
f_log ""
|
||||
|
||||
# === AUTORUN ===
|
||||
restore
|
||||
restore $(pwd)
|
||||
|
|
203
restore_db.sh
Normal file
203
restore_db.sh
Normal file
|
@ -0,0 +1,203 @@
|
|||
#!/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 a new issue