Jump to content

Server Backup Batch File


LostSoulFly

Recommended Posts

This should work with any server, pretty much. Or anything, really.

 

I needed to do some simple backups for my server because stuff happens.

 

I found a nifty program called HoboCopy online. It's not hard to find; just google it. I used Hobocopy because xcopy and robocopy doesn't like files that are locked or in use, and they would be skipped.

 

This method seems pretty reliable in that it will copy everything, regardless of it's lock status.

It is also an incremental backup, because it uses a base zip file to store the snapshot and then each backup after that just includes the differences from the last backup.

 

Here we create the Base zip and the statefile for Hobocopy. (Statefile is where Hobocopy stores what has already been copied and when, so it knows what to copy next time.)

@echo off
REM ::BaseBackup.bat
REM ::Creates a base image backup for use in Hourly.bat
REM Create base statefile

title Base backup.

cd "c:UsersDragoonDesktop"

echo Creating base image now..

HoboCopy.exe /statefile=d:state.txt /verbosity=0 /r /y "MC" "Base" | FIND "complete"
7za.exe a -tzip -y "D:BackupBase.zip" .Base* | FIND "ing archive"
REM delete the Base directory.

rmdir /S /Q Base

You'll need to edit the directories, of course. I'm using some external storage for this backup to be safe.

 

Next you start the Hourly backup: (Edit: added rmdir for Incremental directory. Without it, the incremental just gets added to the backup regardless, making ZIPs larger than necessary.)

@echo off

REM Hourly backup

REM we need to run as admin, so hobo can do a backup of active files.

cd "c:UsersDragoonDesktop"

REM TODO check if d:state.txt exists here

REM goto start

:newbase

echo   NEWBASE

FOR %%A IN (%Date%) DO (

    FOR /F "tokens=1-3 delims=/-" %%B in ("%%~A") DO (

        SET Today=%%B%%C%%D

    )

)
echo   checking for previous backup..

ren "d:Backup" "Backup_%Today%_%time:~0,2%%time:~3,2%%time:~6,2%">nul

echo   removing old statefile..

del d:state.txt>nul
echo Begin new BaseBackup

call BaseBackup.bat

echo End new BaseBackup

REM we just did a backup in the form of a BASE image. Skip the first one!

goto sleep
:start

SET COUNTER=0

echo   Starting initial backup. Set to write backups every hour.

:loop

SET /a COUNTER=%COUNTER%+1

TITLE Internal hourly backup: %COUNTER%/96

echo Starting backup %COUNTER%..
HoboCopy.exe /statefile=d:state.txt /incremental /verbosity=0 /r /y "MC" "Incremental"
7za.exe a -tzip -y -r "D:BackupIncremental_%COUNTER%.zip" .Incremental*.* -x!*.log.* | FIND "ing archive"
rmdir /S /Q Incremental>nul
:sleep

echo Backup %COUNTER% done.

echo   Sleeping for 1 hour..

PING 1.1.1.1 -n 1 -w 3600000 > NUL

if '%COUNTER%'=='96' goto newbase

goto loop 

This uses the statefile to check what has changed and create a Incremental_xx.zip file on the same external drive.

 

 

Now, if you want to rebuild a backup:

@echo off
title Restore.
REM Set the maximum depth of incrementals to go.
SET Max=9
REM set the number to start from. Usually, if you want all changes (and to be certain everything works) keep it 1.
SET Start=1
SET COUNTER=%Start%
echo Extracting BASE first..

SET /a Max=%Max%+1

7za.exe x -y "D:BackupBase.zip" -o"C:UsersDragoonDesktoprebuilt" | FIND "ing archive"

:loop

7za.exe x -y "D:BackupIncremental_%COUNTER%.zip" -o"C:UsersDragoonDesktoprebuilt" | FIND "ing archive"

REM Increment the counter; goto next ZIP
SET /a COUNTER=%COUNTER%+1

if '%COUNTER%'=='%Max%' goto end
goto loop

:end
echo Done!
pause

This will take a few arguments at the top of the file, such as where to start in the incremental backups and how far to go. It effectively unzips 1-9 incrementals into a folder on your desktop.

 

I know there are some things I can change or add to make it better, and if anyone wants to add to it, please do!

 

Edited again. Added in some new stuff to the hourly backup; now it is pretty much automated.

Edited by LostSoulFly
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...