-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.sh
executable file
·205 lines (169 loc) · 4.7 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
# 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.
#########################
# Environment variables #
#########################
# Backup folder
if [ -n $DB_BACKUP_FOLDER ]
then
backup_folder=$DB_BACKUP_FOLDER
fi
# Backup hosts file
if [ -n $DB_BACKUP_HOSTS_FILE ]
then
backup_hosts_location=$DB_BACKUP_HOSTS_FILE
fi
# Max backups to keep
if [ -n $DB_BACKUP_MAX ]
then
max_backups=$DB_BACKUP_MAX
fi
########
# Passed options, any passed option will overwrite a previously set environment variable
########
while getopts ":b:c:d:h" opt; do
case $opt in
b)
backup_folder=$OPTARG
;;
c)
backup_hosts_location=$OPTARG
;;
d)
max_backups=$OPTARG
;;
h)
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.
-d: Maximum number of backups to keep. Defaults to 24.
-h: Print this help message.
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.
- DB_BACKUP_MAX: Maximum number of backups to keep. Defaults to 24.
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
# Backup folder
if [ -z $backup_folder ]
then
backup_folder=$PWD/backups
fi
# Max number of backups to keep
if [ -z $max_backups ]
then
max_backups=24
fi
# Save the date
date=`date +%d\-%m\-%Y\_%H\-%M\-%S`
###############
# File Checks #
###############
# 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
echo "-------------------------------"
fi
# Check if the config file exists
if [ ! -f $backup_hosts_location ]; then
echo "Config file $backup_hosts_location does not exist."
exit 1
fi
# Load Config
backup_hosts_file=$(cat $backup_hosts_location)
######################
# Delete old backups #
######################
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
done
if $deleted ; then
echo "--------------------------------------"
fi
fi
####################
# Start the backup #
####################
# Create new Backup folder
mkdir $backup_folder/"$date" -p
# Print start time
echo "Started Backup at `date`"
echo "----------------------------------------------"
# 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} --lock-tables=0 > $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
# Print end time
echo "Finished Backup at `date`"
echo "-----------------------------------------------"