Blog

Go Back

(Windows) How to Schedule Zipped Backups of a Folder

I own and operate a public Minecraft server and one of the things I struggle with is backups. I want to make sure that if the server gets griefed or hacked, my house burns down, or my hard drive dies, my world is still safe. Here is a tutorial on how to build a batch file that will copy and zip those files and move them to Google Drive to be synced to the cloud.

Create a file with the extension of “.bat”. I called mine “backup.bat” and open it with your preferred text editing software (I used Notepad)

On the first line, type the following:

@echo off

This means to not display every command that is being run in the command prompt window.

Now, create a directory on your hard drive. I used the directory “D:\tmp.backup” but you can use anything that works for you. This is where your batch file and temporary files will be stored.

On the next line in your batch file, type the text below, replacing my directory with your directory.

cd D:\tmp.backup

This tells the program where to look for and place the temporary files.

Now, type this code, replacing the first path in quotes with the directory you want to copy, and the second path in quotes with your temporary directory, but add a backslash and the name of the folder you are copying.

echo d | xcopy “DIRECTORY TO COPY” “TEMPORARY DIRECTORY\FOLDER NAME”

If the directory you are copying has subfolders, add a new line for each of those subdirectories.

echo d | xcopy “DIRECTORY TO COPY\SUBDIRECTORY” “TEMPORARY DIRECTORY\FOLDER NAME\SUBFOLDER”

The next two lines of code are used for adding a timestamp to the name of the file.

set hr=%time:~0,2%
if “%hr:~0,1%” equ “ ” set hr=0%hr:~1,1%

If you do not want the timestamp, do not add these lines to your batch file.

Go to http://www.7-zip.org/download.html and click the download that says “7-Zip Command Line Version”. Copy the executable file out of the downloaded zip folder and put it in your temporary directory. The code for zipping the folder will not work unless you download this!

7za a -tzip FOLDERNAME_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.zip “FOLDERNAME”

Use this code if you want the timestamp. The name of the zip folder will look something like “FOLDERNAME_2017_02_20.zip”.

If you do not want the timestamp, use this code:

7za a -tzip “FOLDERNAME.zip” “world”

Paste the below code in your batch file to copy the zipped directory to Google Drive, or whatever backup service you are using.

echo yes | xcopy FOLDERNAME_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.zip “FOLDER-IN-BACKUP-SERVICE\FOLDERNAME"

If you didn’t use the timestamp, use this code:

echo yes | xcopy FOLDERNAME.zip “FOLDER-IN-BACKUP-SERVICE\FOLDERNAME"

Now, use this code to delete the zipped folder that still remains in the temporary folder

echo yes | del FOLDERNAME_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.zip

Again, if you didn’t use the timestamp, use this code:

echo yes | del FOLDERNAME.zip

Use this code to delete the copy of the folder in the temporary directory

echo yes | rmdir /s “TEMPORARY DIRECTORY\FOLDER NAME”

Last but not least, type the word ‘pause’ in on the last line. This tells Windows not to close the window after it finishes

pause

Here is a copy of the code file with the timestamps”

@echo off
cd TEMPORARY DIRECTORY

echo d | xcopy “PATH OF FOLDER TO COPY” “TEMPORARY DIRECTORY\NAME OF FOLDER TO COPY”
echo d | xcopy “PATH OF FOLDER TO COPY\SUBDIRECTORY” “TEMPORARY DIRECTORY\NAME OF FOLDER TO COPY\SUBDIRECTORY”

set hr=%time:~0,2%
if “%hr:~0,1%” equ “ ” set hr=0%hr:~1,1%

7za a -tzip FOLDERNAME_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.zip “FOLDER NAME”

echo yes | xcopy FOLDERNAME_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.zip “FOLDER IN BACKUP SERVICE”

echo yes | del FOLDERNAME_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.zip

echo yes | rmdir /s “TEMPORARY DIRECTORY\FOLDER NAME”

pause

Here is a copy without the timestamps:

@echo off
cd TEMPORARY DIRECTORY

echo d | xcopy "PATH OF FOLDER TO COPY” “TEMPORARY DIRECTORY\NAME OF FOLDER TO COPY”
echo d | xcopy “PATH OF FOLDER TO COPY\SUBDIRECTORY” “TEMPORARY DIRECTORY\NAME OF FOLDER TO COPY\SUBDIRECTORY”

7za a -tzip FOLDERNAME.zip “FOLDER NAME”

echo yes | xcopy FOLDERNAME.zip “FOLDER IN BACKUP SERVICE”

echo yes | del FOLDERNAME.zip

echo yes | rmdir /s “TEMPORARY DIRECTORY\FOLDER NAME”

pause

Now, open Task Scheduler and click “Create Basic Task” on the left. Type in a name and description, and choose how often you want the backup to happen.

When you get to Action, click “Start a program”. Click Browse, and find your batch file. Click Next and Finish. Enjoy your backups!


Back to Top