Automatic File Backups

/
0 Comments
In this post, I will be showing you how to create a small program to backup your files, and how to make this program run on a schedule.
This is an instruction for windows machines (I can post one for Unix machines later if requested).

What you need:
  • Windows Machine with an administrator account (Non administrators can do a backup, but not on a schedule.)
  • 7-zip
  • Something to back up onto, this could be an external hard drive, a USB flash memory stick, some cloud service, or some sort of Network drive. I will be using a network drive, which I can make another post if requested to explain how this is made. You should never back up to an internal drive, if the computer dies, there's a chance to loose the data on the drive.

There are only two types of people in the world, those who have lost data, and 
those who will.

First off, you need to figure out where your backup will be placed, and where it is coming from. For me, the backup is going to a folder on a network drive, the location for this is "\\Home-net196\dbak\", and the location of the files that I am backing up is "C:\Users\Sehr Ludwig\Documents\School\Semester 2".
To determine where you are putting the backup files, find the folder that you want to put the back up into, if it is on an external drive of some sort, it will be something like this "E:\Backup\" where "E" is the drive letter of the device, and "Backup" is the name of the folder where you will be storing the data.
To determine where the files you are backing up, locate them in your file explorer, right click on the file/folder, click on "properties", then copy the text after "Location:".
Save these file paths (Locations) for use later on in the project.

Now what we will do, is create the backup program. We are going to create a Batch file, which is basically just a "batch" of commands that Command Prompt will execute in order.
To create a Batch file, open Notepad, and before we begin the code, click "File", "Save as...", "Save as type", "All Files (*.*)", then name the file "Backup.bat". If you need to, save the file where you want to access it, such as "C:\Users\USERNAME\Documents\".
Now you have a blank Batch file to work with, we will begin to code.
What this program will do, is tell the program 7-zip to do the backup, yes, its that simple... Or is it?
The code is as follows, but you will need to change things for it to work on your computer:
<p><em>@echo off</em></p><p><em>"C:\Program Files\7-Zip\7z.exe" a -t7z "  ~Backup Save Location~ ~File Name ~ %DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%.7z" "   ~Backup Source Location ~  "</em></p>
The first thing "@echo off" just tells the Command Prompt not to display the parts we don't want to see. The second line is the important one.
"C:\Program Files\7-Zip\7z.exe" is the location of the program 7-Zip, Yours will probably be in the same location, but if it isn't, then change this to fit what you need.
a -t7z Just tells the program 7-Zip what we are doing with it.
" ~Backup Save Location~ ~File Name ~ %DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%.7z" is the location where 7-Zip will save to, and the file name with the date written and .7z at the end. mine is "\\Home-net196\dbak\S2BK%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%.7z"
and finally, " ~Backup Source Location ~ " is the location of the files you want to back up. Mine is "C:\Users\Sehr Ludwig\Documents\School\Semester 2"
When you are writing code for Command Prompt, you should always put quotes around things that have spaces in them, thus, the quotes around file location paths in this code.
Once you have all this done, save the file, and double click "Backup.bat" to test it out. Check to see if the backup was made, and if you can take the files back out of it.
If you aren't going to make this a scheduled task, then you can be done here.
For reference, here is the code I used:
@echo off
"C:\Program Files\7-Zip\7z.exe" a -t7z "\\Home-net196\dbak\S2BK%DATE:~7,2%.%DATE:~4,2%.%DATE:~-4%.7z" "C:\Users\Sehr Ludwig\Documents\School\Semester 2"
Now we will set the scheduled task. For this part you will need to be a user in the administrators group.
Go to "Control Panel", then "Administrative Tools", and then "Task Scheduler". This is where the magic happens.
In Task Scheduler, click the menu button "Action", then "Create Task...".
Once you are in the "Create Task" window, name the task accordingly, add a description, and then click the radio button (Little circle) for "Run whether the user is logged on or not".
Now go to the "Triggers" menu. In here, you will add what triggers this task. I will show you how to make it run every week, but if you want something different, feel free. Click the button "New...", then click the radio button "Weekly", then select the day of the week, and the time that you want the backup to happen. You will HAVE to turn the computer on before this, otherwise it will not happen, and you will not have the backup, you will also have to make sure that the backup location is connected as well, so plug your USB stick in before this happens.
Click "OK"
Now co to the "Action" Menu. In here, you will add a link to the Batch file. Alternatively, you can put the contents of the Batch file in here, but that is more complicated, and you wouldn't be able to set it to backup by clicking the Batch file. To link to the Batch file, click "New...", then click "Browse...", then find the Batch file, then click "Open", then click "OK", then click "OK", you will probably be prompted to put in your Username and Password, click "OK" after putting your credentials in.
You have successfully created a backup program, and set it to backup on a schedule. Now you can be more secure with your data.
You can rely on this to work, but you should check that it is still functioning properly every once and a while to make sure.
I hope this helps to prevent data loss.



You may also like

No comments: