2018-01-31 11:28:03 +00:00
|
|
|
#!/bin/sh
|
2018-01-30 20:57:23 +00:00
|
|
|
|
2018-01-30 21:17:20 +00:00
|
|
|
# Copyright 2018 K. Langenberg. All rights reserved.
|
|
|
|
# Use of this source code is governed by a GPLv3-style
|
|
|
|
# license that can be found in the LICENSE file.
|
2018-01-30 20:57:23 +00:00
|
|
|
|
2018-01-30 21:17:20 +00:00
|
|
|
#########################
|
|
|
|
# Environment variables #
|
|
|
|
#########################
|
|
|
|
|
2018-01-31 10:25:22 +00:00
|
|
|
# Backup folder
|
|
|
|
if [ -n $DB_BACKUP_FOLDER ]
|
|
|
|
then
|
|
|
|
backup_folder=$DB_BACKUP_FOLDER
|
|
|
|
fi
|
2018-01-30 21:17:20 +00:00
|
|
|
|
2018-01-31 10:25:22 +00:00
|
|
|
# Backup hosts file
|
|
|
|
if [ -n $DB_BACKUP_HOSTS_FILE ]
|
|
|
|
then
|
|
|
|
backup_hosts_location=$DB_BACKUP_HOSTS_FILE
|
|
|
|
fi
|
2018-01-30 21:17:20 +00:00
|
|
|
|
2018-01-31 12:39:19 +00:00
|
|
|
# Max backups to keep
|
|
|
|
if [ -n $DB_BACKUP_MAX ]
|
|
|
|
then
|
|
|
|
max_backups=$DB_BACKUP_MAX
|
|
|
|
fi
|
|
|
|
|
2018-01-30 21:17:20 +00:00
|
|
|
########
|
|
|
|
# Passed options, any passed option will overwrite a previously set environment variable
|
|
|
|
########
|
|
|
|
|
2018-01-31 12:39:19 +00:00
|
|
|
while getopts ":b:c:d:h" opt; do
|
2018-01-30 21:17:20 +00:00
|
|
|
case $opt in
|
|
|
|
b)
|
|
|
|
backup_folder=$OPTARG
|
|
|
|
;;
|
|
|
|
c)
|
|
|
|
backup_hosts_location=$OPTARG
|
|
|
|
;;
|
2018-01-31 12:39:19 +00:00
|
|
|
d)
|
|
|
|
max_backups=$OPTARG
|
|
|
|
;;
|
2018-01-30 21:17:20 +00:00
|
|
|
h)
|
2018-01-31 12:41:09 +00:00
|
|
|
echo "Available Options:
|
|
|
|
-b: Folder where to store the backups. Defaults to \$PWD/backups.
|
|
|
|
-c: Location of the config.json file which holds all hosts you want to backup. Defaults to \$PWD/backup_hosts.json.
|
2018-01-31 12:39:19 +00:00
|
|
|
-d: Maximum number of backups to keep. Defaults to 24.
|
2018-01-30 21:17:20 +00:00
|
|
|
-h: Print this help message.
|
|
|
|
|
2018-01-31 10:25:22 +00:00
|
|
|
Environment Variables:
|
|
|
|
- DB_BACKUP_FOLDER: Where to store the backups.
|
|
|
|
- DB_BACKUP_HOSTS_FILE: Location of the config.json file which holds all hosts you want to backup.
|
2018-01-31 12:39:19 +00:00
|
|
|
- DB_BACKUP_MAX: Maximum number of backups to keep. Defaults to 24.
|
2018-01-30 21:17:20 +00:00
|
|
|
|
|
|
|
Copyright 2018 K. Langenberg. All rights reserved.
|
|
|
|
Use of this source code is governed by a GPLv3-style
|
|
|
|
license that can be found in the LICENSE file."
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG. Use -h to print all available options." >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "Option -$OPTARG requires an argument. Use -h to print all available options." >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
############
|
|
|
|
# Defaults #
|
|
|
|
############
|
|
|
|
|
|
|
|
# Backup configuration file
|
|
|
|
if [ -z $backup_hosts_location ]
|
|
|
|
then
|
|
|
|
backup_hosts_location=$PWD/backup_hosts.json
|
|
|
|
fi
|
2018-01-30 20:57:23 +00:00
|
|
|
|
|
|
|
# Backup folder
|
2018-01-30 21:17:20 +00:00
|
|
|
if [ -z $backup_folder ]
|
2018-01-31 13:10:28 +00:00
|
|
|
then
|
2018-01-30 21:17:20 +00:00
|
|
|
backup_folder=$PWD/backups
|
|
|
|
fi
|
|
|
|
|
2018-01-31 12:39:19 +00:00
|
|
|
# Max number of backups to keep
|
|
|
|
if [ -z $max_backups ]
|
2018-01-31 13:10:28 +00:00
|
|
|
then
|
|
|
|
max_backups=24
|
2018-01-31 12:39:19 +00:00
|
|
|
fi
|
|
|
|
|
2018-01-31 10:19:32 +00:00
|
|
|
# Save the date
|
|
|
|
date=`date +%d\-%m\-%Y\_%H\-%M\-%S`
|
2018-01-30 21:17:20 +00:00
|
|
|
|
2018-01-31 10:19:32 +00:00
|
|
|
###############
|
|
|
|
# File Checks #
|
|
|
|
###############
|
2018-01-30 20:57:23 +00:00
|
|
|
|
|
|
|
# Check if the backup folder exists
|
|
|
|
if [ ! -d $backup_folder ]; then
|
|
|
|
echo "Backup folder does not exist, trying to create it..."
|
|
|
|
mkdir -p $backup_folder
|
|
|
|
|
|
|
|
if [ ! $? -eq 0 ]; then
|
|
|
|
echo "Could not create Backup folder. Please make sure you have sufficent rights to create $backup_folder"
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-01-30 21:19:23 +00:00
|
|
|
echo "-------------------------------"
|
2018-01-30 20:57:23 +00:00
|
|
|
fi
|
|
|
|
|
2018-01-30 21:17:20 +00:00
|
|
|
# Check if the config file exists
|
2018-01-31 10:19:32 +00:00
|
|
|
if [ ! -f $backup_hosts_location ]; then
|
|
|
|
echo "Config file $backup_hosts_location does not exist."
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-01-30 21:17:20 +00:00
|
|
|
|
2018-01-31 10:19:32 +00:00
|
|
|
# Load Config
|
2018-01-31 11:28:03 +00:00
|
|
|
backup_hosts_file=$(cat $backup_hosts_location)
|
2018-01-31 10:19:32 +00:00
|
|
|
|
2018-01-31 12:39:19 +00:00
|
|
|
######################
|
|
|
|
# Delete old backups #
|
|
|
|
######################
|
|
|
|
|
2018-01-31 13:10:28 +00:00
|
|
|
if [ -n "${max_backups}" ] && [ "$(ls -A $backup_folder)" ]; then
|
|
|
|
deleted=false
|
|
|
|
|
|
|
|
# While there are > $max_backups, delete every old backup
|
|
|
|
while [ $(ls $backup_folder -1 | wc -l) -gt $max_backups ]; do
|
|
|
|
BACKUP_TO_BE_DELETED=$(ls $backup_folder -1tr | head -n 1)
|
|
|
|
echo "Deleted old backup $BACKUP_TO_BE_DELETED"
|
|
|
|
rm -rf $backup_folder/$BACKUP_TO_BE_DELETED
|
|
|
|
|
|
|
|
deleted=true
|
2018-01-31 12:39:19 +00:00
|
|
|
done
|
2018-01-31 13:10:28 +00:00
|
|
|
|
|
|
|
if $deleted ; then
|
|
|
|
echo "--------------------------------------"
|
|
|
|
fi
|
2018-01-31 12:39:19 +00:00
|
|
|
fi
|
|
|
|
|
2018-01-31 10:19:32 +00:00
|
|
|
####################
|
|
|
|
# Start the backup #
|
|
|
|
####################
|
2018-01-30 20:57:23 +00:00
|
|
|
|
|
|
|
# Create new Backup folder
|
|
|
|
mkdir $backup_folder/"$date" -p
|
|
|
|
|
2018-01-31 13:35:02 +00:00
|
|
|
# Print start time
|
|
|
|
echo "Started Backup at `date`"
|
|
|
|
echo "----------------------------------------------"
|
|
|
|
|
2018-01-30 20:57:23 +00:00
|
|
|
# Loop through all backupfolders and convert them
|
|
|
|
for row in $(echo "${backup_hosts_file}" | jq -r '.[] | @base64'); do
|
|
|
|
_jq() {
|
|
|
|
echo ${row} | base64 -d | jq -r ${1}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if we have a host, use localhost if not
|
|
|
|
db_host=localhost
|
|
|
|
if [ "$(_jq '.host')" != "null" ]
|
|
|
|
then
|
|
|
|
db_host=$(_jq '.host')
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for a user, set to root if none exists
|
|
|
|
db_user=root
|
|
|
|
if [ "$(_jq '.user')" != "null" ]
|
|
|
|
then
|
|
|
|
db_user=$(_jq '.user')
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for a user password, set to empty if none exists
|
|
|
|
db_pw=
|
|
|
|
if [ "$(_jq '.password')" != "null" ]
|
|
|
|
then
|
|
|
|
db_pw=$(_jq '.password')
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for a database port, set to 3306 if none exists
|
|
|
|
db_port=3306
|
|
|
|
if [ "$(_jq '.port')" != "null" ]
|
|
|
|
then
|
|
|
|
db_port=$(_jq '.port')
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Do the backup
|
|
|
|
echo "Backing up $db_host"
|
2019-02-06 15:15:28 +00:00
|
|
|
mysqldump --all-databases -u ${db_user} -p${db_pw} -h ${db_host} --port ${db_port} --lock-tables=0 > $backup_folder/"$date"/${db_host}_all-databases.sql
|
2018-01-30 20:57:23 +00:00
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo "Success."
|
|
|
|
else
|
|
|
|
echo "Error."
|
|
|
|
|
|
|
|
# Delete the file if the backup was not successfull
|
|
|
|
rm $backup_folder/"$date"/${db_host}_all-databases.sql -f
|
|
|
|
fi
|
2018-01-31 13:10:28 +00:00
|
|
|
echo "-------------------------"
|
2018-01-30 20:57:23 +00:00
|
|
|
|
|
|
|
done
|
2018-01-31 13:35:02 +00:00
|
|
|
|
|
|
|
# Print end time
|
|
|
|
echo "Finished Backup at `date`"
|
|
|
|
echo "-----------------------------------------------"
|