This commit is contained in:
kolaente 2018-01-30 21:57:23 +01:00
parent 70ee358bb5
commit 829b23d744
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 102 additions and 0 deletions

81
backup.sh Executable file
View File

@ -0,0 +1,81 @@
#!/bin/bash
# Config
backup_hosts_location=$PWD/backup_hosts.json
backup_hosts_file=$(<$backup_hosts_location)
# Backup folder
backup_folder=$PWD/backups
# 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
fi
# Save the date
date=`date +%d\-%m\-%Y\_%H\-%M\-%S`
# Delete backups older than three days
echo "Deleting Backups older than three days..."
find $backup_folder/* -type d -ctime +3 | xargs rm -rf
echo "Deleted."
echo "-------------------------------"
# Create new Backup folder
mkdir $backup_folder/"$date" -p
# 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"
mysqldump --all-databases -u ${db_user} -p${db_pw} -h ${db_host} --port ${db_port} > $backup_folder/"$date"/${db_host}_all-databases.sql
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
echo "------------------"
done

21
backup_hosts.json Normal file
View File

@ -0,0 +1,21 @@
[
{
"host":"host1",
"user":"root",
"password":"1234"
},
{
"host":"host2",
"user":"root",
"password":"123443"
},
{
"host":"127.0.0.1",
"user":"root",
"password":"jup2000"
},
{
"user":"root",
"password":"123443"
}
]