Compare commits
14 commits
Author | SHA1 | Date | |
---|---|---|---|
31628857bf | |||
1d4dbd9889 | |||
ba4cee5adb | |||
a33059f6a8 | |||
46405d5594 | |||
8c5a46063d | |||
ac136915d5 | |||
598e20a86e | |||
e06706685a | |||
ced1fbb02f | |||
7ea3f69bfc | |||
2b9ef28316 | |||
f6ab63abce | |||
501b3a5e36 |
24
backup.sh
24
backup.sh
|
@ -62,22 +62,21 @@ backup()
|
|||
database_exclude_expression=`prepaire_skip_expression "${database_exclude[@]}"`
|
||||
f_log "Exclude databases: $database_exclude_expression"
|
||||
|
||||
for BDD in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | egrep -v "$database_exclude_expression"); do
|
||||
|
||||
touch $DST/$BDD/error.log
|
||||
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-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$BDD/__create.sql
|
||||
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-extra-file=$CONFIG_FILE $BDD -N -e "$query" | sed 's/|//' | awk '{print $1}'); do
|
||||
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
|
||||
|
@ -104,15 +103,22 @@ backup()
|
|||
f_log "Exclude data tables: $data_tables_exclude_expression"
|
||||
|
||||
query="SHOW TABLES;"
|
||||
for TABLE in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B $BDD -e "$query" | egrep -v "$tables_exclude_expression"); do
|
||||
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
|
||||
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$BDD/ $BDD $TABLE 2>> $DST/$BDD/error.log
|
||||
fi
|
||||
# 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
|
||||
|
@ -280,7 +286,7 @@ fi
|
|||
|
||||
# === SETTINGS ===
|
||||
f_log "============================================"
|
||||
f_log "Dump into: $BACKUP_DIR"
|
||||
f_log "Dump into: $DST"
|
||||
f_log "Config file: $CONFIG_FILE"
|
||||
f_log "Verbose: $VERBOSE"
|
||||
f_log "Compress: $COMPRESS"
|
||||
|
|
56
backup_db.sh
56
backup_db.sh
|
@ -62,22 +62,21 @@ backup()
|
|||
'performance_schema'
|
||||
)
|
||||
|
||||
local array_views=()
|
||||
|
||||
touch $DST/$DATABASE/error.log
|
||||
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-extra-file=$CONFIG_FILE --skip-column-names -B -e "$query" | awk -F"\t" '{ print $2 }' > $DST/$DATABASE/__create.sql
|
||||
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-extra-file=$CONFIG_FILE $DATABASE -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 $DATABASE $viewName >> $DST/$DATABASE/__views.sql 2>> $DST/$DATABASE/error.log
|
||||
array_views+=($viewName)
|
||||
done
|
||||
|
@ -96,22 +95,46 @@ backup()
|
|||
)
|
||||
|
||||
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"
|
||||
|
||||
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"
|
||||
|
||||
tables=( ${TABLES[@]} )
|
||||
tables_expression=$(prepaire_skip_expression "${tables[@]}")
|
||||
f_log "Only tables: $tables_expression"
|
||||
|
||||
query="SHOW TABLES;"
|
||||
for TABLE in $(mysql --defaults-extra-file=$CONFIG_FILE --skip-column-names -B $DATABASE -e "$query" | egrep -v "$tables_exclude_expression"); do
|
||||
f_log " ** Dump $DATABASE.$TABLE"
|
||||
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
|
||||
mysqldump --defaults-file=$CONFIG_FILE --no-data --add-drop-table --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
else
|
||||
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/error.log
|
||||
# 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
|
||||
|
@ -166,6 +189,7 @@ usage()
|
|||
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
|
||||
|
@ -185,7 +209,7 @@ Examples:
|
|||
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
|
||||
}
|
||||
|
@ -196,6 +220,7 @@ if [ $# = 0 ]; then
|
|||
fi
|
||||
|
||||
DATABASE=''
|
||||
TABLES=''
|
||||
EXCLUDE_TABLES=''
|
||||
EXCLUDE_DATA_TABLES=''
|
||||
BIN_DEPS="mysql mysqldump $COMPRESS"
|
||||
|
@ -213,6 +238,10 @@ 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
|
||||
|
@ -268,11 +297,12 @@ fi
|
|||
|
||||
# === SETTINGS ===
|
||||
f_log "============================================"
|
||||
f_log "Dump into: $BACKUP_DIR"
|
||||
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 "============================================"
|
||||
|
|
16
restore.sh
16
restore.sh
|
@ -11,7 +11,7 @@ if [ ! -n "$BASH" ] ;then echo Please run this script $0 with bash; exit 1; fi
|
|||
# === FUNCTIONS ===
|
||||
database_exists()
|
||||
{
|
||||
RESULT=`mysqlshow --defaults-extra-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
|
||||
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
|
||||
if [ "$RESULT" == "$@" ]; then
|
||||
echo YES
|
||||
else
|
||||
|
@ -82,7 +82,7 @@ restore()
|
|||
|
||||
if [ -f $DIR/$BDD/__create.sql ]; then
|
||||
f_log "Create database $BDD"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE < $DIR/$BDD/__create.sql 2>/dev/null
|
||||
mysql --defaults-file=$CONFIG_FILE < $DIR/$BDD/__create.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
if [ $(database_exists $BDD) != "YES" ]; then
|
||||
|
@ -94,7 +94,7 @@ restore()
|
|||
f_log "Create tables in $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Create table: $BDD/$TABLE"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||
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;"
|
||||
|
@ -119,8 +119,8 @@ restore()
|
|||
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-extra-file=$CONFIG_FILE $BDD --local-infile -e "SET foreign_key_checks = 0; SET unique_checks = 0; SET sql_log_bin = 0;
|
||||
SET character_set_filesystem=utf8;
|
||||
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;"
|
||||
|
@ -152,12 +152,12 @@ restore()
|
|||
|
||||
if [ -f "$DIR/$BDD/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
mysql --defaults-extra-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
|
||||
|
||||
if [ -f "$DIR/$BDD/__views.sql" ]; then
|
||||
f_log "Import views into $BDD"
|
||||
mysql --defaults-extra-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
|
||||
|
@ -165,7 +165,7 @@ restore()
|
|||
done
|
||||
|
||||
f_log "Flush privileges;"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE -e "flush privileges;"
|
||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||
|
||||
f_log "** END **"
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ restore()
|
|||
|
||||
if [ -f $DIR/__create.sql ]; then
|
||||
f_log "Create database $BDD"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE < $DIR/__create.sql 2>/dev/null
|
||||
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)
|
||||
|
@ -49,7 +49,7 @@ restore()
|
|||
f_log "Create tables in $BDD"
|
||||
for TABLE in $tables; do
|
||||
f_log "Create table: $BDD/$TABLE"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE $BDD -e "SET foreign_key_checks = 0;
|
||||
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;"
|
||||
|
@ -75,7 +75,7 @@ restore()
|
|||
split -l $CONFIG_CHUNK "$DIR/$TABLE.txt" "$DIR/${TABLE}_part_"
|
||||
for segment in "$DIR/${TABLE}"_part_*; do
|
||||
f_log "Restore from $segment"
|
||||
mysql --defaults-extra-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;
|
||||
LOAD DATA LOCAL INFILE '$segment'
|
||||
INTO TABLE $TABLE;
|
||||
|
@ -87,7 +87,7 @@ restore()
|
|||
fi
|
||||
done
|
||||
|
||||
if [ ! -f "$DIR/$TABLE.txt.bz2" ]; then
|
||||
if [ -f "$DIR/$TABLE.txt" ]; then
|
||||
f_log "Delete source file: $TABLE.txt"
|
||||
rm $DIR/$TABLE.txt
|
||||
fi
|
||||
|
@ -108,16 +108,16 @@ restore()
|
|||
|
||||
if [ -f "$DIR/__routines.sql" ]; then
|
||||
f_log "Import routines into $BDD"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE $BDD < $DIR/__routines.sql 2>/dev/null
|
||||
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-extra-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
||||
mysql --defaults-file=$CONFIG_FILE $BDD < $DIR/__views.sql 2>/dev/null
|
||||
fi
|
||||
|
||||
f_log "Flush privileges;"
|
||||
mysql --defaults-extra-file=$CONFIG_FILE -e "flush privileges;"
|
||||
mysql --defaults-file=$CONFIG_FILE -e "flush privileges;"
|
||||
|
||||
f_log "** END **"
|
||||
|
||||
|
@ -201,4 +201,3 @@ f_log ""
|
|||
|
||||
# === AUTORUN ===
|
||||
restore $BACKUP_DIR
|
||||
|
||||
|
|
Loading…
Reference in a new issue