From 019fa8b9e3e14530380781d2cfd35728f670e459 Mon Sep 17 00:00:00 2001 From: root <root@sdvdbodsql01.slub-dresden.de> Date: Mon, 8 Aug 2022 10:13:50 +0200 Subject: [PATCH] Complete rewrite of backup script. Extend logic on standard config (for localhost, as root, no password, all databases). --- README.md | 2 - config.json | 0 config_template.json | 0 .../sql-backup.service | 2 +- .../sql-backup.timer | 0 sql-backup-sample.conf | 9 ++ sql-backup.conf | 9 ++ sql-backup.py | 52 ++++++++ sql-backup.sh | 111 +++++++++++++----- 9 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 config.json create mode 100644 config_template.json rename sql-backup.service => service-unit/sql-backup.service (87%) rename sql-backup.timer => service-unit/sql-backup.timer (100%) create mode 100644 sql-backup-sample.conf create mode 100644 sql-backup.conf create mode 100755 sql-backup.py diff --git a/README.md b/README.md index 4f05d9e..2a00b8f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ - - # Automated SQL Backup Script A simple bash script, to automatically create sql dumps on a daily basis, scheduled by systemd. diff --git a/config.json b/config.json new file mode 100644 index 0000000..e69de29 diff --git a/config_template.json b/config_template.json new file mode 100644 index 0000000..e69de29 diff --git a/sql-backup.service b/service-unit/sql-backup.service similarity index 87% rename from sql-backup.service rename to service-unit/sql-backup.service index 7fc1ca3..bbd9977 100644 --- a/sql-backup.service +++ b/service-unit/sql-backup.service @@ -4,7 +4,7 @@ Wants=sql-backup.timer [Service] Type=oneshot -ExecStart=./sql-backup.sh +ExecStart= [Install] WantedBy=multi-user.target diff --git a/sql-backup.timer b/service-unit/sql-backup.timer similarity index 100% rename from sql-backup.timer rename to service-unit/sql-backup.timer diff --git a/sql-backup-sample.conf b/sql-backup-sample.conf new file mode 100644 index 0000000..5cc4c12 --- /dev/null +++ b/sql-backup-sample.conf @@ -0,0 +1,9 @@ +{ + "sqlHost": "localhost", + "sqlPort": "3306", + "sqlUser": "root", + "sqlPass": null, + "dbNames": "all", + "backupRoot": "/var/backups/sql/", + "backupRetainDays": 30 +} diff --git a/sql-backup.conf b/sql-backup.conf new file mode 100644 index 0000000..edd8729 --- /dev/null +++ b/sql-backup.conf @@ -0,0 +1,9 @@ +{ + "sqlHost": "localhost", + "sqlPort": "3306", + "sqlUser": "backup", + "sqlPass": "l)m0h4vtJ@D7*f5YOvLO5c=0g?4K3aEM", + "dbNames": "all", + "backupRoot": "/var/backups/sql/", + "backupRetainDays": 30 +} diff --git a/sql-backup.py b/sql-backup.py new file mode 100755 index 0000000..07de054 --- /dev/null +++ b/sql-backup.py @@ -0,0 +1,52 @@ +import os +import json +import subprocess +import mysql.connector + +######################### +### ### +### SETTINGS ### +### ### +######################### + +try: + configFile = open('/opt/sql-backup/sql-backup.conf') + print('INFO: File exists, load it --> To be implemented') +except: + print('WARN: No configuration file found. Attempting to backup all existing Databases as root.') + configFile = open('/opt/sql-backup/sql-backup-sample.conf') + +config = json.load(configFile) +configFile.close() + +sqlHost = config['sqlHost'] +sqlPort = config['sqlPort'] +sqlUser = config['sqlUser'] +sqlPass = config['sqlPass'] +dbNames = config['dbNames'] +backupRoot = config['backupRoot'] +backupRetainDays = config['backupRetainDays'] + +######################### +### ### +### START OF SCRIPT ### +### ### +######################### + +isDir = os.path.isdir(backupRoot) +if isDir == False: + print('WARN: Backup root directory does not exist. Try to create it.') + try: + os.mkdir(backupRoot) + except: + print('ERROR: Could not create backup root directory. Stopping backup process') + quit() + +connection = mysql.connector.connect(host = sqlHost, port = sqlPort, + user = sqlUser, password = sqlPass) +cursor = connection.cursor() +cursor.execute('SHOW DATABASES;') +dbNames = [] +for record in cursor.fetchall(): + dbNames.append(record[0]) +print(dbNames) diff --git a/sql-backup.sh b/sql-backup.sh index 005f0ef..2250c84 100755 --- a/sql-backup.sh +++ b/sql-backup.sh @@ -1,41 +1,96 @@ -#!/bin/bash +#!/usr/bin/env bash +set -o nounset # Treat unset variables as an error + +######################### +### ### +### SETTINGS ### +### ### +######################### + export PATH=/bin:/usr/bin:/usr/local/bin +configFile=/etc/sql-backup/sql-backup.conf +if test -f "configFile"; then + echo "INFO: Config file exists, load it --> To be implemented" +else + echo "WARN: No configuration file found. Attempting to backup all existing Databases as root." + sqlHost="localhost" + sqlPort="3306" + sqlUser="root" + sqlPass="" + dbNames=($(mysql -u root -e "SHOW DATABASES;")) + backupRoot="/var/backups/sql/" + backupRetainDays=30 +fi today=$(date +"%y%m%d") -sqlHost="localhost" -sqlPort="3306" -sqlUser="backup" -sqlPass="l)m0h4vtJ@D7*f5YOvLO5c=0g?4K3aEM" -dbName="booked_prod" -backupPath="/var/backups/sql/"$dbName -backupRetainDays=30 - -mkdir -p ${backupPath} -cd $backupPath - -mysqldump -h ${sqlHost} \ - -P${sqlPort} \ - -u${sqlUser} \ - -p${sqlPass} \ - ${dbName} > ${backupPath}/${dbName}-${today}.sql - -if [ $? -eq 0 ]; then - echo "Database backup successfully completed" +######################### +### ### +### START OF SCRIPT ### +### ### +######################### + +# check for backup root directory +if [[ -d "$backupRoot" ]]; +then + echo "INFO: Backup root directory found. Proceeding." else - echo "Error found during backup" + echo "WARN: Backup root directory does not exist. Try to create it." + { # try + mkdir $backupRoot + } || { + echo "ERROR: Could not create Backup root directory. Stop script." + exit 1 + } fi -delDate=$(date +"%y%m%d" --date="${BACKUP_RETAIN_DAYS} days ago") -delFile=${dbName}-${delDate}.sql -for f in * +for dbName in "${dbNames[@]}"; do - if [[ "$f" < "$delFile" ]] + + if [ $dbName == "Database" ] || \ + [ $dbName == "information_schema" ] || \ + [ $dbName == "mysql" ] || \ + [ $dbName == "performance_schema" ] || \ + [ $dbName == "phpmyadmin" ]; then - echo "delete $f" - rm -f $f + continue + fi + + backupPath=$backupRoot$dbName + mkdir -p ${backupPath} + cd $backupPath + + if [ -z $sqlPass ]; + then + mysqldump -h ${sqlHost} \ + -P${sqlPort} \ + -u${sqlUser} \ + ${dbName} > ${backupPath}/${dbName}_${today}.sql + else + mysqldump -h ${sqlHost} \ + -P${sqlPort} \ + -u${sqlUser} \ + -p${sqlPass} \ + ${dbName} > ${backupPath}/${dbName}_${today}.sql + fi + + if [ $? -eq 0 ]; then + + echo "INFO: Database backup for ${dbName} successfully completed." + delDate=$(date +"%y%m%d" --date="${backupRetainDays} days ago") + delFile=${dbName}-${delDate}.sql + + for f in *; do + if [[ "$f" < "$delFile" ]]; then + echo "delete $f" + rm -f $f + else + echo "keep $f" + fi + done else - echo "keep $f" + echo "ERROR: Backup failed for ${dbName}. Skip deletion process for older Backups." + continue fi done -- GitLab