Dated Full & Incremental BACKUP In a Batch File Examples

Make comments, ask questions, or just complain about the software on this site. Or comment on any educational software.
Please note that by clicking on links that may appear in these posts that you may be leaving the Dale Harris Educational Software website and that the content of those sites is the sole resposibility of the authors of those sites.

Moderators:daleadmin, Dale Harris, Alan, Andrew

Post Reply
User avatar
brucef2112
Forum Regular
Posts:336
Joined:Mon Mar 06, 2006 11:19 pm
Location:Broward County, Floriduhh
Contact:
Dated Full & Incremental BACKUP In a Batch File Examples

Post by brucef2112 » Mon Apr 18, 2011 11:29 pm

I searched the forum for backup ideas and only found one topic that was several years old. And it was basically a batch file to do a full copy of the POS directory.
I figured I'd share what works for me and hope it helps someone. Even if your not interested in backups per say, check out the code. There are some interesting uses of the FOR command to manipulate text strings, dates and extract file information. You may be shocked by what can be done with the simple FOR command in a batch file.

OK.
One, doing backups suck. Two, finding out that you need a backup but don't have a one sucks even more! I speak from experience....
I know If its not automated, its not consistent. I know backups must be consistent because the Data Gods know when I forget and they use this against me.

Why a simple Copy command isn't what works for me.
I have several different computers, each with different flavors of Windows.
I have several stores. And for some reason or another the POS directory name is different for each.
I'm not at the stores everyday to do backups. I have friends with stores using DHPOS and I'm stupid enough to help them with their systems for free.

With the above in mind I came up with an automated backup process with the following requirements.
I don't want to spend money.
I want to use what is available in Windows.
I don't want to go around updating individual batch files.
I needed a single Batch file that works with;
a. Different flavors of Windows and service packs.
b. Different directory names where the POS is located in.
c. Does a Full Backup on the first of every month.
d. Does a Incremental backup of only files that change each day during the month.
e. And the backups have names that are user friendly by including the DATE as part of the name for easy browsing later on.

Maintaining a single batch file and just distributing it is much easier than maintaining a bunch of hard coded ones at various places and systems.

By using Windows Scheduler that comes with the OS I have a backup that auto runs every night.
On the 1st of the month it does a full backup of all files. All other days it backs up only files that have changed that day.
The backup folders are easy to identify as 'Full' or 'Incremental' and are date ordered making it easy to view then in the browser window.
A full backup would be saved to a folder named FullBkUp2011-04-01
An incremental backup would be saved to a folder named IncrmntlBkUp2011-04-20
By placing the batch file in your POS folder it always knows the source of the POS(and its subfolders).
So you just need to pass the destination drive on the command line such as: POSbkUp.BAT E:\POSBkUps\

Feel free to use as is or just hack a way and modify for your needs.
Notice I didn't even put my name in the comments to seek any credit. I don't want any.
I consider my self an idiot and a bit stupid and don't need this fact to be spread around through a broken batch file authored by me.
Of course you should test this for your own needs on some sample data and not your live POS until you are confident it works for you.
Note that the code has limited error traps. Like not passing the destination name after the bat will be a big failure of the backup.
REMEMBER, A SELF PROCLAIMED IDIOT MADE THIS BATCH FILE! USER BEWARE! <<--- LEGAL DISCLAIMER

I've added lots of comments to help explain what is going on. If the comments are stripped out, the batch file is pretty small.

If you have any questions or need help to modify for your needs (even if not for backups) feel free to ask. Remember, I work for free.
The following batch file is what I came up with. Save this into a plain text file in your POS directory and name it something clever like: POSbkUp.BAT. Add this to Windows Scheduler and your ready to go. In Scheduler you'll need to specify a user name and password to automate the running of the program. The user name specified needs administrator rights because we will be changing the archive flag of the backed up files. For me, I created a separate user account for this purpose called "Backup" and created a password. Don't try a blank password. Most often Scheduler will not run a program on a user name that doesn't have a password.

Code: Select all

@ECHO Off
SETLOCAL
:: THIS BATCH FILE MUST BE LOCATED (run from) IN THE DHPOS DIRECTORY.
:: USAGE from the DOS Prompt or within another batch file.
:: POSbkUp.BAT DRIVE:\PATH\
::    DRIVE = the drive letter of the device you want to backup to.
::    PATH  = the directory you want to save the backups into. Include the backslash!
:: 
:: EXAMPLE: POSbkUp.BAT E:\POSbkups\
::
:: If you specify a path that contains spaces it must have double quotes around it.
::
:: EXAMPLE: POSbkUp.BAT "E:\My POS Backup Location\" 
:: Don't forget to Include the backslash at the end!

:: NOTE That this is written for Windows OS that allows long file names to be used.
::  * I am using a "long file name" so its easier to ID the archived files later on.
::  * example: If its Jan. 30, 2009 it will be saved as MYFILE_2009-01-30.txt.
::  * This will keep the files in alphabetical order by year/month/day when
::  * the folder is veiwed using Windows' explorer. Easy on the eyes to browse.


:: Get the destination path passsed on the command line.
SET _DestinationPath=%1


:: IF YOU WANT TO HARDCODE YOUR SPECIFIC DESTINATION DRIVE AND PATH INSTEAD
:: OF PASSING IT ON THE COMMAND LINE, REMOVE THE 'REM' ON THE LINE BELOW AND
:: cHANGE THE DRIVE LETTER AND PATH TO MATCH YOUR BACKUP LOCATION. 
:: A Fully qualified directory name is needed. If directory doesn't exist,
:: it will be created. Obviously this needs to be standard writable drive and 
:: have plenty of space.
:: If you do hardcode your destination. THIS WILL OVERRIDE ANY DESTINATION PATH THAT
:: IS PASSED ON THE COMMAND LINE. SO YOU ONLY NEED TO CALL BkUpPOS.BAT by its self.
:: If you specify a path containing spaces it must have double quotes around it.
REM SET _DestinationPath=C:\dhPOS_Backup\


:: Lets not have this fail because user forgot to add the ending backslash.
:: Test to see if the destination path given has a backslash on the end.

:: Strip the set of quotes if they are there.
FOR %%R in (%_DestinationPath%) do SET _DestinationPath=%%~R

:: Get the last character in the path name.
SET _LastChar=%_DestinationPath:~-1%

:: If its not there, we'll add it to the destination path.
IF NOT %_LastChar%==\ SET _DestinationPath=%_DestinationPath%\

:: We'll wrap the destination path in quotes so we won't have problems passing it later,
:: regardless if its a path containing spaces in its name or not.
SET _DestinationPath="%_DestinationPath%"

:: LIKE ALMOST ALL BACKUP SOFTWARE, THIS MUST BE RUN WITH ADMINISTRATOR USER RIGHTS
:: IN ORDER FOR THE INCRIMENTAL BACKUP PROCESS TO WORK. THIS IS BECAUSE THE XCOPY
:: WILL ATTEMPT TO FLIP EACH FILE'S ARCHIVE ATTRIBUTE OFF AFTER IT IS COPIED.
:: In all cases the backup will run. However subsequent Incrimental backups will still contain
:: all files because user didn't have admin rights to flip the -A attribute.

:: I have 2 registers running on different PC and the path name to DHPOS is also different.
:: This one batch file is designed to work on both so I don't need to maintain
:: two different versions of this batch file just because the paths are different.
:: You must place this batch file in your DHPOS directory, it will figure out the 
:: source path of your POS directory (and sub directories) to backup.

:: As long as this batchfile is located (run from) inside your DHPOS's directory
:: THERE IS NO NEED TO HARDCODE THE PATH TO THE DHPOS DIRECTORY & FILES.
::  
::   * GET THE PATH TO THE DIRECTORY WHERE DHPOS (AND THIS BAT FILE) IS LOCATED.
::   * DHPOS is DOS based so there wouldn't be spaces in the path
::   * that would need to be wrapped in double quotes. But no harm in doing it.So we will...
FOR /F %%i IN ("%0") DO SET _POSDIR="%%~dpi"

:: ==============================================================     
:: Parse the system date so we can build a directory Name to include the date as 
:: part of folder name. 
:: I have 2 PCs running different versions of Windows.So the DATE is formated diffent in each.
:: This one batch file is designed to work on both OS's so I don't need to maintain
:: two different versions of this batch file to accomidate different date formats.
:: My two PC's return the date format as MM/DD/YYYY the other is ddd MM/DD/YYYY. 
:: You can verify by opening a DOS window and at the prompt type DATE /t and see what
:: the DATE format is. if its one of the above, you don't need to change anything here.
:: It will work with either date format. As is, this will work for win98 and Win XP in the USA.
:: If your computer's date format isn't MM/DD/YYYY OR ddd MM/DD/YYYY adjust this code as needed
:: for your regional settings. (I think the UK regional setting is DD/MM/YYYY, adjust as needed)
:: ==============================================================

:: Lets get the computer's DATE.
:: Have you always thought a command in a batch file MUST be on a single line? 
:: The answer is NO! Well, sort of...
:: This form of the FOR command, each SET statement below must be on its own line as it is shown.
:: Also the open and close parethises must be where they are. Mess with any of this
:: or its format and the FOR Statement won't work!!!!!! TRUST ME....
:: !!! NOTE that the DELIMS= statement below is a forward slash *AND* a space!
:: (This killed hours for me)
 
FOR /F "TOKENS=1-5 DELIMS=/ " %%A IN ("%DATE%") DO (
SET _A=%%A
SET _B=%%B
SET _C=%%C
SET _D=%%D
)


:: Now lets determine which of the two types of DATE formats was returned by Date/t.
:: DOS returns MM/DD/YYYY however some Win XP return ddd MM/DD/YYYY format.
:: If yours is not one of the two forms above, you'll need to adjust 
:: the above and below lines to work with your flavor of OS and regional location.

SET IsDayOfWeek=FALSE
:: Test for any day of the week in the var_A.
IF %_A%==Mon SET IsDayOfWeek=TRUE
IF %_A%==Tue SET IsDayOfWeek=TRUE
IF %_A%==Wed SET IsDayOfWeek=TRUE
IF %_A%==Thu SET IsDayOfWeek=TRUE
IF %_A%==Fri SET IsDayOfWeek=TRUE
IF %_A%==Sat SET IsDayOfWeek=TRUE
IF %_A%==Sun SET IsDayOfWeek=TRUE

IF %IsDayOfWeek%==TRUE GOTO ShiftVars
:: Otherwise this is the DOS date format that starts with the month of the year.
:: Varables A,B,C contain month, day, year.
SET _mm=%_A%
SET _dd=%_B%
SET _yyyy=%_C%
GOTO JUMP1

:ShiftVars
:: This is the Win XP Date format that starts with the day of the week.
:: so the varables B,C,D contain month, day, year.
SET _mm=%_B%
set _dd=%_C%
SET _yyyy=%_D%

:JUMP1
::==============================================================
::  * Build a string with the full path and file name for the destination
::  * to archive to. NOTE that I am using a "long file name"
::  * so its easier to ID the archived files later on.
::  * example: If its Jan. 30, 2009 it will be saved as MYFILE_2009-01-30.txt.
::  * This will keep the files in alphabetical order by year/month/day when
::  * the folder is veiwed using Windows' explorer.
   
::  * We will set the default to be an Incremental Backup.
::  * Lets build a destination path that includes 'IncrmntlBkUp' and the date.
SET _fnBkUpDest=%_DestinationPath%IncrmntlBkUp%_yyyy%-%_mm%-%_dd%\

::  * See if it is the first of the month.
SET _IsFirstOfMnth=FALSE
IF %_dd%==01 SET _IsFirstOfMnth=TRUE

::  * If its the first of the month do a full backup of the POS. So set Destination path as such.
::  * Lets build a destination path that includes 'FullBkUp' and the date.
IF %_IsFirstOfMnth%==TRUE SET _fnBkUpDest=%_DestinationPath%FullBkUp%_yyyy%-%_mm%-%_dd%\
 
:: /S - Copies all files in the current directory and in any subdirectory within it. 
:: /M - Copies the marked archive files. This switch checks the archive 
::      attribute of a file: if the file`s archive attribute is set to
::      off (-A), the file will not be copied.
:: /k - Copies files & retains the read-only attribute on destination files
::      if present on the source files. By default, xcopy removes the read-only attribute.
:: /V - Each file copied is verified to be sure the file stored on the destination 
::      disk is identical to the original on the source disk. 
:: NOTE THAT IF THE WINDOWS USER ACCOUNT IS NOT AN ADMINISTRATOR THEN THE ARCHIVE FLAG
:: WILL NOT BE RESET BY THE XCOPY. THIS MUST BE RUN WITH ADMINISTRATOR USER RIGHTS.
:: OTHERWISE IT WILL BACKUP THE DATA BUT THE ARCHIVE FLAGS WILL REMAIN SET AND IN TURN
:: SUBSEQUENT BACKUPS WILL CONTINUE TO BACK-UP THE SAME FILES EVEN THOUGH THEY HAVEN'T CHANGED.

:: IF 1ST OF month backup ALL FILES AND SUB DIRECTORIES, regardless of Archive flag.
IF %_IsFirstOfMnth%==TRUE XCOPY %_POSDIR%*.* %_fnBkUpDest% /S /K /V

:: IF NOT 1ST OF month backup ONLY CHANGED FILES BASED ON FILE'S Archive flag.
IF %_IsFirstOfMnth%==FALSE XCOPY %_POSDIR%*.* %_fnBkUpDest% /S /M /K /V

:END
::   * Everybody enjoys a clean environment. Lets free up the memory we used before we quit. 
::   * Clean-up All Environment Space variables that begin with underscore.
     FOR /F "DELIMS==" %%v IN ('SET _') DO SET %%v=

ENDLOCAL
:EOF
The long file names aside (adjust for 8.3 names), I'm pretty sure the commands used will also work from DOS 5.0+ (pre-Windows). So this backup could be automated by placing this at the end of a batch file that starts the POS.EXE and having it backup after exiting POS.
Later,
Bruce

They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."
Benjamin Franklin - Historical Review of Pennsylvania, 1759

User avatar
small
Forum Regular
Posts:135
Joined:Mon Sep 05, 2005 10:22 pm
Location:Carmi, Illinois

Re: Dated Full & Incremental BACKUP In a Batch File Examples

Post by small » Tue Apr 19, 2011 12:17 am

This Batch file backs up the whole dhpos file, wouldn't the only files that need to be backed up is the accual files that are created by DHPOS. Just asking.

User avatar
brucef2112
Forum Regular
Posts:336
Joined:Mon Mar 06, 2006 11:19 pm
Location:Broward County, Floriduhh
Contact:

Re: Dated Full & Incremental BACKUP In a Batch File Examples

Post by brucef2112 » Tue Apr 19, 2011 3:33 am

I suppose if someone never upgrade to a newer version they could just backup data files. Or if they had a single register and kept perfect track of the few times they did upgrade software they could do a full backup at the time of upgrade and just continue using the incremental backups of data after. In a simple case as these, the backup program as is may be overkill. It could be modified to do just the specific POS data. Of course this leave one to modify the bat file if a new data file becomes part of a future upgrade. As it is, Dale can add whatever to the POS, and without any action on my part my backup will capture the 'new stuff' as long as its in the POS directory or one of its sub-directories. Aside from the way the batch file builds a folder name with the current date to store the backup in, you could do the above by just using XCOPY C:\POS\*.* E:\MyBackupLocation\ /S /M /K /V and call it a day! Note that the XCOPY's /M switch just copies files that have changed. As long as no other backup software is running that would affect the Archive flag of files, the first time this command line was run it would copy everything in the POS. (they all would have a +A archive flag) After this, subsequent runs of the exact same command line (with a different destination name so the previous one doesn't get overwritten) would copy only the data files because they would typically be the only files that got modified or created by the POS after the first run.

Remember that I'm multi store with multi computer and 'tech support' for a friend of mine who uses this POS. So consistency at each location makes a recovery process easier. With software upgrades ongoing, and the possibility of it not happening on the same day, there is still a need to backup both data and application files. If you have just data files saved and not the correct version of POS for those files you may find yourself in a mess restoring data to a version of the application that may not doesn't match.

In my batch file, only on the first of the month does it backup the entire POS directory. Each day after the first an incremental backup is performed. So it only backs up changed files and newly created files. Typically each day would be just the POS data files. However this would pickup other files (exported data , xls, inventory, receiving, P.0., doc, etc) as they were created and also application files that would occasionally change, say as the result of an upgrade to the software.

Keep in mind that data disaster is not always an in your face kind of event, such as hard drive failure or a slip of the finger that
types DEL C:\POS\*.* [ENTER]! These are easy. Fix problem area. Restore yesterday's backup and rejoice while doing a victory jig in the fact that you had a backup to restore from!

From my own experience, data disaster can creep in and not get notice until it builds for a while. Something like a new software version may introduce corrupt data that could go unnoticed for some time. Or someone plugs the vacuum cleaner in and turns it on just at the moment your POS is writing to disk the end of day sales. But you don't know its corrupt til the end of the month when you try and run an end of month report with that days data included and it spews garbage.

I suppose this goes to a discussion more about the philosophy of backup methods.
For myself, I feel warm and fuzzy with the philosophy of starting each month with a full backup of the POS and any related files. And then follow this with incremental backups (data, application, and what ever) the rest of the month. This way when data loss or corruption is noticed I can go back and identify when it was last good. Then Restore my last known good Full backup just before that point and then restore each incremental up to the date I need. So if I need to restore to Feb 7th I'd get the Feb 1st full backup and restore it, and then restore each of the 6 consecutive incremental backups after it. Without a lot of remembering on my part of what went on back then, I know that my data is with the correct software version along with any other files I may have created up to Feb 7th.

At the end of the year do I have a bunch of redundant files stored? Yes I do. However this has to do with consistency of process to ensure changes are captured. And when needed the system can be restored quickly. When disaster strikes there is peace of mind that I can get back to the first of any month with a single restore and move on from there.

[bruce goes to get some milk and cookies]
I feel warm and fuzzy,
Later,
Later,
Bruce

They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."
Benjamin Franklin - Historical Review of Pennsylvania, 1759

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests