-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2backup
executable file
·271 lines (234 loc) · 6.62 KB
/
m2backup
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
# m2backup was built to backup mark2 based Minecraft servers
# Copyright (C) 2014 Jonas Friedmann - License: WTFPL
#####
# Settings
#####
# Exectuive user
RUNAS="minecraft"
# Binary names
BIN_TAR="tar"
BIN_NICE="nice"
BIN_IONICE="ionice"
BIN_MARK2="mark2"
# nice and ionice settings
RUNBACKUP_NICE="${BIN_NICE} -n19"
RUNBACKUP_IONICE="${BIN_IONICE} -c 3"
# Messages
ANNOUNCE=false
SAY_BACKUP_START="Backup started..."
SAY_BACKUP_FINISHED="Backup successfully finished."
# Read user settings from ~/.minebackup.conf
SETTINGS_FOLDER="${HOME}/.minebackup"
# Timestamp format
TIMESTAMP=$(date +"%Y-%m-%d-00-00")
#####
# DO NOT EDIT BELOW
#####
# Check if $SETTINGS_FOLDER exist
if [ ! -d ${SETTINGS_FOLDER} ]
then
# Create default one
echo "[INFO] Creating configuration folder ${SETTINGS_FOLDER}"
mkdir -p ${SETTINGS_FOLDER}
echo "[INFO] Creating default configuration file ${SETTINGS_FOLDER}/default.conf"
cat > ${SETTINGS_FOLDER}/default.conf << EOCONF
# Mark2 session name
MARK2NAME="mc-default"
# Server root directory
SERVERDIR="/opt/minecraft/mc-default"
# Backup directory
BACKUPDIR="/opt/backups/minecraft/mc-default"
# Folders / worlds to backup
WORLDS=( "plugins logs" )
# Exclude the following files/directories in backups
EXCLUDES=("plugins/dynmap/web/tiles")
# Clear logs older than N days
AUTOCLEAR="7"
## Overridable configurations (remove "#" to activate)
#RUNBACKUP_NICE="${BIN_NICE} -n19"
#RUNBACKUP_IONICE="${BIN_IONICE} -c 3"
# Timestamp format
#TIMESTAMP=$(date +"%Y-%m-%d-00-00")
#ANNOUNCE=false
#SAY_BACKUP_START="Backup started..."
#SAY_BACKUP_FINISHED="Backup successfully finished."
EOCONF
echo "[INFO] Successfully created default configuration files, go ahead and adjust the default configuration as you wish."
exit
fi
# Check if binaries exist
BINS=( "${BIN_TAR} ${BIN_NICE} ${BIN_IONICE} ${BIN_MARK2}" )
for BIN in $BINS;
do
type -P ${BIN} &>/dev/null && continue || echo "'${BIN} not found! Make sure you've installed that depencendy"; exit 1
done
# Check if $BACKUPDIR exist
if [ ! -d ${BACKUPDIR} ]
then
echo "'${BACKUPDIR}' doesn't exist. Run the following commands as root:"
echo "mkdir -p ${BACKUPDIR}"
echo "chown -R ${USER} ${BACKUPDIR}"
exit 1
fi
function load_config () {
[ ${DODEBUG} -eq 1 ] && set -x
if [ -z ${1} ]; then
# Check if second argument exists
echo "[ERROR] No second argument given"
exit 1
elif [[ ${1} == "-debug" ]]; then
# Check if it's not "-debug"
echo "[ERROR] No second argument given"
exit 1
else
# Load config
if [ -f ${SETTINGS_FOLDER}/${1}.conf ]
then
. ${SETTINGS_FOLDER}/${1}.conf
else
echo "[ERROR] Couldn't find configuration file ${SETTINGS_FOLDER}/${1}.conf"
exit 1
fi
fi
}
# 'Check executive user' function
function as_user() {
[ ${DODEBUG} -eq 1 ] && set -x
if [ "$(whoami)" = "${RUNAS}" ] ; then
/bin/bash -c "$1"
else
su - ${RUNAS} -c "$1"
fi
}
# 'Check running process' function
function is_running() {
[ ${DODEBUG} -eq 1 ] && set -x
if mark2 list | grep "${MARK2NAME}" >/dev/null 2>&1
then
return 0
else
return 1
fi
}
# 'Disable ingame saving' function
function mc_saveoff() {
[ ${DODEBUG} -eq 1 ] && set -x
if is_running
then
echo -ne "[INFO] ${MARK2NAME} is running, suspending saves... "
[ "${ANNOUNCE}" = true ] && as_user "mark2 send -n ${MARK2NAME} 'say ${SAY_BACKUP_START}'"
as_user "mark2 send -n ${MARK2NAME} 'save-off'"
as_user "mark2 send -n ${MARK2NAME} 'save-all'"
sleep 3
echo -ne "done\n"
else
echo "[INFO] ${MARK2NAME} was not running... done"
fi
}
# 'Enable ingame saving' function
function mc_saveon() {
[ ${DODEBUG} -eq 1 ] && set -x
if is_running
then
echo -ne "[INFO] ${MARK2NAME} is running, re-enabling saves... "
[ "${ANNOUNCE}" = true ] && as_user "mark2 send -n ${MARK2NAME} 'say ${SAY_BACKUP_FINISHED}'"
as_user "mark2 send -n ${MARK2NAME} 'save-on'"
echo -ne "done\n"
else
echo "[INFO] ${MARK2NAME} was not running. Not resuming saves... done"
fi
}
# Backup function
function mc_backup() {
[ ${DODEBUG} -eq 1 ] && set -x
# For each world/folder in the server directory
echo -ne "[INFO] Starting to backup each world...\n"
for i in ${WORLDS}; do
echo -ne "[INFO] Processing world/folder \"${i}\"...\n"
# Check if folder exists in destination
if [ ! -d ${BACKUPDIR}/${i} ]
then
echo -ne "[INFO] Creating folders for backup destination \"${BACKUPDIR}/${i}\"...\n"
as_user "mkdir -p ${BACKUPDIR}/${i}"
fi
# Check if backup already exists
if [ -f ${BACKUPDIR}/${i}/${TIMESTAMP}.tgz ]
then
# Backup already existant
echo -ne "[ERROR] Backup already exists \"${BACKUPDIR}/${i}/${TIMESTAMP}.tgz\"\n"
else
# Build exclude string
local _tarexcludes=""
for ii in ${EXCLUDES[@]}
do
_tarexcludes="$_tarexcludes --exclude=${ii}"
done
# Check if permissions are okay
touchtest=$(touch ${BACKUPDIR}/${i}/.test)
touchstatus=$?
[ $touchstatus -eq 0 ] && echo -ne "[INFO] Check for correct permissions ... done\n" && rm ${BACKUPDIR}/${i}/.test
[ $touchstatus -ne 0 ] && echo -ne "[ERROR] Check for correct permissions ... failed\n" && exit
echo -ne "[INFO] Backup '${i}' ..."
${RUNBACKUP_NICE} ${RUNBACKUP_IONICE} ${BIN_TAR} cvzf ${BACKUPDIR}/${i}/${TIMESTAMP}.tgz ${SERVERDIR}/${i} ${_tarexcludes} >/dev/null 2>&1
echo -ne "done\n"
fi
done
}
# 'List available backups' function
function listbackups() {
[ ${DODEBUG} -eq 1 ] && set -x
#! ToDo !#
}
# 'List available backups' function
function autoclear() {
[ ${DODEBUG} -eq 1 ] && set -x
if [ -z ${1} ]; then
# Check if second argument exists
echo "[ERROR] No second argument given"
exit 1
fi
if [[ -z ${AUTOCLEAR} ]]; then
echo "[ERROR] No AUTOCLEAR configuration variable found!"
exit 1
fi
find ${BACKUPDIR} -mtime +${AUTOCLEAR} -type f -exec rm {} \;
}
#####
# Catch argument
#####
echo "$@" > /dev/null 2>&1
if [ "$_" = '-debug' ];
then
DODEBUG=1
else
DODEBUG=0
fi
#Start-Stop here
case "${1}" in
list)
load_config "${2}"
listbackups "${2}"
;;
backup)
load_config "${2}"
mc_saveoff "${2}"
mc_backup "${2}"
mc_saveon "${2}"
;;
clear)
load_config "${2}"
autoclear "${2}"
;;
*)cat << EOHELP
Usage: ${0} COMMAND [ARGUMENT]
COMMANDS
backup <world> Backup a mark2 server.
clear <world> Clear old backups older than \"n\" days
list <world> List available backups per server.
-debug Enable debug output (Must be the last argument).
EOHELP
exit 1
;;
esac
exit 0