#!/bin/sh # # MySQL backup script # Set authorisation and host paramethers in homedir .my.cnf param. Read README.md # # dnf install https://repo.percona.com/yum/percona-release-latest.noarch.rpm # dnf install percona-xtrabackup-80.x86_64 # dnf install qpress # # xtrabackup --decompress --target-dir=/data/compressed/ # usage() { echo "Usage: mysql_xtrabackup.sh -d /var/backup -n daily [-c 10 -s --compress --compress-threads=6 -e test@domain.org]" echo echo "-d | --dir :: backup directory" echo "-n | --name :: backup name" echo "-c | --copies :: number of copies to store (default: 10)" echo "-e | --email :: notification email" echo "-i | --ignore-database" echo "--compress-threads :: number of worker threads to compress. (default: 2)" echo "-q | --quiet :: silent mode" echo "--pid-file :: pid file default ${pidfile}" echo "-h | --help :: display this help" } error() { echo -e "\033[0;31m${1}\e[00m" ; if ! [ "${email}" = "" ] ; then echo "ERROR: `hostname`, ${script}: ${1}" | mail -s "'ERROR: `hostname`, ${script}: ${1}'" ${email} ; fi } export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin" ; script="$(basename "$(test -L "${0}" && readlink "${0}" || echo "${0}")")" ; pidfile="/tmp/`basename ${0}`.pid" ; while [ "${1}" != "" ]; do case ${1} in -d | --dir ) shift dir=${1} ; ;; -n | --name ) shift name=${1} ; ;; -c | --copies ) shift copies=${1} ; ;; -e | --email ) shift email=${1} ; ;; -i | --ignore-database ) shift ignore_database=${1} ; ;; --compress-threads ) shift compress_threads=${1} ;; -q | --quiet ) quiet=1 ;; --pid-file ) shift pidfile=${1} ;; -h | --help ) usage ; exit 0; ;; * ) echo "Uncnown option ${1}!" ; usage ; exit 1 ; esac shift done if [ "${copies}" = "" ] || [ ! -n "${copies}" ] || [ "${copies}" -le "0" ] ; then copies=10 ; fi if [ "${compress_threads}" = "" ] || [ ! -n "${compress_threads}" ] || [ "${compress_threads}" -lt "0" ] ; then compress_threads=2 ; fi if [ "${dir}" = "" ] || [ ! -d ${dir} ] ; then error "Directory '${dir}' does not exists!" ; exit 1 ; fi # sleep random 1-6 sec for crone sleep `shuf -i0-9 -n1` ; if [ -s ${pidfile} ] ; then error "ERROR: `hostname` script ${script} already running! Pid file \"${pidfile}\" exists!" ; exit 1 ; fi trap "rm -f ${pidfile} ;" EXIT INT KILL TERM SIGKILL SIGTERM SIGHUP ERR ; echo $$ > ${pidfile} ; prefix="mysql_xtrabackup.`hostname -s`.${name}"; if [ `ls -t ${dir} | grep ${prefix} | wc -l` -ge "${copies}" ] ; then i=1 ; for filename in `ls ${dir} | grep ${prefix} | sort -r` ; do if [ "${i}" -ge "${copies}" ] ; then rm -fr "${dir}/${filename}" ; fi i=$(expr $i + 1) ; done fi if [ ! "${quiet}" ] ; then echo "Starting database dump (`date +\"%H:%M:%S\"`)" ; fi dump_file_name="`realpath ${dir}`/${prefix}.`date +\"%y%m%d.%H%M%S\"`" ; xtrabackup --backup --compress --rsync --slave-info --compress-threads=${compress_threads} --databases-exclude=${ignore_database} --target-dir=${dump_file_name} > /dev/null; if [ ! "${quiet}" ] ; then echo "Dump completed (`date +\"%H:%M:%S\"`)..." ; fi rm -f ${pidfile} ; ## xtrabackup --decompress --target-dir=/data/compressed/