Implementing a periodic backup strategy for word documents, spreadsheets, notes, images, photos or any other files or directory of files of interest is available to Windows users. Simply implementing a batch file that copies the file from one location to another and utilizing the Windows Schedule Task facility can accomplish this task.
Let's take a quick use case, you have a spreadsheet that is used for some type of data updated daily. Backing up this file at the end of the day is the primary requirement. Further, having the ability to keep a backup separately for review, in its original content, is a the secondary requirement. The last requirement suggests a naming convention for the back up file to refer to a specific day's backup from another. This can simply be resolved by a naming convention. The file name backup could simple be named based on date and time to achieve a unique name.
The example we'll use here is a backup of an ordinary spread sheet. Each backup will be saved to a specific directory with a unique file name composed of Date+Time+FileName.xls. Further, the backup will occur every day at a specific time. The way we implement this operation will be conducted through the Windows Schedule Task facility and a common batch file.
In order to instruct the Window Scheduler to run a task we must implement a batch file to create the unique backup file name and copy the spreadsheet to the backup directory. The only complication that we have here is the unique file name. The quickest solution to this is to base the file name on date + time + the filename. This can be done simply in DOS.
Lets create a date string with out the "/" or "-"
set myTargetFileDate=%date:~0,3%%date:~4,2%%date:~7,2%%date:~10,4%
This would generate : Sat04012010
Creating a time screen with out the ":" is implemented as
set myTargetFileTime=%time:~0,2%%time:~3,2%%time:~6,5%
This generates : 145023.54
Now simply concatenating a filename is the next step.
set aTargetFileName=MyExcelFile.xls
Now the concatenating these variables
set myTargetFileName=%myTargetFileDate%-%myTargetFileTime%%aTargetFileName%
The filename is now : Sat03012010145023.54MyExcelFile.xls
Now, using this method in allows the batch file to have a unique file name to utilize in any operation. With a little imagination this method could be used to create a unique directory structure, archive name or anything else that requires uniqueness.
Note, this method is completely in the context of one run a day and no other operation of this sort is anticipated to be run simultaneously. Be FOREWARNED, if you use this method for additional in tasks insure to take into account naming collisions.
Now the batch file name is establish its a matter of copying the file to the backup location desired.
ie: copy c:\data\files\somedatefile.xls C:\data\backup\%myTargetFileName%
A quick batch file is created below that we can use to copy a file from one directory to another. End result of every call to this batch file will create a new file based on date and time of execution. This is a snapshot of the targeted file.
@echo off Set myFileName=MyExcel2007.xls rem echo %myFileName% set myFileDate=%date:~0,3%%date:~4,2%%date:~7,2%%date:~10,4% rem echo %myFileDate% set myFileTime=%time:~0,2%%time:~3,2%%time:~6,5% rem echo %myFileTime% set myTargetFileName=%myFileDate%-%myFileTime%%myFileName% rem echo %myFileName% Echo BACKUP s:\fileserver\share\somespreadsheet.xls TO Echo c:\backup\data\myspreadsheets\%MyFileName% copy c:\data\files\somedatafile.xls C:\data\backup\%myTargetFileName% |
Now that we have a working file setup some preliminary tasks and than setup a schedule.
- Create a directory c:\data\files
- Create a file names someDataFile.xls
- Create a backup directory c:\data\backup
- Create a batch file DTStampBackup.bat in c:\data
First to a directory in C:\data\files and insure the spreadsheet is there. Than do a directory of c:\data\backup this should be empty to insure everything is in order. Execute the
DTStampBackup.bat to test the batch file operates correctly. Removing the REM statements will echo the file name parts upon creation. If every thing ran correctly the backup directory there should have at least one file that is a copy of the someDataFile.xls.
Now let's schedule a task. . .
First, going to the Windows Start Button, selecting the Control Panel and select Scheduled Tasks and click on Add Scheduled Task. This displays the Scheduled Task Wizard offering an Application for selection or a browse button. The "Browser Button" is are interest here. Selecting "browse" use the dialog to select DTStampBackup.bat. Next type a name of the task "Backup Some File" and select a daily to perform the task. Select a time, try 3 minutes in the future and select every day.
That should be it. In about three minutes check the c:\data\Backup and there should be yet another file with a DateTimeStamp in the new filename.
The unique name creation is the key here. Now that its set in an environment variable the operations using the name can be include xcopy, making archives, connecting to web sites, uploading files etc... This makes things a lot better.
tags: dos unique version backup copy microsoft windows
links: digg this del.icio.us technorati reddit