Tuesday, August 25, 2009

simple backup system using batch files

batch files are text files, filled with commands, that windows can execute, like a program

so in essence, a batch file could be considered a program

batch files can bet triggered by the windows scheduler, so if you wanted to, you could schedule the backup script we're going to make, so that it backs up your files each night!

batch files are easy to make, simply create a new text file, then rename it to funtest.bat, instead of .txt, the extension will need to be changed to .bat, this tells the computer that it can run the file instead of just opening it up in notepad or ms word or what have you

one of the quickest ways to do this is to right-click in windows explorer, select "new", then select "text document"

now we need to right-click the file we just created, and select "rename"

you can name the file anything you'd like, the most important part is the extension though, the extension must be renamed from .txt to .bat, after you rename your file and change the extension, you will be warned by windows that changing an extension of a file may make the file open in the wrong program or just not open at all, since we are doing this on purpose it is completely okay to say yes here

now we have a fresh batch file! congratulations! unfortunately it will not be of much use until we add some commands, right-click the batch file and select "edit" to add some code and get our batch file working for us

we're going to keep it simple with the commands, there are many websites out there to help you get acquainted with the command syntax, i recommend this one for a basic list of most available commands on the windows platform:

we're going to keep it simple, we are making a folder with our first batch file, here is the code to use:
mkdir awesome_folder

the mkdir command makes a directory, whatever follows the command will be what the new directory is named, so with this command we will make a new directory named "awesome_folder"

type the code into your batch file and then save it

now we are ready to DO SOMETHING here! double click the batch file you just added code to, you may see a black cmd prompt pop up for a split second (that is the batch file running!), a moment later we will be seeing a new directory named "awesome_folder"

bam! we just wrote a very basic program and had it do a bit of work for us.

now let's take it a step further, awesome folder has important information contained therein. we NEED to make a backup of this folder at times, wouldn't it be nice to double click a little batch file in order to make it do the work for us?

here is the code to use:
xcopy awesome_folder c:\temp\BACKUP_awesome_folder /y /e /i

the xcopy command lets us copy files and folders as well as subfolders and any files within the folder structure, we are telling it to copy everything from "awesome_folder" to "c:\temp\BACKUP_awesome_folder". you will notice 3 letters with a forward slash following the command, these are switches, most cmd prompt programs allow us to use switches to change the behavior one way or another, here is an explanation of the options i've used

/y - do not ask us if it is okay to copy files if the destination already contains the files, if we do not specify /y with the command then our batch file will ask us if it's okay to overwrite files and wait for us to type a y for yes or an n for no, we want this thing to be fully automatic so adding the /y option forces the xcopy program to say yes automagically for ever file that may be overwritten

/e - this means to copy all directories, subdirectories, and any files or other subdirectories contained within the directory tree, essentially the /e switch means to copy everything, e is for everything

/i - this switch causes xcopy to assume that the destination is a folder and not a single file, you will need to specify this switch in most cases if you are copying directory trees

most programs you will use in a batch script will allow you to see what switches are available, you can usually see this by using the /h, /?, or /help switch after the command, let's see what switches xcopy has available! open a command prompt and type the command:
xcopy /?

as you can see, xcopy has a lot of switches! hittin' mad switches up in here! ;)

let's double click our batch file and give it a whirl, if all goes well, you should be seeing something like this:

that's it! you've just made a 1 click backup solution for your important files, just adjust the batch file to pull from the location you'd like to backup on your computer, then adjust the destination. you can have the backup sent anywhere, a usb key drive, a mounted network drive, a gigantic fiber attached storage area network, etc...

taking it a step further!

i like to compress my backups, it helps me save space, it is amazing how much microsoft office documents can compress for example, a nice command prompt friendly tool to help with this is winrar, you can use the 30 day trial version in your batch programs without any limitations, here's the winrar website if you haven't google'd it already:

winrar has a lot of commandline switches, by default the rar.exe program that you will need to use is located at:
C:\Program Files\WinRAR\

open a cmd prompt and go to this location, next type this command so we can see all the switches:
rar /?

woooah! that is a lot of switches!! this can get confusing really quick! i suggest playing with the switches in a test environment, make some dummy directory structures with some subfolders and files strewn about and mess about with rar there

to help you get going, here is a basic one line command that i use to help with some of my own backup jobs:
"c:\Program Files\WinRAR\Rar.exe" a -r D:\backups\my_stuff.rar C:\test\BACKUP_awesome_folder\

let's start with the first part, we are calling the rar program, rar.exe. i am using the full path, most third party windows programs will require this, unless you copy the program to c:\windows\system32\, we'd prefer not to do that, you could add an environment variable for set path's if you wanted instead but i prefer to just call the program with it's complete full path...so forget all of that system32 and environment variable stuff, let's keep it simple and just use full paths in cases like this ;)

we are giving rar.exe a command and a switch, we won't get into this in too much detail, "a -r" basically means create a new archive and add the specified files along with folders and subfolders, ie. the entire directory tree, into the archive

following that we are giving the destination, the location where we would like the compressed backup file to be located and named (xcopy does this the opposite direction! source first THEN destination, don't worry if you get confused, it is confusing, and as you get into more batch file fun you will run across more and more confusing contradictions like this)

the last portion of the command is the source, or what we would like to actually compress

let's double click our latest batch file, if all goes well we should see a my_stuff.rar file sitting in d:\backups\, open the rar file up and we should see a directory structure containing our files and folders

we can combine both of these batch files into 1, this way we can make a copy of our files, then get a compressed backup of the copy, in effect double backups, 1 live uncompressed copy and 1 uncompressed copy for archiving purposes, this is easy, we're just going to make a new batch file and add the code from the previous 2 batch files, so you should have code like this for our combined batch file:
xcopy awesome_folder c:\temp\BACKUP_awesome_folder /y /e /i
"c:\Program Files\WinRAR\Rar.exe" a -r D:\backups\my_stuff.rar C:\temp\BACKUP_awesome_folder\

one last word of advice, sometimes the batch file will fail. the cmd prompt window goes away so quickly that you can't see what the error is, to get around this you can open a cmd prompt, navigate to the folder that you are keeping your batch files in, if you then type the file name of one of the batch files and hit enter, you will see the batch file getting executed line by line, any errors that may occur should be visible now

here is a screenshot of our latest version of the backup batch script when run from within the cmd prompt

No comments:

Post a Comment