aboutsummarylogtreecommitdiffstats
path: root/minecraftctl
blob: 55ed49c0a53539386dd53b665bd3a85bf299aeba (plain)
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
#!/bin/bash


# Adapted from generic minecraft init script
#	 http://www.minecraftwiki.net/wiki/Tutorials/Server_startup_script
#
# Main Differences: 
#   - Start stop and reload is handled by systemd, removed
#   - update handled by pacman, removed
#   - changed from init script to "minecraftctl" command.
#   - uses /etc/conf.d/minecraftd for variables
#
# Changes:
#
#   Changed running screen to work as root, broke it out into mc_docommand
#   Commented echo commands for successful execution to not spam the log.


. /etc/conf.d/minecraft || echo "can't source /etc/conf.d/minecraft"


ME=`whoami`
as_user() {
  if [ $ME == $MCUSER ] ; then
    bash -c "$1"
  else
    su - $MCUSER -c "$1"
  fi
}

mc_saveoff() {
  if pgrep -u $MCUSER -f $SERVICE > /dev/null
  then
    #echo "$SERVICE is running... suspending saves"
	mc_docommand "say $BACKUPSTARTMSG"
	mc_docommand save-off
	mc_docommand save-all
    sync
    sleep 10
  else
    echo "$SERVICE is not running. Not suspending saves."
  fi
}

mc_saveon() {
  if pgrep -u $MCUSER -f $SERVICE > /dev/null
  then
    #echo "$SERVICE is running... re-enabling saves"
	mc_docommand save-on
	mc_docommand "say $BACKUPENDMSG"
  else
    echo "$SERVICE is not running. Not resuming saves."
  fi
}

mc_backup() {
   mc_saveoff
   
   NOW=`date "+%Y-%m-%d_%Hh%M"`
   BACKUP_FILE="$BACKUPPATH/${WORLD}_${NOW}.tar"
   #echo "Backing up minecraft world..."
   #as_user "cd $_SRVDIR && cp -r $WORLD $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"`"
   as_user "tar -h -C \"$_SRVDIR\" -cf \"$BACKUP_FILE\" \"$WORLD\""

   #echo "Backing up $SERVICE"
   as_user "tar -h -C \"$_SRVDIR\" -rf \"$BACKUP_FILE\" \"$SERVICE\""
   #as_user "cp \"$_SRVDIR/$SERVICE\" \"$BACKUPPATH/minecraft_server_${NOW}.jar\""

   mc_saveon

   #echo "Compressing backup..."
   as_user "gzip -f \"$BACKUP_FILE\""
   #echo "Done."
}

mc_command() {
  command="$1";
  if pgrep -u $MCUSER -f $SERVICE > /dev/null
  then
    pre_log_len=`wc -l "$_SRVDIR/server.log" | awk '{print $1}'`
    #echo "$SERVICE is running... executing command"
	mc_docommand "$command"
    sleep .1 # assumes that the command will run and print to the log file in less than .1 seconds
    # print output
    tail -n $[`wc -l "$_SRVDIR/server.log" | awk '{print $1}'`-$pre_log_len] "$_SRVDIR/server.log"
  else
    echo "$SERVICE is not running. Couldn't executed command."
  fi
}

mc_docommand() {
	screen -p 0 -S minecraft -X stuff "`printf \"$1\r\"`"
}


#Start-Stop here
case "$1" in
  backup)
    mc_backup
    ;;
  status)
    if pgrep -u $MCUSER -f $SERVICE > /dev/null
    then
      echo "$SERVICE is running."
    else
      echo "$SERVICE is not running."
    fi
    ;;
  command)
    if [ $# -gt 1 ]; then
      shift
      mc_command "$*"
    else
      echo "Must specify server command (try 'help'?)"
    fi
    ;;

  *)
    echo "Usage: $0 {backup|status|command \"server command\"}"
    exit 1
  ;;
esac

exit 0