Last active
March 1, 2022 15:44
-
-
Save alainwolf/f197d8aa0facab410052c80c0305cb1c to your computer and use it in GitHub Desktop.
MariaDB Backup Scrip to be run on Synology NAS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ****************************************************************************** | |
# MariaDB Backup Scrip to be run on Synology NAS | |
# (MariaDB 10.3.29-1038 package published by Synology Inc.) | |
# | |
# * Full online physical backups with mariabackup: | |
# - no databse downtime (as with physical copy or HyperBackup) | |
# - database caches are not affected (as with SQL-dumps) | |
# * Ensure only one single instance is running at same time | |
# * Ensure at least one successful full backup is present at all times | |
# | |
# See https://mariadb.com/kb/en/mariabackup-overview/ | |
# | |
# Prerequisites: | |
# ln -s /volume1/@appstore/MariaDB10/usr/local/mariadb10/bin/mariabackup \ | |
# /usr/local/bin/ | |
# | |
# CREATE USER 'mariabackup'@'localhost' IDENTIFIED BY 'change_me'; | |
# GRANT RELOAD, PROCESS, LOCK TABLES, REPLICATION CLIENT ON *.* \ | |
# TO 'mariabackup'@'localhost'; | |
# GRANT CREATE ON PERCONA_SCHEMA.* TO 'mariabackup'@'localhost'; | |
# FLUSH PRIVILEGES; | |
# | |
# Make sure app-data backup for MariaSB in HyperBackup is disabled! | |
# Make sure your backup-root directory is included by HyperBackup! | |
# | |
# ****************************************************************************** | |
# --------------------------------------- | |
# Settings | |
# --------------------------------------- | |
_BACKUP_ROOT="/var/services/homes/admin/mariabackup" | |
_SYSTEM_USER="mysql" | |
_DATABASE_USER="mariabackup" | |
_DATABASE_PASSWORD="change_me" | |
# --------------------------------------- | |
# Abort on any error or undefined variable | |
set -e -u | |
_lock_file="/tmp/mariabackup.lock" | |
_log="${_BACKUP_ROOT}/mariabackup.log" | |
_error_log="${_BACKUP_ROOT}/mariabackup.err" | |
_success_file="completed.ok" | |
_full_1="${_BACKUP_ROOT}/full-1" | |
_full_2="${_BACKUP_ROOT}/full-2" | |
# Am I the one? | |
if [[ $( whoami ) != "$_SYSTEM_USER" ]]; then | |
printf "This script is supposed to run by the \"%s\" user!\nAborting!\n" \ | |
$_SYSTEM_USER | |
exit 1 | |
fi | |
# --------------------------------------- | |
# Lock-file | |
# --------------------------------------- | |
# Redirect file descriptor 100 to our lock file until end of this script, | |
# or exit with error if that fails (because its locked by another process). | |
if ! exec 100>$_lock_file ; then | |
printf "Redirection to %s failed!\nIs another instance running?\n" $_lock_file | |
exit 1 | |
fi | |
# Obtain a lock on our redirected file descriptor 100 | |
# or exit immediately with error if no lock can be obtained. | |
if ! flock -n 100 ; then | |
printf "Obtaining lock on fd failed!\nIs another instance running?\n" | |
exit 1 | |
fi | |
# --------------------------------------- | |
# Look for previous successfull backups | |
# --------------------------------------- | |
# Look for full-1 | |
if [ -f "${_full_1}/${_success_file}" ]; then | |
printf "File \"%s\" exists.\n" "${_full_1}/${_success_file}" | |
else | |
# Not found or presumably not complete: delete this dirctory! | |
rm -rf "$_full_1" | |
# We will backup to full-1 | |
_target_dir=$_full_1 | |
fi | |
# Look for full-2 | |
if [ -f "${_full_2}/${_success_file}" ]; then | |
printf "File \"%s\" exists\n" "${_full_2}/${_success_file}" | |
else | |
# Not found or presumably not complete: delete this dirctory! | |
rm -rf "$_full_2" | |
# We will backup to full-2 | |
_target_dir=$_full_2 | |
fi | |
# --------------------------------------- | |
# Delete older one of the two | |
# --------------------------------------- | |
# Is this one is older as the other | |
if [ "$_full_1" -ot "$_full_2" ]; then | |
# Delete older directory | |
rm -rf "$_full_1" | |
# We will backup to full-1 | |
_target_dir=$_full_1 | |
else | |
# Delete older directory | |
rm -rf "$_full_2" | |
# We will backup to full-2 | |
_target_dir=$_full_2 | |
fi | |
# --------------------------------------- | |
# Do the backup | |
# --------------------------------------- | |
printf "Starting MariaDB Backup to %s ...\n" $_target_dir | |
mariabackup --backup --target-dir="${_target_dir}" \ | |
--user=${_DATABASE_USER} --password=${_DATABASE_PASSWORD} \ | |
> "$_log" 2> "$_error_log" | |
printf "MariaDB Backup completed to.%s \n" $_target_dir \ | |
tee "${_target_dir}/${_success_file}" | |
# -*- mode: bash; indent-tabs-mode: nil; tab-width: 4; -*- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment