Backing up Minecraft

Here’s a tiny shell script I wrote to copy the current world to a timestamped folder. It’s relying on the fact that the third line of the file contains a ‘world name’ parameter. This lets me scoop based on what map we’re actually playing on; In the case I’ve swapped it out for a fresh world.

cd /home/switch/mc/
WORLDNAME=`cat | head -3 | tail -1 | \
awk 'BEGIN { FS = "=" }; { print $2 }'`
cp -r $WORLDNAME "/var/minecraft/$WORLDNAME-"`date +%m-%d-%y`

EDIT: See comments for new non-embarrassing version

This entry was posted in Work. Bookmark the permalink.

2 Responses to Backing up Minecraft

  1. Chris walker says:

    Why wouldn’t you just grep for like world name = .*$ or whatever the wacky regexp notation grep uses? Or even use sed to find it and parse it (sed is the devil).

  2. mrgrinick says:

    I used awk because it’s the sibling who’s syntax I remember. God knows why a few slashes in sed are harder to recall than that whole ‘BEGIN F87 {}{}’=’FS” crap…

    As far as the head/tail thing goes, I had just realized you could cut lines like that, and that’s what inspired this script in the first place.

    I’ve since swapped it out for grep and added a little logging file. The new one looks like this:

    #Copy current world from /home/switch/mc to /var
    cd /home/switch/mc/
    #rip current world name from config file
    WORLDNAME=`grep level-name < | awk 'BEGIN { FS = "=" }; { print $2 }'`
    #find the most recent backup that starts with the same name
    BACKUP=`ls -lt /var/minecraft | awk '{ print $9 }' | grep ^$WORLDNAME | head -1`
    CURRENTDATE=`date +%m-%d-%y`
    if [ $WORLDNAME -nt /var/minecraft/$BACKUP ]
    echo 'world is newer than most recent backup, making new copy'
    cp -r $WORLDNAME "/var/minecraft/$WORLDNAME-$CURRENTDATE"
    echo "$FULLDATE - Copied files to $WORLDNAME-$CURRENTDATE" >> /var/minecraft/log
    echo 'current backup is same age; nothing doing'
    echo "$FULLDATE - Skipped backup of $WORLDNAME" >> /var/minecraft/log

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s