Compare commits

...

3 Commits
master ... dev

Author SHA1 Message Date
Knah Tsaeb 4f3dcf663a Add config file
Merge backup_db.sh in backup.sh
Merge restore_db.sh in restore.sh
Update README
2019-02-21 12:33:09 +01:00
Knah Tsaeb cf65af4066 Add changelog and config file for [gitchangelog](https://github.com/vaab/gitchangelog) 2019-02-21 12:26:29 +01:00
Knah Tsaeb 94378c92dd Add gitignore 2019-02-21 11:49:49 +01:00
13 changed files with 1148 additions and 837 deletions

295
.gitchangelog.rc Normal file
View File

@ -0,0 +1,295 @@
# -*- coding: utf-8; mode: python -*-
##
## Format
##
## ACTION: [AUDIENCE:] COMMIT_MSG [!TAG ...]
##
## Description
##
## ACTION is one of 'chg', 'fix', 'new'
##
## Is WHAT the change is about.
##
## 'chg' is for refactor, small improvement, cosmetic changes...
## 'fix' is for bug fixes
## 'new' is for new features, big improvement
##
## AUDIENCE is optional and one of 'dev', 'usr', 'pkg', 'test', 'doc'
##
## Is WHO is concerned by the change.
##
## 'dev' is for developpers (API changes, refactors...)
## 'usr' is for final users (UI changes)
## 'pkg' is for packagers (packaging changes)
## 'test' is for testers (test only related changes)
## 'doc' is for doc guys (doc only changes)
##
## COMMIT_MSG is ... well ... the commit message itself.
##
## TAGs are additionnal adjective as 'refactor' 'minor' 'cosmetic'
##
## They are preceded with a '!' or a '@' (prefer the former, as the
## latter is wrongly interpreted in github.) Commonly used tags are:
##
## 'refactor' is obviously for refactoring code only
## 'minor' is for a very meaningless change (a typo, adding a comment)
## 'cosmetic' is for cosmetic driven change (re-indentation, 80-col...)
## 'wip' is for partial functionality but complete subfunctionality.
##
## Example:
##
## new: usr: support of bazaar implemented
## chg: re-indentend some lines !cosmetic
## new: dev: updated code to be compatible with last version of killer lib.
## fix: pkg: updated year of licence coverage.
## new: test: added a bunch of test around user usability of feature X.
## fix: typo in spelling my name in comment. !minor
##
## Please note that multi-line commit message are supported, and only the
## first line will be considered as the "summary" of the commit message. So
## tags, and other rules only applies to the summary. The body of the commit
## message will be displayed in the changelog without reformatting.
##
## ``ignore_regexps`` is a line of regexps
##
## Any commit having its full commit message matching any regexp listed here
## will be ignored and won't be reported in the changelog.
##
ignore_regexps = [
r'@minor', r'!minor',
r'@cosmetic', r'!cosmetic',
r'@refactor', r'!refactor',
r'@wip', r'!wip',
r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*[p|P]kg:',
r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*[d|D]ev:',
r'^(.{3,3}\s*:)?\s*[fF]irst commit.?\s*$',
r'^$', ## ignore commits with empty messages
]
## ``section_regexps`` is a list of 2-tuples associating a string label and a
## list of regexp
##
## Commit messages will be classified in sections thanks to this. Section
## titles are the label, and a commit is classified under this section if any
## of the regexps associated is matching.
##
## Please note that ``section_regexps`` will only classify commits and won't
## make any changes to the contents. So you'll probably want to go check
## ``subject_process`` (or ``body_process``) to do some changes to the subject,
## whenever you are tweaking this variable.
##
section_regexps = [
('Add', [
r'^[aA]dd\s*?([^\n]*)$',
]),
('Update', [
r'^[Uu]pdate\s*?([^\n]*)$',
]),
('Replace', [
r'^[rR]eplace\s*?([^\n]*)$',
]),
('Fix', [
r'^[fF]ix\s*?([^\n]*)$',
]),
('Delete', [
r'^[dD]elete\s*?([^\n]*)$',
]),
('Other', None ## Match all lines
),
]
subject_process = (strip | ucfirst | final_dot)
## ``body_process`` is a callable
##
## This callable will be given the original body and result will
## be used in the changelog.
##
## Available constructs are:
##
## - any python callable that take one txt argument and return txt argument.
##
## - ReSub(pattern, replacement): will apply regexp substitution.
##
## - Indent(chars=" "): will indent the text with the prefix
## Please remember that template engines gets also to modify the text and
## will usually indent themselves the text if needed.
##
## - Wrap(regexp=r"\n\n"): re-wrap text in separate paragraph to fill 80-Columns
##
## - noop: do nothing
##
## - ucfirst: ensure the first letter is uppercase.
## (usually used in the ``subject_process`` pipeline)
##
## - final_dot: ensure text finishes with a dot
## (usually used in the ``subject_process`` pipeline)
##
## - strip: remove any spaces before or after the content of the string
##
## - SetIfEmpty(msg="No commit message."): will set the text to
## whatever given ``msg`` if the current text is empty.
##
## Additionally, you can `pipe` the provided filters, for instance:
#body_process = Wrap(regexp=r'\n(?=\w+\s*:)') | Indent(chars=" ")
#body_process = Wrap(regexp=r'\n(?=\w+\s*:)')
#body_process = noop
body_process = ReSub(r'((^|\n)[A-Z]\w+(-\w+)*: .*(\n\s+.*)*)+$', r'') | strip
## ``subject_process`` is a callable
##
## This callable will be given the original subject and result will
## be used in the changelog.
##
## Available constructs are those listed in ``body_process`` doc.
##subject_process = (strip |
## ReSub(r'^([cC]hg|[fF]ix|[nN]ew)\s*:\s*((dev|use?r|pkg|test|doc)\s*:\s*)?([^\n@]*)(@[a-z]+\s+)*$', r'\4') |
## SetIfEmpty("No commit message.") | ucfirst | final_dot)
## ``tag_filter_regexp`` is a regexp
##
## Tags that will be used for the changelog must match this regexp.
##
tag_filter_regexp = r'^[0-9]+\.[0-9]+(\.[0-9]+)?$'
## ``unreleased_version_label`` is a string or a callable that outputs a string
##
## This label will be used as the changelog Title of the last set of changes
## between last valid tag and HEAD if any.
unreleased_version_label = "(unreleased)"
## ``output_engine`` is a callable
##
## This will change the output format of the generated changelog file
##
## Available choices are:
##
## - rest_py
##
## Legacy pure python engine, outputs ReSTructured text.
## This is the default.
##
## - mustache(<template_name>)
##
## Template name could be any of the available templates in
## ``templates/mustache/*.tpl``.
## Requires python package ``pystache``.
## Examples:
## - mustache("markdown")
## - mustache("restructuredtext")
##
## - makotemplate(<template_name>)
##
## Template name could be any of the available templates in
## ``templates/mako/*.tpl``.
## Requires python package ``mako``.
## Examples:
## - makotemplate("restructuredtext")
##
#output_engine = rest_py
#output_engine = mustache("restructuredtext")
output_engine = mustache(".gitchangelog.tpl")
#output_engine = makotemplate("restructuredtext")
## ``include_merge`` is a boolean
##
## This option tells git-log whether to include merge commits in the log.
## The default is to include them.
include_merge = True
## ``log_encoding`` is a string identifier
##
## This option tells gitchangelog what encoding is outputed by ``git log``.
## The default is to be clever about it: it checks ``git config`` for
## ``i18n.logOutputEncoding``, and if not found will default to git's own
## default: ``utf-8``.
#log_encoding = 'utf-8'
## ``publish`` is a callable
##
## Sets what ``gitchangelog`` should do with the output generated by
## the output engine. ``publish`` is a callable taking one argument
## that is an interator on lines from the output engine.
##
## Some helper callable are provided:
##
## Available choices are:
##
## - stdout
##
## Outputs directly to standard output
## (This is the default)
##
## - FileInsertAtFirstRegexMatch(file, pattern, idx=lamda m: m.start())
##
## Creates a callable that will parse given file for the given
## regex pattern and will insert the output in the file.
## ``idx`` is a callable that receive the matching object and
## must return a integer index point where to insert the
## the output in the file. Default is to return the position of
## the start of the matched string.
##
## - FileRegexSubst(file, pattern, replace, flags)
##
## Apply a replace inplace in the given file. Your regex pattern must
## take care of everything and might be more complex. Check the README
## for a complete copy-pastable example.
##
# publish = FileInsertIntoFirstRegexMatch(
# "CHANGELOG.rst",
# r'/(?P<rev>[0-9]+\.[0-9]+(\.[0-9]+)?)\s+\([0-9]+-[0-9]{2}-[0-9]{2}\)\n--+\n/',
# idx=lambda m: m.start(1)
# )
#publish = stdout
## ``revs`` is a list of callable or a list of string
##
## callable will be called to resolve as strings and allow dynamical
## computation of these. The result will be used as revisions for
## gitchangelog (as if directly stated on the command line). This allows
## to filter exaclty which commits will be read by gitchangelog.
##
## To get a full documentation on the format of these strings, please
## refer to the ``git rev-list`` arguments. There are many examples.
##
## Using callables is especially useful, for instance, if you
## are using gitchangelog to generate incrementally your changelog.
##
## Some helpers are provided, you can use them::
##
## - FileFirstRegexMatch(file, pattern): will return a callable that will
## return the first string match for the given pattern in the given file.
## If you use named sub-patterns in your regex pattern, it'll output only
## the string matching the regex pattern named "rev".
##
## - Caret(rev): will return the rev prefixed by a "^", which is a
## way to remove the given revision and all its ancestor.
##
## Please note that if you provide a rev-list on the command line, it'll
## replace this value (which will then be ignored).
##
## If empty, then ``gitchangelog`` will act as it had to generate a full
## changelog.
##
## The default is to use all commits to make the changelog.
#revs = ["^1.0.3", ]
#revs = [
# Caret(
# FileFirstRegexMatch(
# "CHANGELOG.rst",
# r"(?P<rev>[0-9]+\.[0-9]+(\.[0-9]+)?)\s+\([0-9]+-[0-9]{2}-[0-9]{2}\)\n--+\n")),
# "HEAD"
#]
revs = []

20
.gitchangelog.tpl Normal file
View File

@ -0,0 +1,20 @@
{{#general_title}}
# {{{title}}}
{{/general_title}}
{{#versions}}
## {{{label}}}
{{#sections}}
### {{{label}}}
{{#commits}}
* {{{subject}}} (commit {{commit.sha1_short}} by {{{author}}})
{{#body}}
{{{body_indented}}}
{{/body}}
{{/commits}}
{{/sections}}
{{/versions}}

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
backups
etc/mysql/*.cnf
etc/*.cfg

256
CHANGELOG.md Normal file
View File

@ -0,0 +1,256 @@
# Changelog
## (unreleased)
### Add
* 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)
### Add
* Add gitignore. (commit 94378c9 by Knah Tsaeb)
* Add param --tables. (commit 7ea3f69 by mirocow)
--tables - Select only such tables split by space
### Update
* Update backup.sh. (commit 2b9ef28 by mirocow)
* Update backup_db.sh. (commit f6ab63a by mirocow)
### Replace
* Replace option defaults-extra-file into defaults-file. (commit ba4cee5 by mirocow)
* Replace option defaults-extra-file into defaults-file. (commit a33059f by mirocow)
* Replace option defaults-extra-file into defaults-file. (commit 46405d5 by mirocow)
* Replace option defaults-extra-file into defaults-file. (commit 8c5a460 by mirocow)
### Fix
* Fix error touch: cannot touch. (commit 3162885 by mirocow)
fix error touch: cannot touch '/var/backups/mysql/2017.02.11/bd_name/error.log': No such file or directory
* Fix error touch: cannot touch. (commit 1d4dbd9 by mirocow)
fix error touch: cannot touch '/var/backups/mysql/2017.02.11/bd_name/error.log': No such file or directory
* Fix bd name. (commit 598e20a by mirocow)
* Fix dump with geospatial fields. (commit e067066 by mirocow)
* Fix dump with geospatial fields. (commit ced1fbb by mirocow)
* Fixed utf8 encode load data. (commit 501b3a5 by mirocow)
### Delete
* Delete txt source after execute mysql. (commit ac13691 by mirocow)
## 0.0.5 (2016-03-21)
### Add
* Add default character fore backup data. (commit 6cd7eb5 by mirocow)
* Add default character fore backup data. (commit 0586125 by mirocow)
* Add default character fore backup data. (commit 33c5791 by mirocow)
* Add Apache back-end support. (commit 6ecc220 by mirocow)
### Update
* Update backup_db.sh. (commit d42db14 by mirocow)
* Update backup.sh. (commit 14041fb by mirocow)
* Update restore_db.sh. (commit dbb56b4 by mirocow)
* Update restore.sh. (commit 73fcf08 by mirocow)
### Fix
* Fixed bugs with default charset. (commit 40266a4 by mirocow)
* Fix bad previous commit. (commit 438c05c by mirocow)
* Fix if table is empty. (commit e2efabe by mirocow)
* Fix if table is empty. (commit 8f54453 by mirocow)
### Other
* Remove bad commit. (commit fd432fb by mirocow)
* =fix load data from source file with charset cp1251. (commit c635e3b by Mirocow)
* Dont delete source file. (commit 719abd0 by mirocow)
* Don`t delete source file. (commit 024da4c by mirocow)
## 0.0.4 (2016-02-16)
### Update
* Update backup_db.sh. (commit 194b577 by mirocow)
* Update restore_db.sh. (commit c308b1b by mirocow)
### Fix
* Fixed delete segment files. (commit 60303b7 by mirocow)
* Fixed delete segment files. (commit cf43e79 by mirocow)
* Fixed typo. (commit 3dc75d5 by mirocow)
## 0.0.3 (2016-02-16)
### Update
* Update restore.sh. (commit 971c2d0 by mirocow)
* Update restore_db.sh. (commit 9b0bdb0 by mirocow)
## 0.0.2 (2016-02-16)
### Add
* Add check if debian os. (commit b74f33f by mirocow)
* Add check shell. (commit 4d4fc1a by mirocow)
### Update
* Update restore_db.sh. (commit 3cf1be7 by mirocow)
* Update restore_db.sh. (commit be23cdb by mirocow)
* Update restore.sh. (commit fe69208 by mirocow)
* Update restore.sh. (commit e85bdf8 by mirocow)
* Update restore.sh. (commit 15d4283 by mirocow)
* Update restore.sh. (commit d23f850 by mirocow)
* Update backup.sh. (commit 082ebca by mirocow)
* Update restore.sh. (commit c6431c0 by mirocow)
* Update restore.sh. (commit 488c209 by mirocow)
* Update backup.sh. (commit 81fc75d by mirocow)
* Update restore_db.sh. (commit 728a1c6 by mirocow)
* Update restore_db.sh. (commit 7c9b46e by mirocow)
* Update backup_db.sh. (commit 4c8fd75 by mirocow)
* Update backup.sh. (commit a0b772c by mirocow)
* Update backup_db.sh. (commit 84da1b1 by mirocow)
* Update backup.sh. (commit dd2a1b3 by mirocow)
* Update restore.sh. (commit 3dd4b76 by mirocow)
* Update restore_db.sh. (commit 7afff39 by mirocow)
* Update README.md. (commit 2abca96 by mirocow)
* Update backup_db.sh. (commit 05c4948 by mirocow)
* Update backup.sh. (commit cb49829 by mirocow)
* Update backup_db.sh. (commit 2944087 by mirocow)
* Update backup.sh. (commit 37a09e9 by mirocow)
* Update backup_db.sh. (commit d76a2ac by mirocow)
* Update backup.sh. (commit 1f97f5d by mirocow)
* Update backup.sh. (commit 8f46f81 by mirocow)
* Update backup.sh. (commit efe163a by mirocow)
* Update restore_db.sh. (commit 3d617f6 by mirocow)
* Update restore.sh. (commit 32447b4 by mirocow)
* Update backup.sh. (commit 2c98f9a by mirocow)
* Update backup.sh. (commit 427c725 by mirocow)
* Update backup.sh. (commit 2044e32 by mirocow)
fixed params
* Update backup.sh. (commit e2eda7e by mirocow)
add mysqldump flags
* Update backup.sh. (commit c2cb0f8 by mirocow)
fixed path to error log file
* Update backup.sh. (commit c7a1974 by mirocow)
mysqldump params fixed
* Update backup.sh. (commit 1694412 by mirocow)
add nodata filter --exclude-data-tables (fixed)
* Update backup.sh. (commit 04a842a by mirocow)
split file attributes and directory attributes
* Update backup.sh. (commit 73162b0 by mirocow)
replace configuration variables into the top
* Update backup.sh. (commit 4cd8cfd by mirocow)
* Update backup.sh. (commit 0fa7250 by mirocow)
+ add flag exclude data tables
+ add chmod and chown operation with sql dump and compresed files
* Update backup.sh. (commit 8e0a123 by mirocow)
* Update backup.sh. (commit eeea0fd by mirocow)
* Update backup.sh. (commit ead3f36 by mirocow)
* Update backup.sh. (commit 8a478eb by mirocow)
* Update backup.sh. (commit f2011dc by mirocow)
* Update backup.sh. (commit 84f978e by mirocow)
* Update backup.sh. (commit a4a8619 by mirocow)
add chmod && chown commands
* Update README.md. (commit 0b52962 by mirocow)
* Update README.md. (commit bf32098 by mirocow)
* Update backup.sh. (commit cb334ef by mirocow)
* Update backup.sh. (commit 4812068 by mirocow)
* Update backup.sh. (commit 1e00862 by mirocow)
* Update backup.sh. (commit f9e7d22 by mirocow)
* Update backup.sh. (commit 17a0d2a by mirocow)
add exclude tables
* Update backup.sh. (commit 6c35e45 by mirocow)
* Update backup.sh. (commit c5ca85f by mirocow)
* Update backup.sh. (commit ea20cf1 by mirocow)
* Update backup.sh. (commit 6018645 by mirocow)
* Update backup.sh. (commit 727b570 by mirocow)
* Update backup.sh. (commit f6e3e22 by mirocow)
* Update backup.sh. (commit 2df24b4 by mirocow)
* Update backup.sh. (commit 71b85a7 by mirocow)
* Update backup.sh. (commit 96d2aa7 by mirocow)
* Update backup.sh. (commit fcd5767 by mirocow)
* Update restore_db.sh. (commit f4b3fc8 by mirocow)
* Update restore_db.sh. (commit 884f6e9 by mirocow)
* Update backup.sh. (commit c2e3bf0 by mirocow)
* Update README.md. (commit 3cbf44e by Mirocow)
* Update README.md. (commit fb3f982 by Mirocow)
* Update README.md. (commit 2ed63fb by Mirocow)
* Update README.md. (commit b1239eb by Mirocow)
upgrade cron descriptions
* Update README.md. (commit 8f247f8 by Mirocow)
* Update backup.sh. (commit 86d0ea7 by Mirocow)
* Update debian.cnf. (commit 7dd188f by Mirocow)
* Update README.md. (commit 68cba63 by Mirocow)
* Update README.md. (commit 6474de5 by Mirocow)
* Update README.md. (commit c3ff1e2 by Mirocow)
* Update README.md. (commit 60e6729 by Mirocow)
* Update README.md. (commit c84c5e1 by Mirocow)
* Update README.md. (commit 93af490 by Mirocow)
* Update README.md. (commit 3bb2452 by Mirocow)
* Update README.md. (commit 1c7db08 by Mirocow)
### Fix
* Fixes all errors. (commit 2b09c15 by mirocow)
* Fix path for restore table. (commit 32d15cc by mirocow)
* Fix segment (rm and load) (commit a221546 by mirocow)
* Fix variable for config file. (commit 9c509c6 by mirocow)
* Fix MySQL import csv file ERROR 13 (HY000): Can't get stat of /path/file.csv (Errcode: 2) (commit c2e87ac by mirocow)
* Fix MySQL import csv file ERROR 13 (HY000): Can't get stat of /path/file.csv (Errcode: 2) (commit f6e44dd by mirocow)
fix such http://dba.stackexchange.com/questions/55960/mysql-import-csv-file-error-13-hy000-cant-get-stat-of-path-file-csv-errcod
### Other
* Release. (commit f6678aa by mirocow)
* Release. (commit 1d573a6 by mirocow)
* Segment path fixed. (commit afd2c06 by mirocow)
* Segment path fixed. (commit 116511a by mirocow)
* Segment name try fixes. (commit 5b668dc by mirocow)
* Skip files with part _part_ (segment) (commit 1147cce by mirocow)
* Segment fixed. (commit 6b9f6fa by mirocow)
* Check errors. (commit db9e056 by mirocow)
* Some fixes. (commit faee180 by mirocow)
* Some fixes. (commit e331888 by mirocow)
* Some fixes. (commit ea01afb by mirocow)
* =Add split file for fast restore db; fixed path; check scripts on macos. (commit ee7c575 by Mirocow)
* Speed fixed. (commit f812f1c by mirocow)
* Speed fixed. (commit 8566d75 by mirocow)
* Create backup_db.sh. (commit eee6d2b by mirocow)
* Extented functions. (commit 8095c2c by mirocow)
Add many options such
Options:
-e= | --exclude= Exclude databases
-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
Example:
backup.sh --verbose --compress=
backup.sh --verbose --compress=zgip
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"
* =Add usage default descriptions. (commit 733592b by mirocow)
* =Add usage default descriptions. (commit 0d79392 by mirocow)
* =Add switch compress engine or disabled it. (commit 73e86dd by mirocow)
* =Fixed long params. (commit ae351ae by root)
* =Add additional long params. (commit 1d7fce8 by root)
* Rename restore_table.sh to restore_db.sh. (commit 80e08c3 by Mirocow)
* Create .gitignore. (commit 3b7b584 by Mirocow)
* Create debian.cnf. (commit 95c8ffe by Mirocow)
* Скрипт восстанавливает таблицу в папке в которой был запущен. (commit 04fd4e4 by Mirocow)
## 0.0.1 (2013-07-07)
### Update
* Update some fixes. (commit 670e2ca by Mirocow)
### Other
* Реализовано сохранение дампа таблиц БД в репозиторий Реализована восстановление БД за указанную в хранилище дату Реализован пропуск сохраняемы и восстанавливаемых БД по ключу -e. (commit 0147bdb by Mirocow)
* Initial commit. (commit 1a98233 by Mirocow)

View File

@ -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
======
cd ~
git clone https://github.com/Mirocow/mysql_utils.git
cd mysql_utils
git clone https://forge.leslibres.org/Knah-Tsaeb/DiBaReMy.git
cd DiBaReMy
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
======
cd ~
cd mysql_utils
bash backup.sh
sh backup.sh
Backup selected database
======
cd ~
cd mysql_utils
bash backup_db.sh <[database-name]>
sh backup.sh -d=[database-name]
Restore for selected date
=======
cd /var/backups/mysql/[some date]
bash ~/mysql_utils/restore.sh
sh backups/[yyyy.mm.dd]/restore.sh
Restore selected DB
=======
cd /var/backups/mysql/[some date]/[some db name]
bash ~/mysql_utils/restore_db.sh
sh backups/[yyyy.mm.dd]/restore.sh -d=[database-name]
Automation backup with Cron
===========================
nano /etc/default/db_backup
START=yes
@ -74,9 +87,7 @@ Check work
Tested on
==========
Debiad
FreeBsd
Ubuntu
Arch Linux
Help
===========
@ -85,11 +96,12 @@ Help
# bash backup.sh --help
usage: backup.sh options
This script buckup all databases.
This script backup all databases.
Usage: backup.sh <[options]>
Options:
-d= | --database Database name
-e= | --exclude= Exclude databases
--exclude-tables= Exclude tables
-c= | --compress= Compress with gzip or bzip2

364
backup.sh
View File

@ -1,15 +1,16 @@
#!/bin/bash
# === CONFIG ===
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'
if test -f etc/config.cfg ; then
. etc/config.cfg
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 ===
@ -23,10 +24,10 @@ f_log()
local red=$(tput setf 4)
local green=$(tput setf 2)
local reset=$(tput sgr0)
local toend=$(tput hpa $(tput cols))$(tput cub 6)
local toend=$(tput hpa $(tput cols))$(tput cub 6)
logger "BACKUP: $@"
if [ $VERBOSE -eq 1 ]; then
echo "BACKUP: $@"
fi
@ -48,132 +49,250 @@ prepaire_skip_expression()
backup()
{
f_log " START "
query="SHOW databases;"
local default_databases_exclude=(
'information_schema'
'performance_schema'
'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
if [ -z ${DATABASE} ]; then
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 $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 --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"
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
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/$BDD/__views.sql ]; then
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 $BDD 2>> $DST/$BDD/error.log | sed -e 's/DEFINER=[^*]*\*/\*/' > $DST/$BDD/__routines.sql
if [ -f $DST/$BDD/__routines.sql ]; then
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'
'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"
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-file=$CONFIG_FILE --skip-column-names -B $BDD -e "$query" | egrep -v "$tables_exclude_expression"); do
f_log " ** Dump $BDD.$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/$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
# 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')\""
# 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/$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
if [ ! -z "$hasGeo" ]; then
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
mysqldump --defaults-file=$CONFIG_FILE --default-character-set=utf8 --add-drop-table --quick --tab=$DST/$DATABASE/ $DATABASE $TABLE 2>> $DST/$DATABASE/_error.log
fi
else
f_log " ** WARNING : $DST/$BDD/$TABLE.txt not found"
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
cp restore.sh $DST/
f_log " END "
}
usage()
{
cat << EOF
This mysql backup engine.
This script backup all database in backups directory or single database.
Usage: $0 <[options]> or bash $0 <[options]>
Options:
-d= | --database Database name
-e= | --exclude= Exclude databases
--exclude-tables= Exclude 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="/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
DATABASE=''
TABLES=''
EXCLUDE_DATABASES=''
EXCLUDE_TABLES=''
EXCLUDE_DATA_TABLES=''
@ -218,15 +334,17 @@ for BIN in $BIN_DEPS; do
fi
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 "$@"
do
case $i in
-d=* | --database=*)
DATABASE=( "${i#*=}" )
shift # past argument=value
;;
-t=* | --tables=*)
TABLES=( "${i#*=}" )
shift # past argument=value
;;
-e=* | --exclude=*)
EXCLUDE_DATABASES=( "${i#*=}" )
shift # past argument=value
@ -238,7 +356,7 @@ do
--exclude-data-tables=*)
EXCLUDE_DATA_TABLES=( "${i#*=}" )
shift # past argument=value
;;
;;
-c=* | --compress=*)
COMPRESS=( "${i#*=}" )
shift # past argument=value
@ -264,7 +382,9 @@ do
exit
;;
*)
# unknown option
printf "\n Unknown parameter => $i \n\n"
exit
# unknown option
;;
esac
done
@ -277,11 +397,11 @@ DSTOLD=$BACKUP_DIR/$DATEOLD
if [ ! -d "$DST" ]; then
mkdir -p $DST;
chmod $DIRECTORYATTRIBUTES $DST;
chown $USER:$GROUP $DST;
chown $USER $DST;
fi
if [ -d "$DSTOLD" ]; then
rm -fr $DSTOLD;
rm -r $DSTOLD;
fi
# === SETTINGS ===
@ -290,6 +410,8 @@ 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 databases: $EXCLUDE_DATABASES"
f_log "Exclude tables: $EXCLUDE_TABLES"
f_log "Life time: $TIME_REMOVED_DUMP_FILES"

View File

@ -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
backups/.gitignore vendored
View File

@ -1 +0,0 @@
*

27
etc/config.cfg.example Normal file
View File

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

View File

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

View File

@ -0,0 +1,5 @@
[client]
host = localhost
user = root
password = password
socket = /usr/local/var/run/mysqld.sock

View File

@ -1,9 +1,16 @@
#!/bin/bash
# === CONFIG ===
CONFIG_CHUNK=1000000
VERBOSE=0
if test -f etc/config.cfg ; then
. etc/config.cfg
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 ===
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 ===
database_exists()
{
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
if [ "$RESULT" == "$@" ]; then
echo YES
else
echo NO
fi
RESULT=`mysqlshow --defaults-file=$CONFIG_FILE $@| grep -v Wildcard | grep -o $@`
if [ "$RESULT" == "$@" ]; then
echo YES
else
echo NO
fi
}
f_log()
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)
local toend=$(tput hpa $(tput cols))$(tput cub 6)
logger "RESTORE: $@"
if [ $VERBOSE -eq 1 ]; then
echo "RESTORE: $@"
fi
fi
}
restore()
{
DIR=$@
f_log "** START **"
f_log "Check path $DIR"
f_log "** START **"
f_log "Check runtime"
for BIN in $BIN_DEPS; do
which $BIN 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
f_log "Error: Required file could not be found: $BIN"
exit 1
fi
done
f_log "Check backups folder"
if [ "$(ls -1 $DIR/*/__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/*); 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
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;
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
f_log "Check path $DIR"
f_log "Database $DATABASE"
f_log "Check runtime"
for BIN in $BIN_DEPS; do
which $BIN 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
f_log "Error: Required file could not be found: $BIN"
exit 1
fi
done
f_log "Check backups folder"
if [ "$(ls -1 $DIR/*/__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"
exit 1
fi
if [ -z ${DATABASE} ]; then
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
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
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 [ -s "$DIR/$BDD/$TABLE.txt" ]; then
f_log "+ $TABLE"
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 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/$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 [ -s "$DIR/$BDD/$TABLE.txt" ]; then
f_log "+ $TABLE"
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
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
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-file=$CONFIG_FILE -e "flush privileges;"
f_log "** END **"
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
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
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
else
BDD=$DATABASE
DIR=$DIR/$DATABASE
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()
{
cat << EOF
This script restore all databases in backups directory or single database.
usage: $0 options
This script restore databases.
OPTIONS:
-e Exclude databases
-s Selected databases
-c Check innochecksum of table after import
-d= | --database Database name
-e Exclude databases
-s Selected databases
-c Check innochecksum of table after import
Examples:
backup.sh -v -d=databaseName
EOF
}
@ -192,12 +295,6 @@ 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
@ -209,18 +306,18 @@ done
for i in "$@"
do
case $i in
-e)
DATABASES_SKIP=( "${i#*=}" )
shift
;;
-s)
DATABASES_SELECTED=( "${i#*=}" )
shift
;;
-c)
DATABASES_TABLE_CHECK=1
shift
;;
-e)
DATABASES_SKIP=( "${i#*=}" )
shift
;;
-d=* | --database=*)
DATABASE=( "${i#*=}" )
shift # past argument=value
;;
-c)
DATABASES_TABLE_CHECK=1
shift
;;
--config=*)
CONFIG_FILE=( "${i#*=}" )
shift # past argument=value
@ -228,7 +325,7 @@ do
--chunk=*)
CONFIG_CHUNK=( "${i#*=}" )
shift # past argument=value
;;
;;
-v | --verbose)
VERBOSE=1
shift # past argument=value
@ -238,7 +335,7 @@ do
exit
;;
*)
# unknown option
# unknown option
;;
esac
done
@ -248,9 +345,11 @@ 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 "Selected databases: $DATABASE"
f_log "============================================"
f_log ""
# === AUTORUN ===
restore $(pwd)
time restore
f_log "$@"

View File

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