From 36d43b7494400c6a0002c9aae4eb61da15cc158c Mon Sep 17 00:00:00 2001 From: naumso Date: Thu, 14 Dec 2023 19:18:18 +0300 Subject: [PATCH] xtrabackup --- mysql_backup.sh | 4 ++ mysql_xtrabackup.sh | 135 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100755 mysql_xtrabackup.sh diff --git a/mysql_backup.sh b/mysql_backup.sh index d32f575..1d1ce07 100755 --- a/mysql_backup.sh +++ b/mysql_backup.sh @@ -42,6 +42,9 @@ mysqlparams=" --all-databases \ --add-drop-database \ --add-drop-table \ --add-drop-trigger \ + --disable-keys \ + --lock-tables \ + --set-charset \ --triggers \ --add-locks \ --create-options \ @@ -59,6 +62,7 @@ mysqlparams=" --all-databases \ --order-by-primary \ --apply-replica-statements \ --dump-date \ + --max-allowed-packet=512MB \ --ignore-table=mysql.slow_log \ --log-error=/tmp/mysqldump.log" ; diff --git a/mysql_xtrabackup.sh b/mysql_xtrabackup.sh new file mode 100755 index 0000000..1dcc93a --- /dev/null +++ b/mysql_xtrabackup.sh @@ -0,0 +1,135 @@ +#!/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 "--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} ; + ;; + --compress-threads ) shift + compress_threads=${1} + ;; + -q | --quiet ) shift + quiet=1 + ;; + --pid-file ) shift + pidfile=${1} + ;; + -h | --help ) shift + 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 "${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 --compress-threads=${compress_threads} --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/ \ No newline at end of file