This commit is contained in:
Naum Soloveychik
2020-01-10 21:47:46 +03:00
parent 6d309751f4
commit a41830a66c
2 changed files with 49 additions and 35 deletions

View File

@@ -1,13 +1,32 @@
# mysql_backup # MySQL backup shell script
MySQL backup shell script MySQL backup shell script
Feature rich MySQL / MariaDB backup script. Feature rich MySQL / MariaDB backup script.
# Setup password ## Usage
**Usage:** `mysql_backup.sh -d /var/backup -n daily [-c 10 -s -a -e test@domain.org]`
- **-d , --dir **
backup directory
- **-n, --name**
backup name
- **-c, --copies**
number of copies to store (default 10)
- **-e, --email**
notification email
- **-l, --lock-all-tables**
- **-s, --single-transaction**
- **-z, --compress**
compress dump by gzip
- **-q, --quiet**
- **-h, --help**
## Setup database password
Set mysql `user` and `password` to user that execute script using `.my.cnf` in user homedir. Set mysql `user` and `password` to user that execute script using `.my.cnf` in user homedir.
# .my.cnf example ## .my.cnf example
``` ```
[client] [client]

View File

@@ -1,12 +1,4 @@
#!/bin/sh #!/bin/sh
# set mysql user and password to user who dump mysql by .my.cnf in user homedir
#
# nano /home/mysqldump/.my.cnf
# [client]
# user = mysqldump
# password = secret_passwd_here_HMnab4sBMmMwtDgvF=qZuuU#gsED9u6J
# host = localhost
#
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin" ; export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin" ;
@@ -16,16 +8,16 @@ mypidfile="/var/run/`basename ${0}`.pid" ;
usage() usage()
{ {
echo "Usage: mysql_backup.sh -d /var/backup -n daily [-c 10 -s -a -e test@domain.org]" echo "Usage: mysql_backup.sh -d /var/backup -n daily [-c 10 -s -a -e test@domain.org]"
echo echo
echo "-d | --dir :: backup directory" echo "-d | --dir :: backup directory"
echo "-n | --name :: backup name" echo "-n | --name :: backup name"
echo "-c | --copies :: number of copies to store (default 10)" echo "-c | --copies :: number of copies to store (default 10)"
echo "-e | --email :: notification email" echo "-e | --email :: notification email"
echo "-l | --lock-all-tables" echo "-l | --lock-all-tables"
echo "-s | --single-transaction" echo "-s | --single-transaction"
echo "-a | --archive :: gzip dump" echo "-z | --compress :: gzip dump"
echo "-q | --quiet" echo "-q | --quiet"
echo "-h help" echo "-h help"
} }
error() error()
@@ -42,23 +34,25 @@ while [ "$1" != "" ]; do
-d | --dir ) shift -d | --dir ) shift
dir=$1 dir=$1
;; ;;
-n | --name ) shift -n | --name ) shift
name=$1 name=$1
;; ;;
-c | --copies ) shift -c | --copies ) shift
copies=$1 copies=$1
;; ;;
-e | --email ) shift -e | --email ) shift
email=$1 email=$1
;; ;;
-l | --lock-all-tables ) lock=1 -l | --lock-all-tables )
lock=1
;; ;;
-s | --single-transaction ) singletrans=1 -s | --single-transaction )
singletrans=1
;; ;;
-a | --archive ) archive=1 -z | --compress ) archive=1
;; ;;
-q | --quiet ) quiet=1 -q | --quiet ) quiet=1
;; ;;
-h | --help ) usage -h | --help ) usage
exit exit
;; ;;
@@ -77,9 +71,9 @@ fi
if [ "${name}" = "" ] ; if [ "${name}" = "" ] ;
then then
usage ; usage ;
error "Name of backup is not set!" error "Name of backup is not set!"
exit 1 ; exit 1 ;
fi fi
if [ "${copies}" = "" ] || [ ! -n "${copies}" ] || [ "${copies}" -le "0" ] ; if [ "${copies}" = "" ] || [ ! -n "${copies}" ] || [ "${copies}" -le "0" ] ;
@@ -95,7 +89,8 @@ if ! [ -d ${dir} ] ; then
fi fi
if [ -s ${mypidfile} ] ; then if [ -s ${mypidfile} ] ; then
error "ERROR: `hostname` script ${script} already running!" ;
error "ERROR: `hostname` script ${script} already running!" ;
exit 1 ; exit 1 ;
fi fi
@@ -108,8 +103,8 @@ mysqlparams=" --all-databases \
--add-drop-table \ --add-drop-table \
--add-drop-trigger \ --add-drop-trigger \
--add-locks \ --add-locks \
--compact \ --compact \
--disable-keys \ --disable-keys \
--apply-slave-statements \ --apply-slave-statements \
--allow-keywords \ --allow-keywords \
--complete-insert \ --complete-insert \
@@ -127,15 +122,15 @@ mysqlparams=" --all-databases \
--routines \ --routines \
--triggers \ --triggers \
--force \ --force \
--max-allowed-packet=512M --max-allowed-packet=512M \
--log-error=/var/log/mysqldump.log" ; --log-error=/var/log/mysqldump.log" ;
if [ "${singletrans}" ] ; then if [ "${singletrans}" ] ; then
mysqlparams="${mysqlparams} --single-transaction" ; mysqlparams="${mysqlparams} --single-transaction" ;
fi fi
if [ "${lock}" ] ; then if [ "${lock}" ] ; then
mysqlparams="${mysqlparams} --lock-all-tables" ; mysqlparams="${mysqlparams} --lock-all-tables" ;
fi fi
if [ `ls ${dir} | grep mysqldump.daily | wc -l` -ge "${copies}" ] ; then if [ `ls ${dir} | grep mysqldump.daily | wc -l` -ge "${copies}" ] ; then
@@ -161,7 +156,7 @@ mysqldump ${mysqlparams} > ${dump_file_name} ;
if [ "${archive}" ] ; then if [ "${archive}" ] ; then
if [ ! "${quiet}" ] ; then if [ ! "${quiet}" ] ; then
echo "Archiving dump (`date +\"%Y-%m-%d %H:%M:%S\"`)..." ; echo "Archiving dump (`date +\"%Y-%m-%d %H:%M:%S\"`)..." ;
fi fi
gzip ${dump_file_name} ; gzip ${dump_file_name} ;