Automated Minecraft Server Backup for Shared Hosting

In my There’s PowerShell In My Marzicraft! post I shared my script that creates a backup of the server and then uses OneDrive to upload it offsite. In this post, I’ll share my script to backup a Minecraft server that’s on shared hosting and therefore I only have limited access to.

The server in question has a web admin front end and FTP access. I have no access to the terminal or desktop. I’ll be using WinSCP to download the files over FTP, as WinSCP can be scripted, which means I can automate it.

First, I’ll download WinSCP – the portable executable. I’ll need to setup a connection profile to the server to be able to script it later.

Now I’ll create a plain text file, this is the script for WinSCP. Below is the script I created.

option batch continue
synchronize local -delete -mirror "C:\Backups\Master" -criteria=time -filemask="| /logs/"

The script above tells WinSCP to open the saved site name and then synchronises the Minecraft server with the local copy. The synchronize option downloads the changes and new files and removes files that are no longer on the server. In short, it’s an FTP implementation of robocopy /MIR. Notice the -filemask option, this tells it to ignore the logs directory.

Now I need to create the bat script to run WinSCP with it’s own script.

C:\winscp571\WinSCP /script=C:\winscp571\scripts\WinSCP-ServerBackup.txt > C:\winscp571\scripts\logs\Backup.log

In this script, I’m running WinSCP with the path to the script we created earlier and logging the output for future reference.

In order to have a history of backups, I have another PowerShell script that compresses the backups into zip files, names them after the date, and then removes old backup files. Below is that script.

# -------------------------------------------
# Script: Minecraft-DailyBackup.ps1
# Author: Mike Galvin
# Date: 18/02/2017
# -------------------------------------------

$Server = "C:\Backups\Master"
$Backups = "C:\Backups\Backups"

Add-Type -AssemblyName ""
[io.compression.zipfile]::CreateFromDirectory("$Server", "$Backups\Backup-{0:yyyy-MM-dd-HH-mm}.zip" -f (get-date))
Get-ChildItem –Path $Scripts –Recurse | Where-Object CreationTime –lt (Get-Date).AddDays(-8) | Remove-Item

This script gets the synchronized server backup from FTP, creates a zip file from it and names it after the date and time of when the script runs. It then removes files older than 8 days from the current date.

So, as you can see, due to not having the required access to the Minecraft server’s host, I’ve just used another computer to bring the files (only the changed files, to keep bandwidth usage and the duration to a minimum) down to another computer, and then from that created a daily rotating backup. I’ve created a couple of Scheduled Tasks for these scripts and have a fully automated backup for the server.


Follow Mike on Twitter: @Digressive

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s