Tag: Batch files

  • Souping up SyncToy

    I used to back up my work PC to a set of Virtual Hard Disk (.VHD) files until one day I needed to recover from a failure, and I found that the hard drive encryption software we use prevented me from running a restore. That forced me to find another solution and one of my ReadyNAS devices (sadly not the one that recently suffered two disk failures on the same RAID 1 volume, taking with it a big chunk of my data) is now dedicated to backing up my work PC, with a regular file copy taking place.

    I have a drive mapped to a share on the NAS and the command line version of Microsoft’s SyncToy tool (synctoycmd.exe) is set to run as a scheduled task every evening at 10pm. Then, at 11pm, the NAS powers down until 8am the next day. The idea is that, as long as my PC is connected to my home network, it backs up all of the important files, at a time by which I should have stopped working.

    Unfortunately I’m not convinced that it’s working as it should be – just because the Windows 7 Task Scheduler tells me that the task completed doesn’t mean that SyncToy ran successfully (incidentally, if you are having problems with SyncToy on Windows 7, this thread might help).  I was googling for a solution and came across eXDee’s batch files (sometimes the old ways are the best) to check for network connectivity, presence of the appropriate volume and then run synctoycmd.exe, recording a log file on the way. Bingo.

    So, here are my versions (only minor updates from eXDee’s originals), called each night from Task Scheduler and a simple check of the lastsync.log file should tell me whether the backup worked or not.

    Incidentally, don’t be fooled (as I was) by the synctoycmd.exe output that says it saved time by not copying any files. That’s the output from the preview run and there is a long period after this during which there are no status updates whilst the actual file copies take place.

    synctoy.bat

    This is the control file, to be called from Task Scheduler or run manually from the command line:
    @echo off
    title SyncToy run in progress…
    echo Attempting file sync. Please wait…
    sync.bat >lastsync.log

    sync.bat

    This is the file that checks for the presence of my NAS and for a mapped drive before it backs up my data. You’ll need to subsititue your own IP address but I’m particularly impressed by eXDee’s code to look for a TTL rather than a ping success/failure (smart move). Note I haven’t mapped a drive if the connection is not there, although that is a possible enhancement:
    @echo off
    echo SyncToy Log starting at
    time /T
    date /T
    echo ##############################################
    echo Checking connection to NAS…
    echo ##############################################
    PING -n 2 -w 10 192.168.1.14 |find “TTL=” && goto NAS
    goto PINGFAIL

    :NAS
    echo ##############################################
    echo NAS is online. Checking for share…
    if exist “F:\Synced with Company PC\” goto SYNC
    goto NASFAIL

    :SYNC
    echo ##############################################
    echo Drive is mapped. Begin syncing files…
    echo ##############################################
    cd “C:\Program Files\SyncToy 2.1\”
    SyncToyCmd.exe -R
    if %ERRORLEVEL% == 0 goto SUCCESS
    goto SYNCFAIL

    :PINGFAIL
    echo ##############################################
    echo NAS not found. Exiting
    goto END

    :NASFAIL
    echo ##############################################
    echo Share not found. Exiting
    goto END

    :SUCCESS
    echo ##############################################
    echo Synctoy finished successfully. Exiting
    goto END

    :SYNCFAIL
    echo ##############################################
    echo Synctoy Failed. Exiting
    goto END

    :END
    echo ##############################################
    echo Synctoy Log ending at
    time /T
    date /T

    lastsync.log

    An example of a run (the failures were down to file access, rather than any issue with the scripts):

    SyncToy Log starting at
    21:00
    08/11/2011
    ##############################################
    Checking connection to NAS…
    ##############################################
    Reply from 192.168.1.14: bytes=32 time=3ms TTL=64
    Reply from 192.168.1.14: bytes=32 time=39ms TTL=64
    ##############################################
    NAS is online. Checking for share…
    ##############################################
    Drive is mapped. Begin syncing files…
    ##############################################
    Preview of Work Folder Backup (C:\Users\markw\Documents\Work\, F:\Synced with company PC\Work\) in time 00:03:08:253.
    SyncToy action was ‘Echo’
    Found 2 actions to perform.
    Found 47,158 files that did not require action.
    Analyzed 250.5 files per second.
    Avoided copying 135,013,767,205 bytes in 47,158 files.
    Saved approximately 03:00:27:00 by not copying any files.

    SyncToy run of Work Folder Backup (C:\Users\markw\Documents\Work\, F:\Synced with company PC\Work\) completed at 08/11/2011 21:03:27.
    SyncToy action was ‘Echo’.
    SyncToy options were:
    Active for run all
    All files included
    No files excluded
    Do not check file contents
    Include read-only files
    Include hidden files
    Include system files
    Backup older files (send to Recycle Bin)
    All subfolders included
    SyncToy run took 00:00:00:610.
    Copied 5,932,607,488 bytes in 2 files in 00:00:00:610.
    Bytes per second 9,725,586,045.9, files per second 3.3.
    Avoided copying 135,013,767,205 bytes in 47,158 files that did not require action.
    Saved approximately 00:00:13:882 by not copying all files.
    Warning: 4 failures occured.
    You can retry by selecting “Run” again or select “Preview” to see
    the operations that remain to be performed.

    The Sync operation completed successfully on folder pair ‘Work Folder Backup’ but some files were skipped. Please look at the logs for more details.
    ##############################################
    Synctoy Failed. Exiting
    ##############################################
    Synctoy Log ending at
    21:03
    08/11/2011