diff options
author | Thomas Lange <code@nerdmind.de> | 2016-01-09 20:30:00 +0100 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2016-01-09 20:30:00 +0100 |
commit | a6a6cd0bd34a6a4f7c25b901df1bd1d022b1683c (patch) | |
tree | 19e86e489b0c45c0998b778a659bc83263cb110f | |
download | snippets-a6a6cd0bd34a6a4f7c25b901df1bd1d022b1683c.tar.gz snippets-a6a6cd0bd34a6a4f7c25b901df1bd1d022b1683c.tar.xz snippets-a6a6cd0bd34a6a4f7c25b901df1bd1d022b1683c.zip |
Initial commit
-rw-r--r-- | Bash/mysql-database-backup.sh | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Bash/mysql-database-backup.sh b/Bash/mysql-database-backup.sh new file mode 100644 index 0000000..4190fdc --- /dev/null +++ b/Bash/mysql-database-backup.sh @@ -0,0 +1,38 @@ +#!/bin/bash +#=============================================================================== +# Define database login credentials and excluded databases +#=============================================================================== +DATABASE_USERNAME="InsertYourUsernameHere" +DATABASE_PASSWORD="InsertYourPasswordHere" +DATABASE_EXCLUDED="(Database|mysql|information_schema|performance_schema)" + +#=============================================================================== +# Define backup directories and target filename +#=============================================================================== +DIRECTORY_ROOT="/mnt/backups/databases/" +DIRECTORY_PATH="$(date +%Y-%m-%d-%Hh%Mm)/" +DIRECTORY_FILE="${DIRECTORY_ROOT}${DIRECTORY_PATH}%s.sql.bz2" + +#=============================================================================== +# Delete old backups in backup root directory +#=============================================================================== +find "${DIRECTORY_ROOT}" -type d -mtime +30 -exec rm -r {} \; + +#=============================================================================== +# Create backup path directory if not exists +#=============================================================================== +if [ ! -d "${DIRECTORY_ROOT}${DIRECTORY_PATH}" ]; then + mkdir "${DIRECTORY_ROOT}${DIRECTORY_PATH}" +fi + +#=============================================================================== +# Fetch all databases from local MySQL server +#=============================================================================== +DATABASES=`mysql --user="${DATABASE_USERNAME}" --password="${DATABASE_PASSWORD}" --execute "SHOW DATABASES;" | grep -Ev "${DATABASE_EXCLUDED}"` + +#=============================================================================== +# Loop through all databases and create compressed dump +#=============================================================================== +for database in ${DATABASES}; do + mysqldump --user="${DATABASE_USERNAME}" --password="${DATABASE_PASSWORD}" "${database}" | bzip2 > $(printf "${DIRECTORY_FILE}" "${database}") +done
\ No newline at end of file |