Skip to content
Snippets Groups Projects
Commit aaf1ffa8 authored by root's avatar root
Browse files

Implemented config loading logic. Generate config from template if missing. Minor fixes and changes

parent a39f6ac0
No related branches found
No related tags found
No related merge requests found
[
{
"sqlHost": "localhost",
"sqlPort": "3306",
"sqlUser": "root",
"sqlPass": null,
"dbNames": "all",
"backupRoot": "/var/backups/sql1/",
"backupRetainDays": 30
}
]
[
{
"sqlHost": "localhost",
"sqlPort": "3306",
"sqlUser": "root",
"sqlPass": null,
"dbNames": "all",
"backupRoot": "/var/backups/sql/",
"backupRetainDays": 30
}
]
{
"sqlHost": "localhost",
"sqlPort": "3306",
"sqlUser": "root",
"sqlPass": null,
"dbNames": "all",
"backupRoot": "/var/backups/sql/",
"backupRetainDays": 30
}
{
"sqlHost": "localhost",
"sqlPort": "3306",
"sqlUser": "backup",
"sqlPass": "l)m0h4vtJ@D7*f5YOvLO5c=0g?4K3aEM",
"dbNames": "all",
"backupRoot": "/var/backups/sql/",
"backupRetainDays": 30
}
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)
...@@ -8,20 +8,36 @@ set -o nounset # Treat unset variables as an error ...@@ -8,20 +8,36 @@ set -o nounset # Treat unset variables as an error
######################### #########################
export PATH=/bin:/usr/bin:/usr/local/bin export PATH=/bin:/usr/bin:/usr/local/bin
configFile=/opt/sql-backup/config.json
configTemplate=/opt/sql-backup/config_template.json
configFile=/etc/sql-backup/sql-backup.conf if [[ ! -f $configFile ]]; then
if test -f "configFile"; then echo 'WARN: No configuration file found. Generate file from template.'
echo "INFO: Config file exists, load it --> To be implemented" if [[ ! -f $configTemplate ]]; then
else echo 'ERROR: No configuration template found. Installation might be corrupted. Please run update.sh or' \
echo "WARN: No configuration file found. Attempting to backup all existing Databases as root." 'reclone repository.'
sqlHost="localhost" exit 1
sqlPort="3306" else
sqlUser="root" cp $configTemplate $configFile
sqlPass="" fi
dbNames=($(mysql -u root -e "SHOW DATABASES;"))
backupRoot="/var/backups/sql/"
backupRetainDays=30
fi fi
echo 'INFO: Load config file.'
sqlHost=$(cat $configFile | jq -r '.[0].sqlHost')
sqlPort=$(cat $configFile | jq -r '.[0].sqlPort')
sqlUser=$(cat $configFile | jq -r '.[0].sqlUser')
sqlPass=$(cat $configFile | jq -r '.[0].sqlPass')
dbNames=$(cat $configFile | jq -r '.[0].dbNames')
if [ $dbNames == 'all' ]; then
if [ -z $sqlPass ]; then
dbNames=($(mysql -h$sqlHost -P$sqlPort -u$sqlUser -e "SHOW DATABASES;" -s --skip-column-names))
else
dbNames=($(mysql -h$sqlHost -P$sqlPort -u$sqlUser -p$sqlPass -e "SHOW DATABASES;" -s --skip-column-names))
fi
fi
backupRoot=$(cat $configFile | jq -r '.[0].backupRoot')
backupRetainDays=$(cat $configFile | jq -r '.[0].backupRetainDays')
today=$(date +"%y%m%d") today=$(date +"%y%m%d")
######################### #########################
...@@ -44,22 +60,18 @@ else ...@@ -44,22 +60,18 @@ else
} }
fi fi
for dbName in "${dbNames[@]}"; for dbName in "${dbNames[@]}";
do do
if [ $dbName == "Database" ] || \ if [ $dbName == 'information_schema' ] || \
[ $dbName == "information_schema" ] || \ [ $dbName == 'performance_schema' ] || \
[ $dbName == "mysql" ] || \ [ $dbName == 'mysql' ] || \
[ $dbName == "performance_schema" ] || \ [ $dbName == 'phpmyadmin' ]; then
[ $dbName == "phpmyadmin" ];
then
continue continue
fi fi
backupPath=$backupRoot$dbName backupPath=$backupRoot$dbName
mkdir -p ${backupPath} mkdir -p ${backupPath}
cd $backupPath
if [ -z $sqlPass ]; if [ -z $sqlPass ];
then then
...@@ -81,7 +93,7 @@ do ...@@ -81,7 +93,7 @@ do
delDate=$(date +"%y%m%d" --date="${backupRetainDays} days ago") delDate=$(date +"%y%m%d" --date="${backupRetainDays} days ago")
delFile=${dbName}-${delDate}.sql delFile=${dbName}-${delDate}.sql
for f in *; do for f in $backupPath/*; do
if [[ "$f" < "$delFile" ]]; then if [[ "$f" < "$delFile" ]]; then
echo "delete $f" echo "delete $f"
rm -f $f rm -f $f
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment