forked from simylein/minecraft-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
161 lines (144 loc) · 4.16 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
#!/bin/bash
# minecraft server backup script
# for the sake of integrity of your backups,
# I would strongly recommend not to mess with this file.
# if you really know what you are doing feel free to go ahead ;^)
# read server files
source server.settings
source server.functions
# parse backup category
ParseCategory "$@"
# shift args
shift
# parse arguments
ParseArgs "$@"
ArgHelp
# safety checks
RootSafety
ScriptSafety
# debug
Debug "executing $0 script"
# change to server directory
ChangeServerDirectory
# check if server is running
CheckScreen
# test all categories
BackupDirectoryIntegrity
# main backup function
function RunBackup {
Debug "executing backup-${1} script"
# check if disk space is too low
if (((${worldSizeBytes} + ${diskSpaceError}) > ${diskSpaceBytes})); then
OutputDiskSpaceError "${1}" "${2}" "${3}"
exit 1
fi
# check is getting low
if (((${worldSizeBytes} + ${diskSpaceWarning}) > ${diskSpaceBytes})); then
OutputDiskSpaceWarning "${1}"
fi
# check if backup already exists
if ! [[ -s "${backupDirectory}/${1}/${serverName}-${2}.tar.gz" ]]; then
# save the world
Screen "save-all"
AwaitString "Saved the game" "10"
# disable auto save
Screen "save-off"
AwaitString "Automatic saving is now disabled" "5"
# start timer
before=$(date +%s%3N)
# copy world
nice -n 19 cp -r "world" "tmp-${1}"
if [ $? != 0 ]; then
OutputBackupCopyError "${1}" "${2}" "${3}"
rm -r "tmp-${1}"
exit 1
fi
# enable auto save
Screen "save-on"
AwaitString "Automatic saving is now enabled" "5"
# compress world
nice -n 19 tar -czf "world-${1}.tar.gz" "tmp-${1}"
if [ $? != 0 ]; then
OutputBackupTarError "${1}" "${2}" "${3}"
rm -r "world-${1}.tar.gz" "tmp-${1}"
exit 1
fi
# mv backup and remove tmp
nice -n 19 mv "${serverDirectory}/world-${1}.tar.gz" "${backupDirectory}/${1}/${serverName}-${2}.tar.gz"
nice -n 19 rm -r "tmp-${1}"
# stop timer
after=$(date +%s%3N)
else
OutputBackupAlreadyExists "${1}" "${2}" "${3}"
exit 1
fi
if [[ -s "${backupDirectory}/${1}/${serverName}-${2}.tar.gz" ]]; then
# remove old backup if it exists
if [[ -s "${backupDirectory}/${1}/${serverName}-${3}.tar.gz" ]]; then
nice -n 19 rm "${backupDirectory}/${1}/${serverName}-${3}.tar.gz"
fi
# calculate time spent and compression
timeSpent=$((${after} - ${before}))
compressedBackupSize=$(du -sh ${backupDirectory}/${1}/${serverName}-${2}.tar.gz | cut -f1)
compressedBackupSizeBytes=$(du -sb ${backupDirectory}/${1}/${serverName}-${2}.tar.gz | cut -f1)
# check if backup size is to small for a real backup
if ((${compressedBackupSizeBytes} < (${worldSizeBytes} / 100 * ${backupSizeError}))); then
OutputBackupSizeError "${1}" "${2}" "${3}"
exit 1
fi
# check if backup size is suspiciously small
if ((${compressedBackupSizeBytes} < (${worldSizeBytes} / 100 * ${backupSizeWarning}))); then
OutputBackupSizeWarning "${1}" "${2}" "${3}"
fi
# read settings and output success
source server.settings
OutputBackupSuccess "${1}" "${2}" "${3}"
else
OutputBackupGenericError "${1}" "${2}" "${3}"
fi
Debug "executed backup-${1} script"
}
# hourly backup
if [ ${isHourly} == true ]; then
if [ ${doHourly} == true ]; then
# run backup
RunBackup "hourly" "${newHourly}" "${oldHourly}"
else
Log "info" "backup-hourly is disabled" "${backupLog}"
Print "info" "backup-hourly is disabled"
fi
fi
# daily backup
if [ ${isDaily} == true ]; then
if [ ${doDaily} == true ]; then
# run backup
RunBackup "daily" "${newDaily}" "${oldDaily}"
else
Log "info" "backup-daily is disabled" "${backupLog}"
Print "info" "backup-daily is disabled"
fi
fi
# weekly backup
if [ ${isWeekly} == true ]; then
if [ ${doWeekly} == true ]; then
# run backup
RunBackup "weekly" "${newWeekly}" "${oldWeekly}"
else
Log "info" "backup-weekly is disabled" "${backupLog}"
Print "info" "backup-weekly is disabled"
fi
fi
# monthly backup
if [ ${isMonthly} == true ]; then
if [ ${doMonthly} == true ]; then
# run backup
RunBackup "monthly" "${newMonthly}" "${oldMonthly}"
else
Log "info" "backup-monthly is disabled" "${backupLog}"
Print "info" "backup-monthly is disabled"
fi
fi
# debug
Debug "executed $0 script"
# exit with code 0
exit 0