This repository has been archived by the owner on Apr 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mumble-backup.sh
executable file
·202 lines (193 loc) · 5.21 KB
/
mumble-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
#!/bin/bash
# Daily, Weekly, Monthly backups of mumble sqlite DB
# version 2
# TODO: add backing up/restoring the config
# Where is your mumble sqlite DB?
mumbledbdir="/var/lib/mumble-server"
# What is the file called?
mumbledb="mumble-server.sqlite"
# Where do you want to store the backups?
backupdir="/root/mumble-db-backups"
# How many days do you want to keep by default?
globalnumdays=7
# User mumble runs under (usually the same as mumblegroup which is by default mumble-server)
mumbleuser="mumble-server"
# Group mumble user is in
mumblegroup=${mumbleuser}
#
# basic usage
#
function usage ()
{
echo "Usage: "${0}" [-b|backup|--backup] [-c|cleanup|--cleanup [-d days]] [-r|restore|--restore [-y]]"
echo -e " -b, [--]backup \tperform a backup"
echo -e " -c, [--]cleanup \tperform a cleanup (use -d to specify the number of days to keep)"
echo -e " -r, [--]restore \tperform a restore (use -y to blindly restore from yesterday's backup)"
}
#
# Bad command entered
#
function badcommand ()
{
echo "$(timestamp)Bad command, check --help for usage"
}
#
# Take a backup
#
function backup ()
{
# check backupdir exists first
if [ -d ${backupdir} ]; then
echo "$(timestamp)Starting Backup"
# copy the file
cp ${mumbledbdir}/${mumbledb} ${backupdir}/${mumbledb}.$(date +%Y%m%d)
# did copy run into issues?
exit=${?}
if [ ${exit} -eq 0 ]; then
echo "$(timestamp)Finished Backup with no errors"
else
echo "$$(timestamp)ERROR: Finished Backup with errors"
exit 1
fi
else
echo "$(timestamp)ERROR: Backup directory does not exist, expecting the path "${backupdir}
exit 1
fi
}
#
# Clean up the backup directory
# Arguments: number of days to keep
#
function cleanup ()
{
echo "$(timestamp)Staring Cleanup"
echo "$(timestamp)Deleting Backups older than "${1}" days"
# find all files in the directory that are older than numdays (specified above), and remove them
find ${backupdir} -mtime +${1} -exec rm -f {} \;
# did find encounter any issues
exit=${?}
if [ ${exit} -gt 0 ]; then
echo "$(timestamp)ERROR: Finished Cleanup with errors"
exit 1
fi
echo "$(timestamp)Finished Cleanup with no errors"
}
#
# Easily restore a backup (not finished)
# Arguments: Mode (y means yesterday, anything else and it will interactively let you restore a backup)
#
function restorebackup ()
{
# Override menu and just restore yesterday's file
if [[ ${1} == "y" ]]; then
restorefile=${backupdir}"/"${mumbledb}"."$(date -d "yesterday" '+%Y%m%d')
else
echo "Choose a backup to restore from:"
# cause the array we're creating to be empty if there are no files
shopt -s nullglob
# put the files in the backup directory into the ${files} array
files=(${backupdir}/*)
# temporary counter
i=1
for file in "${files[@]}"; do
echo ${i}": "${file}
let i=i+1
done
read choice
# arrays start at 0, not 1
let choice=choice-1
restorefile=${files[${choice}]}
fi
if [ -f "${restorefile}" ]; then
echo "$(timestamp)Stopping mumble..."
# Stop mumble, overwrite the DB, and restart it
# if there are errors, the script will continue and try to re-start mumble (in case it has permissions to stop/start but no permissions to restore DB, for example)
service mumble-server stop
exit=${?}
if [ ${exit} -gt 0 ]; then
echo "$(timestamp)ERROR: Unable to stop mumble, are you root?"
exit 1
fi
echo "$(timestamp)Restoring..."
# set a variable that flags if we've had an error
backuperror=0
cp ${restorefile} ${mumbledbdir}/${mumbledb}
exit=${?}
if [ ${exit} -gt 0 ]; then
echo "$(timestamp)ERROR: Error restoring from backup!"
backuperror=1
fi
chown ${mumbleuser}:${mumblegroup} ${mumbledbdir}/${mumbledb}
exit=${?}
if [ ${exit} -gt 0 ]; then
echo "$(timestamp)ERROR: Error setting permissions!"
backuperror=1
fi
echo "$(timestamp)Starting mumble..."
service mumble-server start
if [ ${exit} -gt 0 ]; then
echo "$(timestamp)ERROR: Unable to start mumble, is it already running?"
exit 1
fi
if [ ${backuperror} -gt 0 ]; then
echo "$(timestamp)ERROR: Unable to restore from "${restorefile}" but was able to re-start mumble"
else
echo "$(timestamp)Successfully restored from "${restorefile}" and restarted mumble"
fi
else
echo "$(timestamp)ERROR: File to restore from does not exist"
exit 1
fi
}
#
# Add a specially formatted timestamp
#
function timestamp ()
{
date "+[%H:%M:%S] %m-%d-%Y : "
}
# User did not specify any arguments
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
# What does the user want?
while [ $# -ne 0 ]; do
case "${1}" in
-h | --help | help)
usage
break
;;
-b | --backup | backup)
# shift here so any further parameters are passed to the command
shift
backup
;;
-c | --cleanup | cleanup)
shift
# if -d was specified after cleanup, then user wanted to specify the number of days to keep. store that in a variable here
if [[ ${1} == "-d" ]]; then
shift
numdays=${1}
shift
else
numdays=${globalnumdays}
fi
cleanup ${numdays}
;;
-r | --restore | restore)
shift
# if -y was specified, we're automatically assuming restoring yesterday's backup
if [[ ${1} == "-y" ]]; then
shift
mode="y"
else
mode="n"
fi
restorebackup ${mode}
;;
*)
badcommand
exit 1
esac
done