Backup via Batch with no 3rd party software

Here was my challenge. I need a temporary backup solution that does not use any 3rd party tools and will give a backup of the past X days (14 in my case). This solution must not be a resource hog.

My backup solution is to copy files from one server to another (or a NAS device) so that these files can be readily copied or restored from the backup location.

My scenario:
1 Server acting as a backup location. Large drives are installed on this server and NTFS file Compression is turned on. A share was created for each server that was going to be backed up using this method. By using Task scheduler, this batch file runs nightly, maps a path to the backup server, creates a folder using the date and time for the folder name, copies the files from the source machine and cleans up any folders older that X number of days.

There were a lot of challenges in designing this solution the first being creating a folder based on the date and time. Anything before 10:00am would create a space in the name causing 2 folders to be created. The second was deleting folders that were X number of days old. Thanks to the FORFILES command , this was an easy fix.

This solution requires 2 files. Backup.bat and exclude.txt. Below is the code for both. xcopy is used in this code and references the exclude.txt file to ensure that system files and other undesireables are not copied.

=====================================================
BACKUP.BAT
=====================================================

:: 
:: Batch file to copy files to a backup location, set the backupdate on the folder, 
:: compress files and folders and delete any folders over xx days old.
:: Written by Frank J. McCourry - Holland Computers, Inc. 
:: I could have used 3rd party software, but where's the fun in that?

:: Enable Command line extensions
setlocal enableextensions

::SET VARIABLES
:: Set lauchpath to the directory where you copied the backup.bat and exclude.txt files.  Dont forget the trailing slash.
SET launchpath=C:\Scripts\

::Set Destination drive letter and path
SET DESTDRIVE=h:
SET DESTURL=\\SERVERNAME\SHARENAME /USER:DOMAIN\USERNAME PASSWORD
SET DESTPATH=\
::Set SOURCE drive and path
SET SOURCEDRIVE=f:
SET SOURCEPATH=\

:: Set Number of Days to Retain Backups
SET RetentionDays=10
::
:: THERE IS NO NEED TO CHANGE ANYTHING BELOW -- UNLESS YOU KNOW WHAT YOU ARE DOING AND YOU REALLY FEL THE NEED.
::

::USE TIME AND DATE TO SET FOLDER NAME TO BE USE AS DESTINATION 
::Fix leading 0 in time if the time is before 10:00 or after 12:00
set hh=%time:~0,2%
if "%time:~0,1%"==" " set hh=0%hh:~1,1%
set time0=%hh%%TIME:~3,2%%TIME:~6,2%
:: Set the Variable dirname as date and time, formatting in a way that allows it to be used as a folder name
set dirname="%date:~-4,4%-%date:~-10,2%-%date:~7,2%-%time0:~0,2%.%time0:~2,2%.%time0:~4,2%"

:: Remove then Map the drive that we will use to send our files to
net use %DESTDRIVE% /D /yes
net use %DESTDRIVE% %DESTURL%

::Check that mapping is correct.  If not fail and end script. This prevent deletion of users files on their C: drive should the drive mapping fail.
If not exist %DESTDRIVE%%DESTPATH% GOTO END

::Change to the newly mapped drive and ensure you are at the root level
%DESTDRIVE%
cd \

:: Remove files over x days old, the /d -xx indicates the number of days.  
::This command only works on Windows Vista or Windows 2003 Server and above.
forfiles /d -%RetentionDays% /c "cmd /c rd @path /s /q"

:: Remove folders over x days old, the /d -xx indicates the number of days.  
::This command only works on Windows Vista or Windows 2003 Server and above.
forfiles /d -%RetentionDays% /c "cmd /c rd @path /s /q"

:: Using the dirname variable, create a folder to place our backups
mkdir "%dirname%"

::copy our files to the new folder using our old friend xcopy.  exclude.txt must reside in the same direstory the script is launched from.
xcopy "%SOURCEDRIVE%%SOURCEPATH%" "%DESTDRIVE%%DESTPATH%%dirname%" /H /E /C /K /Y /F /EXCLUDE:%launchpath%exclude.txt
::Windows 2008 Server or windows Vista and above may use robocopy.  xcopy works with everything (so far).
::Robocopy Command Below.  If you want to use it instead, comment out the xcopy command above and uncomment the command below.
::robocopy "%SOURCEDRIVE%%SOURCEPATH%" "%DESTDRIVE%%DESTPATH%%dirname%" /B /MIR /XA:S

::Sometimes the folder is created as hidden, reset the attributes so we can find the folder later (Still gotta figure this out)
attrib %dirname% -h -s

::Compress Files - This is not necessary if NTFS compression is enabled on the destination drive.  To use it remove the comment colons (::)
:: This is for NTFS file compression only this is not a zip program.  If you want to zip your files you will need to use a 3rd party program. 
::compact /c /s:%DESTDRIVE%%DESTPATH%%dirname%

::Clean up by removing the drive letter we just mapped. Variables will be reset once the batch ends.
net use %DESTDRIVE% /D /yes

:END

=========================================================
Exclude.txt – All exclusions should be on a line by themselves
=========================================================

pagefile.sys
\System Volume Information\
\Recycler\
\TEMP\
*.tmp
desktop.ini

 

=======================================================
Some discoveries made during this process:
1. There are hardly any system resources used with this method.
2. Backup of about 20GB took approximately 1 hour to complete. Not impressive, but it compares with a lot of 3rd party backup software, and since it uses fewer resources, there is no problem running this during production hours.
3. Locked files are not a problem since xcopy and robocopy handle them very well. (I have heard of some occasions where locked files are a problem, but most are hardware related.)
4. NTFS compression took my ~20GB backup and reduced it to under 8GB, a 40% compression ratio. This case was a healthy mix of files that could be highly compressed, such as text and html files as well as files that do not compress well such as JPG images and ZIP files (because they are already compressed).
5. I know it’s 80’s technology, but it’s cheap, fun and just works!

Comment away!

2 Replies to “Backup via Batch with no 3rd party software”

  1. Pingback: Using ShadowSpawn and Robocopy to copy in use files over a low bandwidth connection. | Xpert Notes

  2. Modified the code to allow user to set retention time with a variable and added a line to delete files older than the RetentionDays variable because the rd command does not work properly if there are files in the folder being removed.