ipsure logo
Logo and Language
Login icon Language selection icon
Hello, guest
*NIX BACKUP Active category menu left background Active category menu right background Hands-On blog header image Right block of Hands-On blog header image Final menu block of Hands-On blog header image
MS TIP PKI PROJECTS WORDPRESS Türkçe HANDS-ON SERVICES IT BUSINESS CONTACT ABOUT REFERENCES TERMS RSS
Home page Hands-On Services IT Business Contact About References Terms of Use RSS

06/07/2010

Incremental Backup with cwRsync by Using –backup-dir Option inside a Windows Batch File

Filed under: backup, down-to-earth — Tags: , , , , , — Sezgin Bayrak @ 23:30

Rsync is a powerfull utility for UNIX systems which synchronizes files and folders between local and remote locations using a smart delta transfer algorithm when appropriate. It’s fast, it’s free and it has tons of handy features such as compression, recursion, bandwith limitation and more which you can benefit even in your daily life certainly if you know what to do with it and how. In order to be more familiar with the rsync advantages that can not be found in most similar applications or protocols, you may browse the documenation pages.

While we get used to draw upon it for infrastructure wide syncronization and backup operations on mostly *NIX type systems (as we have mentioned here before), today we’re going to make use of it on our Windows PC to constitute a solid local incremental backup model to preserve our time and labour over against the possible computer disasters.

Basically what we’re going to do is to synch our important folder(s) to another location by a simple batch file and schedule it to take place periodically in order to transfer only the modified items from time to time as we keep on working in these folders. These items that are changed at source and also the extraneous data will be processed identically at the destination but their earlier copies will be moved and sorted into a seperate directory just in case we may need them later. Stale copies will permanently be removed from our system in accordance with the time period we designate inside the batch file.

Let’s settle down to work by downloading the latest version of cwRsync – Rsync for Windows platform (packaging of Rsync and Cygwin which is a Linux-like environment for Windows). Visit http://www.itefix.no/cwrsync/ and navigate down to cwRsync (Installer/Tools) component. Follow “Download source” link and download cwRsync_4.0.4_Installer.zip. We’re going to install cwRsync_4.0.4 (client package) by accepting the default installation location which is C:Program Files. You may certainly change the installation location but don’t forget to revise CWRSYNCHOME variable accordingly inside our co-star backup.bat

Before proceeding to this script, let’s see how the resulting work looks like when we run it;

cwRsync incremental backup directory structure

Create CWRsync folder under your destination backup device (e.g. a USB Hard Disk mounted as “G”) and set the folder permissions appropriately for the user account that will run backup.bat script bearing in mind that rsync needs additional permissions such as creating folders, changing permissions and writing attributes. So assigning administrative privileges to that user or running the script as an administrator shall likely solve the permission problems that may be rarely seen.

Here comes the simple batch script file which you may start to use by only editing the couple of variables in the highlighted section according to your own system, certainly if you don’t need a further customization;

@ECHO OFF
REM ***********************************************************************************
REM BACKUP.BAT
REM http://www.ipsure.com (2010)
REM ***********************************************************************************
SETLOCAL

REM ********************************** EDIT THESE *************************************
REM Source drive letter where the folders you want to backup reside
SET SD=F

REM Name of source folders you want to backup
SET SFOLDER1=My Projects
SET SFOLDER2=Personal Backups

REM Destination drive letter where your backups will reside (e.g. a USB Hard Disk mounted as "G")
SET BD=G

REM Main backup folder whereinto your backups will be transferred
SET BFOLDER=CWRsync

REM Number of days you want to keep the stale copy of the items which were modified/deleted at source
SET DAY=90
REM ***** YOU MAY LEAVE THE REST INTACT IF YOU DON'T PLAN A FURTHER CUSTOMIZATION *****

REM Location of rsync and related files (e.g. C:\Program Files\cwRsync or C:\cwRsync etc.)
SET CWRSYNCHOME=%PROGRAMFILES%\cwRsync
SET CYGWIN=nontsec

REM Make cwRsync home as a part of system PATH to find required DLLs
SET CWOLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\Bin;%PATH%

SET hour=%time:~0,2%
IF "%hour:~0,1%"==" " SET hour=0%time:~1,1%
SET BDATE=%date:~-4,4%%date:~-7,2%%date:~-10,2%
SET BTIME=hour_%hour%_%time:~3,2%

SET OPS=-ravb --itemize-changes --delete
SET BROOT=/cygdrive/%BD%/%BFOLDER%
SET BDIR=CHANGED

REM rsync process
rsync %OPS% --backup-dir=%BROOT%/%BDIR%/%BDATE%/%BTIME%/%SD%/"%SFOLDER1%" --exclude=%BROOT%/%BDIR% /cygdrive/%SD%/"%SFOLDER1%"/ %BROOT%/%SD%/"%SFOLDER1%"/
rsync %OPS% --backup-dir=%BROOT%/%BDIR%/%BDATE%/%BTIME%/%SD%/"%SFOLDER2%" --exclude=%BROOT%/%BDIR% /cygdrive/%SD%/"%SFOLDER2%"/ %BROOT%/%SD%/"%SFOLDER2%"/

REM deletion process
cd /D %BD%:\%BFOLDER%\%BDIR%
FOR /F "skip=%DAY% delims=" %%a IN ('DIR /B /AD 20?????? ^| SORT /R') DO RD /S /Q "%%a"

Don’t forget to modify line 27 as

SET CWRSYNCHOME=C:\"Program Files (x86)"\cwRsync

if you have 64-bit OS.

exclamation markBefore running the script, I strongly advise you to insert an “echo” between DO and RD at line 49 in order to test-drive the batch and to prevent blowing up your files by mistake before the actual schedules;

FOR /F "skip=%DAY% delims=" %%a IN ('DIR /B /AD 20?????? ^| SORT /R') DO echo RD /S /Q "%%a"

If the total size of the folder(s) you decide to backup are very large, it is obviously a good idea to first copy these data manually into their places in order to save time other than leaving this initial move to rsync batch. After you feel confident about the way your backups are handled and stored by the batch, you shall create a new job within Task Scheduler and schedule it to run periodically with the highest privileges.

Related Posts with Thumbnails
Subscribe to our RSS feeds Email Subscription via FeedBurner RSS Subscription via FeedBurner

  No related posts.

1 Comment »

Trackbacks

There has not been any trackback links yet.

Reader Comments

Ben30/03/2011 15:11

I know this is an old article and it’s one I stumbled upon while trying to find an affordable, reliable backup solution for both an old Windows 2000 Domain Controller and a much newer Windows 2008 R2 server – both backing up to Linux based NASes.

I just wanted to say THANK YOU for a very informative read, and an exceptionally useful batch file. It is the perfect combination of simplicity to setup and run with complexity to deter any tampering from IT privileged staff.

Thanks again – and keep up the great work!

RSS feed for comments RSS feed for comments on this post. TrackBack URL

Leave a comment