-
Notifications
You must be signed in to change notification settings - Fork 11
/
backup.sh
145 lines (114 loc) · 3.45 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
#!/bin/bash
# MINECRAFT backup
#Variables
# DateTime stamp format that is used in the tar file names.
STAMP=`date +%H%M`
# The screen session name, this is so the script knows where to send the save-all command (for autosave)
SCREENNAME="minecraft"
# Whether the script should tell your server to save before backup (requires the server to be running in a screen $
AUTOSAVE=1
# Notify the server when a backup is completed.
NOTIFY=1
# Backups DIR name (NOT FILE PATH)
BACKUPDIR="backups"
# MineCraft server properties file name
PROPFILE="server.properties"
# When an error occurs it will send it to this e-mail
MAILTO="[email protected]"
# Enable/Disable Logging (This will just echo each stage the script reaches, for debugging purposes)
LOGIT=1
# *-------------------------* SCRIPT *-------------------------*
# Set todays backup dir
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Starting the backup script.."
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Working in directory: $PWD."
fi
BACKUPDATE=`date +%Y-%m-%d`
FINALDIR="$BACKUPDIR/$BACKUPDATE/${STAMP}"
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Checking if backup folders exist, if not then create them."
fi
if [ -d $BACKUPDIR ]
then
echo -n < /dev/null
else
mkdir "$BACKUPDIR"
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Created Folder: $BACKUPDIR"
fi
fi
if [ -d "$FINALDIR" ]
then
echo -n < /dev/null
else
mkdir -p "$FINALDIR"
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Created Folder: $FINALDIR"
fi
fi
# --Check for dependencies--
#Is this system Linux?
#LOL just kidding, at least it better be...
#Get level-name
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Fetching Level Name.."
fi
while read line
do
VARI=`echo $line | cut -d= -f1`
if [ "$VARI" == "level-name" ]
then
WORLD=`echo $line | cut -d= -f2`
fi
done < "$PROPFILE"
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Level-Name is $WORLD"
fi
BFILE="$WORLD.tar.gz"
CMD="tar -czf $FINALDIR/$BFILE $WORLD"
BFILEN="${WORLD}_nether.tar.gz"
CMDN="tar -czf $FINALDIR/$BFILEN ${WORLD}_nether"
BFILEE="${WORLD}_the_end.tar.gz"
CMDE="tar -czf $FINALDIR/$BFILEE ${WORLD}_the_end"
if [ $LOGIT -eq 1 ]
then
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Packing and compressing folder: $WORLD to tar file: $FINALDIR/$BFILE"
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Packing and compressing folder: ${WORLD}_nether to tar file: $FINALDIR/$BFILEN"
echo "$(date +"%G-%m-%d %H:%M:%S") [LOG] Packing and compressing folder: ${WORLD}_the_end to tar file: $FINALDIR/$BFILEE"
fi
if [ $NOTIFY -eq 1 ]
then
screen -x $SCREENNAME -X stuff "`printf "say Backing up world: \'$WORLD\'\r"`"
fi
#Create timedated backup and create the backup directory if need.
if [ $AUTOSAVE -eq 1 ]
then
if [ $NOTIFY -eq 1 ]
then
screen -x $SCREENNAME -X stuff "`printf "say Forcing Save..\r"`"
fi
#Send save-all to the console
screen -x $SCREENNAME -X stuff `printf "save-all\r"`
sleep 2
fi
if [ $NOTIFY -eq 1 ]
then
screen -x $SCREENNAME -X stuff "`printf "say Packing and compressing world...\r"`"
fi
# Run backup command
screen -x $SCREENNAME -X stuff "`printf "save-off\r"`"
$CMD
$CMDN
$CMDE
screen -x $SCREENNAME -X stuff "`printf "save-on\r"`"
if [ $NOTIFY -eq 1 ]
then
# Tell server the backup was completed.
screen -x $SCREENNAME -X stuff "`printf "say Backup Completed.\r"`"
fi