Moved from github, lost history. Starting over.
[minecraft-server-init-script.git] / minecraft-server
1 #!/bin/bash
2
3 # Minecraft server script
4 # v0.3 
5 # leander@one-button.org
6
7 # User editable variables:
8 # Always include the trailing slashes on directories!
9 MINECRAFT="minecraft_server.jar" # Name of your server jar file.  This script supports vanilla and craftbukkit.
10 MCPATH="/var/minecraft_directory/" # Directory on disk of your MC server. Make sure your MINECRAFT jar file is located here.
11 SERVERNAME="minecraft"
12 ARGS="-Xmx2G -Xms2G -jar" # Standard arguments for the minecraft server jar.  
13 USER="steve" # User your minecraft server runs under.
14 BACKUPDIR="/var/backup/minecaft_backup/" # Directory for backup tar files.
15 DAYSOFBACKUPS="+7" # number of days of backups to keep. Default is +7. 
16
17 # Various programs this script calls
18 JAVA="/usr/bin/java"
19 SCREEN="/usr/bin/screen"
20 DATE=$(date +%m-%d-%Y-%H:%M)
21 TAR="/bin/tar"
22 RSYNC="/usr/bin/rsync"
23 FIND="/usr/bin/find"
24
25 # RAM Disk settings.  
26 # Don't use this if you don't know what you're doing!
27 # See the README for me info
28 RAMDISK=false # Set to true if you want to use a RAM disk, false to run from a hard disk.  See README first.
29 RAMDISKDIR="/lol/ramdisk/" # The path where your RAM disk is mounted. 
30
31 already_running(){
32
33         if pgrep -u $USER -f $MINECRAFT >  /dev/null
34         then
35                 return 1
36         else
37                 return 0
38         fi
39 }
40
41 run_as() {
42
43         WHOAMI=$(whoami)
44
45         if [ "$WHOAMI" == "$USER" ]
46         then
47                 bash -c "$1"
48         else
49                 su  - $USER -c "$1"
50         fi
51
52 }
53
54 start() {
55         
56         already_running
57
58         if [ "$?" == "1" ]  
59         then
60                 echo "Minecraft already running ..."
61         else
62                 if [ $RAMDISK == "true" ]
63                 then
64                         echo "Copying files to RAM Disk ..."
65                         setup_ramdisk
66                         echo "Starting $MINECRAFT ..."
67                         run_as "cd $RAMDISKDIR && $SCREEN -dm -S $SERVERNAME $JAVA $ARGS $MINECRAFT"
68                         sleep 7
69                 else
70
71                         echo "Starting $MINECRAFT ..."
72                         run_as "cd $MCPATH && $SCREEN -dm -S $SERVERNAME $JAVA $ARGS $MINECRAFT"
73                         sleep 7
74                 fi
75         
76                 already_running 
77                         if [ "$?" == "1" ]
78                         then
79                                 echo "$MINECRAFT started on $SERVERNAME screen session."
80                         else
81                                 echo "Error, $MINECRAFT could not be started.  Check ${MCPATH}logs/ for errors."
82                         fi
83
84         fi
85 }
86
87
88 stop() {
89         
90         already_running
91         if [ "$?" == "1" ]
92         then
93                 if [ $RAMDISK == "true" ]
94                 then
95                 
96                 echo "Syncing RAM drive to disk .."
97                 sync_ram_to_disk
98                 echo "Sync complete."
99
100         fi
101                 echo "Stopping $MINECRAFT ... "
102                 run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say SERVER SHUT DOWN IN 5 SECONDS.  SAVING WORLD ...\"$(echo -ne '\015')"
103                 run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-all\015'"
104                 sleep 5
105                 run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff stop\015'"
106                 sleep 2
107                 already_running
108                         if [ "$?" == "1" ]
109                         then
110                                 echo "$MINECRAFT could not be stopped."
111                                 echo "Check ${MCPATH}server.log for errors."
112                         else
113                                 echo "$MINECRAFT stopped."
114                         fi
115
116         else
117                 echo "$MINECRAFT is not running"
118         fi
119 }
120
121 status() {
122
123         already_running
124         if [ "$?" == "1" ]
125         then
126                 echo "$MINECRAFT is running."
127         else
128                 echo "$MINECRAFT is not running."
129         fi
130 }
131
132 save_all() {
133
134         already_running
135         if [ "$?" == "1" ]
136         then
137                 echo "Saving all in memory to disk ..."
138                 run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say SAVING WORLD.  PREPARE FOR LAG.\"$(echo -ne '\015')"
139                 run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-all\015'"
140                 sleep 10
141                 run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say SAVE COMPLETE.\"$(echo -ne '\015')"
142         else
143                 echo "$MINECRAFT not running."
144                                 
145         fi
146
147 }
148
149 whitelist() {
150
151         already_running
152         if [ "$?" == "1" ]
153         then
154                 if [ "$1" == "add" ]
155                 then
156                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"whitelist add $2\"$(echo -ne '\015')"
157
158                 elif [ "$1" == "remove" ]
159                 then
160                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"whitelist remove $2\"$(echo -ne '\015')"
161                 fi
162                         
163         else
164                 echo "$MINECRAFT not running."
165                                 
166         fi
167
168
169 }
170
171 ban() {
172
173         already_running
174         if [ "$?" == "1" ]
175         then
176                 if [ "$1" == "ban" ]
177                 then
178                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"ban $2\"$(echo -ne '\015')"
179
180                 elif [ "$1" == "pardon" ]
181                 then
182                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"pardon $2\"$(echo -ne '\015')"
183                 
184                 elif [ "$1" == "ban-ip" ]
185                 then
186                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"ban-ip $2\"$(echo -ne '\015')"
187
188                 elif [ "$1" == "pardon-ip" ]
189                 then
190                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"pardon-ip $2\"$(echo -ne '\015')"
191                 fi
192                         
193         else
194                 echo "$MINECRAFT not running."
195                                 
196         fi
197
198
199 }
200
201 backup() {
202         already_running
203         if [ "$?" == "1" ]
204         then
205                 if [ "$RAMDISK" == "true" ]
206                 then
207                         echo "Syncing RAM drive to disk .."
208                         sync_ram_to_disk
209                         echo "Sync complete."
210                 fi
211
212                 echo "Backing up server ..."
213                 if [ -d $BACKUPDIR ]
214                 then
215                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say BACKING UP.  SERVER GOING READ-ONLY.\"$(echo -ne '\015')"
216                         run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-off\015'"       
217                         run_as "$TAR czf ${BACKUPDIR}${SERVERNAME}-${DATE}.tar.gz $MCPATH"
218                         run_as "$FIND ${BACKUPDIR} -maxdepth 1 -type f -mtime $DAYSOFBACKUPS -delete"
219                         run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-on\015'"        
220                         run_as "$SCREEN -p 0 -S $SERVERNAME -X stuff \"say BACK UP COMPLETE.  SERVER GOING READ-WRITE.\"$(echo -ne '\015')"
221                         
222                         echo "Backup complete."
223                 else
224                         echo "Error, backup directory not found."
225                 fi
226         else
227                         run_as "$TAR czf ${BACKUPDIR}minecraft-server-${DATE}.tar.gz $MCPATH"
228                         echo "Backup complete." 
229                         echo "Cleaning up backups older than $DAYSOFBACKUPS days ..."
230                         run_as "$FIND ${BACKUPDIR} -maxdepth 1 -type f -mtime $DAYSOFBACKUPS -delete"
231                         echo "Old backups cleaned up"
232         fi
233
234 }
235
236 setup_ramdisk() {
237                 echo "You are setup to run in a RAM disk."
238                 echo "PLEASE see the README about syncing the RAM drive with a hard drive!"
239                 run_as "$RSYNC -rO --delete-after $MCPATH $RAMDISKDIR"
240 }
241
242 sync_ram_to_disk() {
243         
244         if [ -f $RAMDISKDIR/${MINECRAFT} ]
245         then
246
247                 echo "Syncing RAM disk to drive ..."
248                 run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-off\015'"       
249                 run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-all\015'"
250                 sleep 2
251                 run_as "$RSYNC -art --delete-after $RAMDISKDIR $MCPATH"
252                 run_as "$SCREEN -p 0 -S $SERVERNAME -X eval 'stuff save-on\015'"
253                 echo "Sync complete."
254
255         else
256                 echo "Error, $MINECRAFT not in RAM disk."
257         fi
258 }
259
260 case $1 in
261         start)
262                 start
263                 exit 1
264                 ;;
265         stop)
266                 stop
267                 exit 1
268                 ;;
269         
270         save-all)
271                 save_all
272                 exit 1
273                 ;;
274
275         backup)
276                 save_all
277                 backup
278                 exit 1
279                 ;;
280
281         whitelist-add)
282                 whitelist add $2 
283                 exit 1
284                 ;;
285         
286         whitelist-remove)
287                 whitelist remove $2 
288                 exit 1
289                 ;;
290
291         ban)
292                 ban ban $2 
293                 exit 1
294                 ;;
295
296         ban-ip)
297                 ban ban-ip $2 
298                 exit 1
299                 ;;
300
301         pardon)
302                 ban pardon $2 
303                 exit 1
304                 ;;
305
306         pardon-ip)
307                 ban pardon-ip $2 
308                 exit 1
309                 ;;
310         
311         status)
312                 status
313                 exit 1
314                 ;;
315
316         writetodisk)
317                 sync_ram_to_disk
318                 exit 1
319                 ;;
320
321         restart)
322                 stop
323                 start
324                 exit 1
325                 ;;
326
327         *)
328                 echo "Usage: minecraft-server {start|restart|save-all|backup|writetodisk|stop|whitelist-add|whitelist-remove|ban|ban-ip|pardon|pardon-ip}"
329                 exit 1
330                 ;;
331 esac
332
333 exit 0