Skip to content

Instantly share code, notes, and snippets.

@floreo
Created January 23, 2017 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save floreo/750f7acaf7acc2355923bd6008391caa to your computer and use it in GitHub Desktop.
Save floreo/750f7acaf7acc2355923bd6008391caa to your computer and use it in GitHub Desktop.
mysqldump script using pt-show-grants
#!/usr/bin/env bash
# wget percona.com/get/percona-toolkit.deb
declare -r \
_BACKUP_DIR="/root/backup/" \
_OCCURRENCES=24
# Get the real number of backup occurrences and delete the old ones
function delete () {
if [ -z "${1}" ] ; then
return 1
fi
local _name="${1}"
local _real_occurrences="$(ls "${_BACKUP_DIR}" | grep -c "${_name}")"
if [ "${_real_occurrences}" -ge "${_OCCURRENCES}" ] ; then
ls -tl "${_BACKUP_DIR}"* | grep "${_name}" | awk '{ print $NF }' | tail -$((_real_occurrences - _OCCURRENCES)) | xargs rm
fi
return 0
}
mysql -N -e 'show databases;' | grep -v 'performance_schema' | while read _db;
do
mysqldump --events --single-transaction "${_db}" | gzip -9 -c > "${_BACKUP_DIR}${_db}_$(date +'%F_%T').sql.gz"
delete "${_db}"
done
# Export grants with percona toolkit
pt-show-grants | gzip -9 -c >"${_BACKUP_DIR}grants_$(date +'%F_%T').sql.gz"
delete "grants"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment