2

I'm abit stuck with this backup script I have been writing. The goal of the script is to:

  1. Wake-up a sleeping PC on my LAN

  2. Run Microsofts Synctoy(cmd version) to sync all of the paired folders that I have setup and output results to log file.

  3. If there is an error, it should write it to the log file and then send an email to me via the mailsend.exe.

The batch file is set to run every night with Windows 7's Scheduled tasks.

Contents of batch file:

    @ECHO OFF
    SET /a RETRY=0
    SET /a RETRIES=5
    SET MAC=000c76******
    SET IP=192.168.0.8
    SET SUBNET=255.255.255.0
    SET PORT=7

    ECHO %date% - %time% - Started sync.

    :CHECK
    PING -n 1 %IP% | find "bytes=">NUL
    IF %ERRORLEVEL%==0 (
    GOTO SYNC
    )
    IF %ERRORLEVEL%==1 (
    GOTO WAKE
    )

    :WAKE
    SET /a RETRY=%RETRY%+1
    IF %RETRY% GEQ 6 (
    SET ERR_VAL=RETRY
    GOTO ERROR
    )
    ECHO Waking up \\NAS Attempt %RETRY%\5...
    START C:\sync\wolcmd.exe %MAC% %IP% %SUBNET% %PORT%
    timeout /T 30 /NOBREAK>NUL
    GOTO CHECK

    :SYNC
    ECHO SyncToy is running...
    "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R>C:\sync\synctoy_log.txt
    IF %ERRORLEVEL% == 0 (
    ECHO %date% - %time% - Success: Sync completed.>>C:\sync\synctoy_error_log.txt
    GOTO END
    ) ELSE (
    SET ERR_LEV=%ERRORLEVEL%
    SET ERR_VAL=SYNC
    GOTO ERROR
    )

    :ERROR
    IF %ERR_VAL%==RETRY (
    ECHO Error: Failed to sync, retries exceeded.
    ECHO %date% - %time% - Error: Failed to sync, retries exceeded.>>C:\sync\synctoy_error_log.txt
    )
    IF %ERR_VAL%==SYNC (
    ECHO Error: SyncToy error (%ERR_LEV%).
    ECHO %date% - %time% - Error: SyncToy error (%ERR_LEV%).>>C:\sync\synctoy_error_log.txt
    )
    START C:\sync\mailsend.exe -to example.email@googlemail.com -from example.email@gmail.com -ssl -attach synctoy_error_log.txt,text/plain,i -smtp smtp.googlemail.com -port 465 -sub SyncToy_log +cc +bc -v -auth-login -user example.email@gmail.com -pass examplepass
    GOTO END

    :END
    EXIT

Contents of synctoy_error_log.txt

    18/02/2013 -  6:02:16.40 - Success: Sync completed.
    20/02/2013 -  6:05:25.71 - Success: Sync completed.
    21/02/2013 -  6:07:14.27 - Success: Sync completed.
    22/02/2013 -  6:02:56.34 - Success: Sync completed.
    24/02/2013 -  6:01:49.97 - Success: Sync completed.
    25/02/2013 -  6:01:35.14 - Success: Sync completed.

As you can see there has been a couple of days where the log was not written. The PC with the scheduled task running and the PC I want to wake up should have been accesible at this time.

Is there anything that I am doing wrong here in my error checking or something?

I don't get an email saying there was a problem either, but if I disconnect the sleeping PC from the LAN and force the script to start, I do get an email saying it couldn't wake it up.

Thanks for any advice you can give me it's greatly appriciated. I know this isn't the most efficient script, but I've been trying to pick up everything I can to get by.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rikki B
  • 636
  • 8
  • 23
  • 2
    It may be a permissions issue, that the scheduled task can't send the email. Have you tried writing a simpler batch file that just sends the email, and let it run as scheduled, to make sure that part is working? You could also try having the batch file write to its own log every time it runs, so you can at least confirm it is running on those days (and maybe even spit out a few debugging messages to the log so you can see how far it gets). Also look at your event log to see if the task scheduler reported any errors. – Nate Hekman Feb 25 '13 at 17:49
  • Thanks for the great advice, I will go ahead and try that. – Rikki B Feb 25 '13 at 18:33
  • I made a new simplified batch file with just the "mailsend.exe" line and it works correcty when activated via a scheduled task. I also added a new log file to capture the debug data from within the batch file itself. I just have to wait and see if it's being activated on the days the log's not written. – Rikki B Feb 26 '13 at 00:58
  • Also I checked the event log and there is no log that coincides with any of the problems. – Rikki B Feb 26 '13 at 01:31

1 Answers1

0

under what account does the script run as scheduled task? normally the nt account system is not able to use any network shares, try running under a user account which is allowed to log on as a batch task in secpol.

peet
  • 274
  • 3
  • 5
  • 18
  • It's running under an Administrator account. – Rikki B Mar 24 '13 at 04:57
  • 1
    click start type secpol.msc and start it - Select "Local Policies" in MSC snap in - Select "User Rights Assignment" - Right click on "Log on as batch job" and select Properties - Click "Add User or Group", and include the relevant user. – peet Mar 25 '13 at 05:03
  • @rikki-b you did not try even, there is only one item behind that Thing, you can map the drive in your script directly and to do so you would Need first to delete the net use and then remap it as the account of the script /Network ressources can't get mapped twice with the same account). – peet Nov 23 '13 at 20:10