{"id":579,"date":"2013-08-17T14:58:41","date_gmt":"2013-08-17T14:58:41","guid":{"rendered":"http:\/\/www.scaine.net\/site\/?p=579"},"modified":"2017-02-24T17:29:35","modified_gmt":"2017-02-24T17:29:35","slug":"backing-up-your-minecraft-server-in-ubuntu%ef%bb%bf%ef%bb%bf","status":"publish","type":"post","link":"https:\/\/www.scaine.net\/site\/2013\/08\/backing-up-your-minecraft-server-in-ubuntu%ef%bb%bf%ef%bb%bf\/","title":{"rendered":"Backing Up Your Minecraft Server in Ubuntu\ufeff\ufeff"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright\" src=\"https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/Fire.png\" alt=\"Fire\" width=\"150\" height=\"150\">I wrote <a href=\"https:\/\/www.scaine.net\/site\/2013\/02\/step-by-step-install-a-minecraft-server-on-ubuntu\/\">an earlier post on how to create a Minecraft Server on Ubuntu<\/a>. It&#8217;s been pretty popular, so this article will describe a very simple way to ensure that you take hourly automated backups of your Minecraft server(s).<\/p>\n<p>This is incredibly useful for a number of reasons :<\/p>\n<ol>\n<li><span style=\"line-height: 15px;\">Some random punter has griefed your server beyond recognition and you need to roll it back to its former glory.<\/span><\/li>\n<li>A fire has started on the other side of the map, you didn&#8217;t notice it, and now your world is a charred, devastated ruin.<\/li>\n<li>You fell into a lava pit with your all your coolest stuff on and can&#8217;t bear the thought of having to do without.<\/li>\n<li>You accidentally hit a zombie pigman in the Nether and now there&#8217;s an angry mob at the portal preventing you from progressing.<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-593\" src=\"https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/Selection_018.png\" alt=\"Facepalm\" width=\"444\" height=\"295\">These are the kind of calamities that stop you playing Minecraft for weeks, or make you ditch your hard earned world to start over, with heavy heart.<\/p>\n<h2><!--more-->Backup!<\/h2>\n<p>So what do you do? Well the Minecraft games directory is actually really, really straight forward, so taking a backup is actually as simple as running a little&nbsp;<em>tar<\/em> command and putting the resultant file somewhere safe!<\/p>\n<p>Here&#8217;s my <strong>tar<\/strong> command :<\/p>\n<pre>tar -cpvzf \/home\/mc\/Minecraft\/Backups\/Server1\/minecraft-day0.tar.gz \/home\/mc\/Minecraft\/Games\/Server1<\/pre>\n<p>So, you can see that my server has a <strong>Minecraft<\/strong> directory. In there, I have <strong>Backups<\/strong> and <strong>Games<\/strong>. And inside <strong>Games<\/strong>, I&#8217;m backing up the <strong>Server1<\/strong> server directory. Simple! When I run that command, the <strong>Backups<\/strong> directory gets a <em>minecraft-day0.tar.gz<\/em> file in it.<\/p>\n<p>So, pop into your server directory and create a file called something like backup-server1-daily, put that <strong>tar<\/strong> command in it, then make it exectuable, by either using nautilus, or running &#8220;chmod +x backup-server1-daily&#8221;.<\/p>\n<p>So how do I make this run? With the built-in linux scheduler, <strong>cron<\/strong>. And how, exactly, do you do that? Well, I cheat actually. I install <a href=\"http:\/\/www.webmin.com\/\" target=\"_blank\">Webmin<\/a>, then use a web browser to configure it via the GUI. If you want the quick method, however, you can simply do this in a&nbsp;terminal :<\/p>\n<pre>crontab -e<\/pre>\n<p>Add this line :<\/p>\n<pre>@daily \/home\/mc\/Minecraft\/Games\/Server1\/backup-daily-server1 #Make daily backup of Minecraft server1<\/pre>\n<p>Now, when you do a <strong>crontab -l<\/strong>, you&#8217;ll see your script is ready to run each day, at midnight.<\/p>\n<h2>Hourly<\/h2>\n<p>Now things get a little more complex if we want to run things hourly. Why? Well, because if you fall into the lava pit at 13:59, you&#8217;ll only have one minute or less to exit the game and restore your server before the hourly backup is run and you&#8217;re back to square one, crying into your coffee.<\/p>\n<p>So, we have to run a script hourly, which rotates 24 times, saving off the old backups in succession.<\/p>\n<p>Here&#8217;s a script to do so :<\/p>\n<pre>cd \/home\/mc\/Minecraft\/Backups\/Server1\r\nrm -f minecraft.tar.gz.24\r\nmv minecraft-hour23.tar.gz minecraft-hour24.tar.gz\r\nmv minecraft-hour22.tar.gz minecraft-hour23.tar.gz\r\nmv minecraft-hour21.tar.gz minecraft-hour22.tar.gz\r\nmv minecraft-hour20.tar.gz minecraft-hour21.tar.gz\r\nmv minecraft-hour19.tar.gz minecraft-hour20.tar.gz\r\nmv minecraft-hour18.tar.gz minecraft-hour19.tar.gz\r\nmv minecraft-hour17.tar.gz minecraft-hour18.tar.gz\r\nmv minecraft-hour16.tar.gz minecraft-hour17.tar.gz\r\nmv minecraft-hour15.tar.gz minecraft-hour16.tar.gz\r\nmv minecraft-hour14.tar.gz minecraft-hour15.tar.gz\r\nmv minecraft-hour13.tar.gz minecraft-hour14.tar.gz\r\nmv minecraft-hour12.tar.gz minecraft-hour13.tar.gz\r\nmv minecraft-hour11.tar.gz minecraft-hour12.tar.gz\r\nmv minecraft-hour10.tar.gz minecraft-hour11.tar.gz\r\nmv minecraft-hour9.tar.gz minecraft-hour10.tar.gz\r\nmv minecraft-hour8.tar.gz minecraft-hour9.tar.gz\r\nmv minecraft-hour7.tar.gz minecraft-hour8.tar.gz\r\nmv minecraft-hour6.tar.gz minecraft-hour7.tar.gz\r\nmv minecraft-hour5.tar.gz minecraft-hour6.tar.gz\r\nmv minecraft-hour4.tar.gz minecraft-hour5.tar.gz\r\nmv minecraft-hour3.tar.gz minecraft-hour4.tar.gz\r\nmv minecraft-hour2.tar.gz minecraft-hour3.tar.gz\r\nmv minecraft-hour1.tar.gz minecraft-hour2.tar.gz\r\nmv minecraft-hour0.tar.gz minecraft-hour1.tar.gz<\/pre>\n<pre>tar -cpvzf \/home\/mc\/Minecraft\/Backups\/Server1\/minecraft-hour0.tar.gz \/home\/mc\/Minecraft\/Games\/Server1 --exclude '\/home\/mc\/Minecraft\/Games\/Server1\/plugins\/dynmap'<\/pre>\n<p>I&#8217;ll cover that <strong>&#8211;exclude<\/strong> option in a moment. What we&#8217;re doing here is deleting the oldest backup, then renaming every to be one hour older, then using pretty much the same tar command as before in order to create the newest backup.<\/p>\n<p>So, because we&#8217;re doing this hourly, it&#8217;ll be more likely to happen when someone is playing on your server. So we want it to run really quickly. What I&#8217;ve found is that some plugins create an absolutely epic amount of data when you run them with CraftBukkit, and one of those plugins is Dynmap. So when we take the Daily backup, we backup everything, but when we do the hourly backup, we exclude the really big directories that probably haven&#8217;t changed anyway!<\/p>\n<p>There&#8217;s one more trick, which is to ensure that a player isn&#8217;t making a change&nbsp;<em>while<\/em> the backup is taking place. To do so, we need to issue the <strong>save-off<\/strong> command to the server before the backup is run, then issue a <strong>save-on<\/strong> afterwards.<em><br \/>\n<\/em><\/p>\n<p>This will also let us notify any users that a backup is taking place. However, in order to do that, we need to run each Minecraft server in its own terminal using an identifier. As it turns out, Ubuntu has such a facility available into it, called Screen.<\/p>\n<h2>Screen<\/h2>\n<p>The idea behind Screen is to multi-task using just a single terminal window. In that terminal, you create instances with the <strong>screen<\/strong> command, then switch to them as required. What&#8217;s cool about this is that each instance can be tagged with a name, so that you can recall these instances in scripts (and cron!) as required.<\/p>\n<p>To install Screen, just use <strong>sudo apt-get install screen<\/strong>.<\/p>\n<p>Now, I run three or four Minecraft servers, and the way I set them up is like so &#8211; run a terminal, then type the following :<\/p>\n<pre>cd Minecraft\\Games\r\nscreen -R Server1\r\n<em>&lt;the screen clears with a \"new terminal\" message at the bottom&gt;<\/em>\r\ncd Server1\r\n.\/start_server<\/pre>\n<p>Then I type CTRL-A, then CTRL-D. At this point, I&#8217;ve detached from the &#8220;Server1&#8221; screen and I&#8217;m back in my normal terminal window. I can then issue a <strong>screen -ls<\/strong> to see what screen instances are available to reconnect back into. Or carry on, repeating the above for Server2, Server3 and so on.<\/p>\n<p>Then later, I can <strong>screen -R Server1<\/strong> to get back to my Minecraft server. Perhaps to stop it, or issue a console command.<\/p>\n<p>Note that I match my screen tagnames to the directory I run the server in. You don&#8217;t have to do this, but I find it helps when you have a decent number of screens to manage.<\/p>\n<p>What&#8217;s nice about this set up is that I can close my main terminal window at any point, then later open a terminal, type <strong>screen -ls<\/strong>, to see what&#8217;s still running, then use <strong>screen -R<\/strong> to get back to any session I need.<\/p>\n<p>You can see why this is useful generally, but it&#8217;s great for Minecraft servers, because now we can use the <strong>stuff<\/strong> command to send messages into our Minecraft games!<\/p>\n<p>Here&#8217;s my full script, then, sending a message to all players that a backup is taking place, turning off Minecraft saving, performing the backup, informing the players that the backup is complete and turning Minecraft saving back on again.<\/p>\n<pre>screen -R Server1 -X stuff \"say Backup starting. World no longer saving... $(printf '\\r')\"\r\nscreen -R Server1 -X stuff \"save-off $(printf '\\r')\"\r\nscreen -R Server1 -X stuff \"save-all $(printf '\\r')\"\r\nsleep 3\r\n\r\ncd \/home\/sadmin\/Misc\/Apps\/Games\/Minecraft\/Backups\/Cave\r\nrm -f minecraft.tar.gz.24\r\nmv minecraft-hour23.tar.gz minecraft-hour24.tar.gz\r\nmv minecraft-hour22.tar.gz minecraft-hour23.tar.gz\r\nmv minecraft-hour21.tar.gz minecraft-hour22.tar.gz\r\nmv minecraft-hour20.tar.gz minecraft-hour21.tar.gz\r\nmv minecraft-hour19.tar.gz minecraft-hour20.tar.gz\r\nmv minecraft-hour18.tar.gz minecraft-hour19.tar.gz\r\nmv minecraft-hour17.tar.gz minecraft-hour18.tar.gz\r\nmv minecraft-hour16.tar.gz minecraft-hour17.tar.gz\r\nmv minecraft-hour15.tar.gz minecraft-hour16.tar.gz\r\nmv minecraft-hour14.tar.gz minecraft-hour15.tar.gz\r\nmv minecraft-hour13.tar.gz minecraft-hour14.tar.gz\r\nmv minecraft-hour12.tar.gz minecraft-hour13.tar.gz\r\nmv minecraft-hour11.tar.gz minecraft-hour12.tar.gz\r\nmv minecraft-hour10.tar.gz minecraft-hour11.tar.gz\r\nmv minecraft-hour9.tar.gz minecraft-hour10.tar.gz\r\nmv minecraft-hour8.tar.gz minecraft-hour9.tar.gz\r\nmv minecraft-hour7.tar.gz minecraft-hour8.tar.gz\r\nmv minecraft-hour6.tar.gz minecraft-hour7.tar.gz\r\nmv minecraft-hour5.tar.gz minecraft-hour6.tar.gz\r\nmv minecraft-hour4.tar.gz minecraft-hour5.tar.gz\r\nmv minecraft-hour3.tar.gz minecraft-hour4.tar.gz\r\nmv minecraft-hour2.tar.gz minecraft-hour3.tar.gz\r\nmv minecraft-hour1.tar.gz minecraft-hour2.tar.gz\r\nmv minecraft-hour0.tar.gz minecraft-hour1.tar.gz\r\n\r\ntar -cpvzf \/home\/mc\/Minecraft\/Backups\/Server1\/minecraft-hour0.tar.gz \/home\/mc\/Minecraft\/Games\/Server1 --exclude '\/home\/mc\/Minecraft\/Games\/Server1\/plugins\/dynmap'\r\n\r\nscreen -R Server1 -X stuff \"save-on $(printf '\\r')\"\r\nscreen -R Server1 -X stuff \"say Backup complete. World now saving. $(printf '\\r')\"<\/pre>\n<p>And here&#8217;s the result, when it all comes together!<\/p>\n<p><a href=\"https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/2013-08-17_15.55.04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-580 alignnone\" src=\"https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/2013-08-17_15.55.04-480x299.png\" alt=\"A minecraft backup taking place.\" width=\"480\" height=\"299\" srcset=\"https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/2013-08-17_15.55.04-480x299.png 480w, https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/2013-08-17_15.55.04-768x479.png 768w, https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/2013-08-17_15.55.04-640x399.png 640w, https:\/\/www.scaine.net\/site\/wp-content\/uploads\/2013\/08\/2013-08-17_15.55.04.png 1019w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><\/p>\n<p>Of course, you can do this for your daily backups too and enjoy the piece of mind that you can roll back any of your servers to any hour in the last 24 hours, or perhaps any midnight in the last 7 days! Just &nbsp;ensure that you have enough disk space for the undertaking!<\/p>\n<h2>Restore<\/h2>\n<p>Of course, a backup isn&#8217;t much use with a way to restore it. But remember that the <strong>tar<\/strong> command we ran just copied the whole directory, so while there&#8217;s probably some ninja-script-fu that you can run to restore everything, I have to admit that for the rare cases I need to restore, I&#8217;m just lazy. Here&#8217;s my procedure :<\/p>\n<p>1. Stop the minecraft server you&#8217;re about to restore.<\/p>\n<p>2. Rename the directory from Server1 to Server1-old<\/p>\n<p>3. Find the backup tar file that you&#8217;re going to use to restore your server. Right click on that file and choose &#8220;Extract Here&#8221;. You will see a new Server1 directory appear in your backup directory.<\/p>\n<p>4. Cut that Server1 directory from your Backups directory and paste it into your Game directory.<\/p>\n<p>5. Start the server.<\/p>\n<p>Note that if you&#8217;re restoring an hourly backup and you used my &#8220;exclude&#8221; option, then you&#8217;ll also have to restore everything you excluded from the Server1-old directory.<\/p>\n<p>Once you&#8217;re happy that the server is working as you expect, you can delete the Server1-old directory, or perhaps consign it to an archive directory for prosperity, as a dark reminder of what can go wrong in the world of Minecraft.<\/p>\n<p>If you have questions, shout me in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wrote an earlier post on how to create a Minecraft Server on Ubuntu. It&#8217;s been pretty popular, so this article will describe a very simple way to ensure that you take hourly automated backups of your Minecraft server(s). This is incredibly useful for a number of reasons : Some random punter has griefed your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,89,8,4],"tags":[123,125,124],"class_list":["post-579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","category-minecraft","category-software","category-ubuntu","tag-screen","tag-stuff","tag-tar"],"mb":[],"mfb_rest_fields":["title"],"_links":{"self":[{"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/posts\/579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/comments?post=579"}],"version-history":[{"count":14,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":808,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/posts\/579\/revisions\/808"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/media?parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/categories?post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scaine.net\/site\/wp-json\/wp\/v2\/tags?post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}