Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

How to create an incremental backup of your worlds

Discussion in 'Tutorials' started by drkrieger, Jul 11, 2016.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. drkrieger Apprentice Engineer

    Messages:
    407
    Hello folks!

    I've seen a lot of folks who have had the unfortunate luck of losing their world/creations to crashes, game bugs etc. Not pointing a finger at KSH, you guys are awesome, keep up the good work!
    However, crashes and world failures are a reality being a alpha game. Here's a how-to guide on creating a backup that will keep a snapshot of your world every few minutes (adjustable).

    Be warned, this will consume a fair bit of hard drive space depending on how big your world are, and how frequently you set it to run the backup. Mine chews up about 21GB for a few months worth of backups. You can trim the folders down manually if they get too big, or set the task to a less frequent time (you'll see later on).

    Requirements:

    7zip command line - a free compression utility that frankly, kicks ass.
    A Windows 7 or newer operating system, running powershell version 1.0 or later
    You can check you version of powershell by doing this: http://stackoverflow.com/a/1825807


    ***DISCLAIMER: While this backup script has worked nearly flawlessly on my computer, you may have varying results based on your computers settings. However unlikely this may be, I'm not responsible for computer failures, loss of backups, loss of files, loss of time, loss of life, etc. I'm no expert in scripting, and I expect the odd person to run into issues. I also expect many folks who do know powershell scripting to pick this script apart until the cows come home. I created this script by spending a few hours on Google piecing together functions and such from the awesome folks over at StackOverflow.

    Alrighty, with all that outta the way, lets get down to brass tacks.

    First, extract 7zip to a folder that will never be renamed or modified. This is critical because it'll break this script if you ever do remove the folder or rename it or its contents (mainly the 7zip executable).

    Second, you need to create a folder in which you want to store your backups in. This folder will have sub-folders created in it with dates of the backups. Create your folder layout appropriately to your liking. This is how I did mine:
    D:\SpaceEngineers_World_Bak

    If you create a director named this directory on your D:\, you can reduce the amount of modification you need to do to the script.
    Since I only ran this backup script for my dedicated server, I didn't create any sub-folders for other worlds. This script has minimal impact on system performance and won't cause it to slow down much at all during a backup. This may vary depending on how ridiculously large your worlds get (I'm pointing at you @gearheadkinny2, @Commander Rotal). The only slowdown is when 7zip compresses the world file, but if you have a good quad-core CPU, you're fine.

    Ok, once your folder is made, you need to create the powershell script. You can do this in notepad, or you can do this in PowerShell ICE (my preferred method as it will highlight stuff appropriately with colored text). Click start and type notepad or Powershell, then run either one.
    Paste the following blue italicized text into the window:

    # creating variables
    $a=get-date -format "dd.MM.yyy_%h.%m%t"
    $d=get-date -Format "dd.MM.yyy"
    $cleandate=(get-date).AddDays(-5).ToString("dd.MM.yyy")

    # Creating path for backup Directory if it doesn't exist
    if (!(Test-Path -pathType Container -Path "
    D:\SpaceEngineers_World_Bak\$d" )){
    New-Item -ItemType directory -Path "
    D:\SpaceEngineers_World_Bak\$d"
    }

    # Copying the config file to root of backup directory
    Remove-Item -Path "
    D:\SpaceEngineers_World_Bak\SpaceEngineers-Dedicated.cfg"
    Copy-Item -Path "
    C:\ProgramData\SpaceEngineersDedicated\Krieger's World\SpaceEngineers-Dedicated.cfg" -Destination "D:\SpaceEngineers_World_Bak" -Force

    # Zipping up the directory that needs backing up (in a nice convenient date formate)

    D:\Downloads\7za920\7za.exe a -t7z "D:\SpaceEngineers_World_Bak\$d\$a.7z" "C:\ProgramData\SpaceEngineersDedicated\Krieger's World\Saves\Ft. Kick Ass wPlanets!" -m0=lzma2:D=512m:fb=273 -mmt=2 -ms=on

    # Checking to see if there is a directory older than 7 days
    if (!(Test-Path -pathType Container -Path "
    D:\SpaceEngineers_World_Bak\$cleandate" )){
    exit
    }

    # Checking to see how many files are in the old backup, if already cleaned (one file) then exit
    $filecount=(Get-ChildItem
    D:\SpaceEngineers_World_Bak\$cleandate).count
    if ($filecount -lt 2) {exit}

    #Cleaning old backup down to single file per day (after 7 days)
    $exemptfile=gci -recurse
    D:\SpaceEngineers_World_Bak\$cleandate | sort LastWriteTime | select -last 1
    Remove-Item -Recurse
    D:\SpaceEngineers_World_Bak\$cleandate\*.* -Exclude $exemptfile


    Ok, now you've got this in here, you need to modify the directories that items are stored in and copied from. You also need to modify your 7zip location. If you read the code carefully, you'll see command paths listed. The one for 7zip's command line is highlighted in GREEN. Adjust this to match your extracted location. Don't remove any quotations from the script, it'll break if you do!
    Next, modify the script where it points to the location you wish to save the backups to, marked in RED.
    Last, modify the directory where you would like to take a copy of, marked in GREY. Make sure you clip the subdirectory (worlds name) from the path on the first grey item as it's only copying the config file (I bet this part is done wrong overall, but still works. Maybe it's redundant?).

    Right, so you've modified the code. SAVE IT. Lets call it 'SE-Backup.PS1'. The file extension is VERY important if you are using notepad, choose 'All Files', then put in the full extension (basically, add the '.ps1' at end of the filename).

    Kewl, got the script done. Next, you need to allow Windows to run the script. By default, Windows will not allow powershell scripts due to how powerful the powershell command system is (it can literally do anything to your windows, good and bad). To do this, you need to run Powershell as an administrator. Do this by click on Start, typing Powershell, then right clicking on 'Powershell' (not the ICE) and selecting 'Run as Administrator'. You'll now see a blue command window. Type this:
    Set-ExecutionPolicy RemoteSigned

    You'll be prompted to a yes/no question, just press 'y' and enter. You can now run the script!

    Lets make it even simpler. Lets use Windows Task Scheduler to run the script at certain intervals. Next, press Start, then type 'task' and pick the 'Task Scheduler'. From the Action Menu on the right (maximize the window if you can't see it), pick 'Create Task' (NOT the 'Create Basic Task').
    1. In the new window, add a name for the task, description is optional, and select 'Run only when user is logged on'. All other settings are default.
    2. Click on the 'Triggers' tab on the top. Select 'New...', then change the 'Begin on Task' to 'On an event'. Click the 'Edit Event Filter' button in the middle, then click the XML tab. Make sure there's a check box in the 'Edit Quesrry Manually' box at the bottom. Paste this text in the window:
      <QueryList>
      <Query Id="0" Path="System">
      <Select Path="System">
      *[EventData[Data[@Name='param1'] and (Data="Space Engineers")]]
      and
      *[EventData[Data[@Name='param2'] and (Data='running')]]
      </Select>
      </Query>
      </QueryList>
    3. Click 'OK' to save. Now check the 'Repeat Task every' box, and pick the frequency that you'd like to back up the world (I'd recommend no less than 10 minutes, it might duplicate a backup file as auto-save defaults to 5 minutes I believe).
    4. Make sure the 'Enabled' has a check in the box, then click 'OK' at the bottom of the window.
    5. In the 'Actions' tab at the top, click 'New'.
    6. Set the 'Action' to 'Start a Program', and in the 'Program/Script' field, type: powershell.exe
    7. In the 'Add Arguments' field, add this: -file "<PATH TO THE PS1 FILE\SE-Backup.PS1>" (no pointy brackets!)
    8. Click 'OK', then 'OK' again.
    If for some reason the task doesn't run, the 'Data="Space Engineers" ' is wrong (mine's set to run on the 'Service' that is launched when I start my dedicated server). Someone with more knowledge of Windows can hopefully chime in and tell me if that's not correct.

    And that's it! Whewph... took forever, I know.

    But... how to restore the backups?
    Easy peasy.

    Install 7zip (the full version, not command line- get it HERE). After it's installed, you can simply right-click-drag one of the backup files in one of your backup folders to the root of the folder containing your world.
    For Dedicated Server, your worlds are stored here (You need to show 'Hidden Folders'): C:\ProgramData\SpaceEngineersDedicated
    For regular game, your worlds are stored here (again, hidden folders): C:\Users\Media\AppData\Roaming\SpaceEngineers\Saves\<YOUR STEAM ID NUMBER>\

    I'd recommend deleting the bad world folder, then right-click-drag the backup *.7z file to the folder above and chosing 'Extract Here'. This will restore you world!

    *Jazz Hands*
     
    Last edited: Jul 11, 2016
Thread Status:
This last post in this thread was made more than 31 days old.