-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrestore.sh
159 lines (139 loc) · 4.74 KB
/
restore.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
#!/bin/bash
# minecraft server restore script
# read server files
source server.settings
source server.functions
# parse arguments
ParseArgs "$@"
ArgHelp
# safety checks
RootSafety
ScriptSafety
# debug
Debug "executing $0 script"
# change to server directory
ChangeServerDirectory
# check for existance of executable
CheckExecutable
# look if server is running
CheckScreen
# prints countdown to screen
Countdown "restoring a backup"
# server stop
Stop
# awaits server stop
AwaitStop
# force quit server if not stopped
ForceQuit
# output confirmed stop
Log "ok" "server successfully stopped" "${screenLog}"
Print "ok" "server successfully stopped"
# create backup
CachedBackup "restore"
# create arrays with backupdirectorys
Print "info" "scanning backup directory..."
cd ${backupDirectory}
backups=($(ls))
cd hourly
backupsHourly=($(ls))
cd ${backupDirectory}
cd daily
backupsDaily=($(ls))
cd ${backupDirectory}
cd weekly
backupsWeekly=($(ls))
cd ${backupDirectory}
cd monthly
backupsMonthly=($(ls))
cd ${backupDirectory}
cd cached
backupsCached=($(ls))
cd ${backupDirectory}
# ask for daily or hourly backup to restore
PS3="$(date +"%H:%M:%S") prompt: would you like to restore a ${backups[0]}, ${backups[1]}, ${backups[2]}, ${backups[3]}, ${backups[4]} backup? "
select cachedDailyHourlyWeeklyMonthly in "${backups[@]}"; do
Print "info" "you chose: ${cachedDailyHourlyWeeklyMonthly}"
break
done
# select specific backup out of daily, hourly, monthly, weekly or a special backup
if [[ "${cachedDailyHourlyWeeklyMonthly}" == "${backups[0]}" ]]; then
# ask for cached backup
PS3="$(date +"%H:%M:%S") prompt: which ${backups[4]} backup would you like to restore?"
select backup in "${backupsCached[@]}"; do
Print "info" "you chose: ${backup}"
break
done
elif [[ "${cachedDailyHourlyWeeklyMonthly}" == "${backups[1]}" ]]; then
# ask for daily backup
PS3="$(date +"%H:%M:%S") prompt: which ${backups[0]} backup would you like to restore? "
select backup in "${backupsDaily[@]}"; do
Print "info" "you chose: ${backup}"
break
done
elif [[ "${cachedDailyHourlyWeeklyMonthly}" == "${backups[2]}" ]]; then
# ask for hourly backup
PS3="$(date +"%H:%M:%S") prompt: which ${backups[1]} backup would you like to restore? "
select backup in "${backupsHourly[@]}"; do
Print "info" "you chose: ${backup}"
break
done
elif [[ "${cachedDailyHourlyWeeklyMonthly}" == "${backups[3]}" ]]; then
# ask for monthly backup
PS3="$(date +"%H:%M:%S") prompt: which ${backups[2]} backup would you like to restore? "
select backup in "${backupsMonthly[@]}"; do
Print "info" "you chose: ${backup}"
break
done
elif [[ "${cachedDailyHourlyWeeklyMonthly}" == "${backups[4]}" ]]; then
# ask for weekly backup
PS3="$(date +"%H:%M:%S") prompt: which ${backups[3]} backup would you like to restore? "
select backup in "${backupsWeekly[@]}"; do
Print "info" "you chose: ${backup}"
break
done
fi
# ask for permission to proceed
Print "info" "i will now delete the current world-directory and replace it with your chosen backup"
Print "info" "you have chosen: ${backupDirectory}/${cachedDailyHourlyWeeklyMonthly}/${backup} as a backup to restore"
read -p "$(date +"%H:%M:%S") prompt: continue? (y/n): "
# if user replys yes perform restore
regex="^(Y|y|N|n)$"
while [[ ! ${REPLY} =~ ${regex} ]]; do
read -p "$(date +"%H:%M:%S") prompt: please press y or n: " REPLY
done
if [[ ${REPLY} =~ ^[Yy]$ ]]; then
cd "${serverDirectory}"
Print "action" "restoring backup..."
nice -n 19 mv "${serverDirectory}/world" "${serverDirectory}/old-world"
nice -n 19 cp "${backupDirectory}/${cachedDailyHourlyWeeklyMonthly}/${backup}" "${serverDirectory}"
nice -n 19 mv "${backup}" "world.tar.gz"
nice -n 19 tar -xf "world.tar.gz"
nice -n 19 mv "tmp" "world"
nice -n 19 rm "world.tar.gz"
if [ -d "world" ]; then
Log "the backup ${backupDirectory}/${cachedDailyHourlyWeeklyMonthly}/${backup} has been restored" "${screenLog}"
Print "ok" "restore successful"
Print "action" "restarting server with restored backup..."
nice -n 19 rm -r "${serverDirectory}/old-world"
else
Log "error" "something went wrong - could not restore backup" "${screenLog}"
Log "action" "reverting changes..." "${screenLog}"
Print "error" "something went wrong - could not restore backup"
Print "action" "reverting changes..."
nice -n 19 mv "${serverDirectory}/old-world" "${serverDirectory}/world"
fi
./start.sh "$@"
# if user replys no cancel and restart server
else
cd ${serverDirectory}
Print "warn" "backup restore has been canceled"
Print "info" "resuming to current live world"
Print "action" "restarting server..."
Log "info" "backup restore has been canceled" "${screenLog}"
Log "info" "resuming to current live world" "${screenLog}"
./start.sh --force "$@"
fi
# log to debug if true
Debug "executed $0 script"
# exit with code 0
exit 0