Author Topic: Batch Script to Turn on, backup to and turn off second PC  (Read 10810 times)

As this script was lost when one of the hamsters went terminal, I have reposted....

The below batch script, put in to Scheduled Tasks to run daily does the following:

    Checks if remote PC is on
    If not sends WOL packet to turn on
    Then checks to see if it has powered up (If not assumes it is not connected to home network and exits)
    If it has then runs back up and creates log file
    Then sends shutdown request, giving 25 seconds notice and option to cancel in case someone has started using it
    If remote PC already on, it just runs the backup and logging and does nothing after.


Using the below free software:

    Sync Toy 2.1 - Microsoft Powertoys
    WOL.exe - Gammadyne Corporation
    psshutdown.exe - Microsoft Resource Kit


CHANGED: The new version of SyncToy now allows setting up the Sync job via a script, so I have added that change below, if the job already exists then it ignores it, so you don't need to check. Also check the SyncToy documentation for more info on different types of sync (I'm using "echo" below, which mirrors changes in one direction and is the safest option providing you only update the files in one location.)

Everything below with REM in front is a comment for those less familiar with batch files (also name the file backup.bat or similar)

Code: [Select]
echo off
cls

echo ===================================================
echo Checking Black Media Center Power Status...
echo ===================================================
echo.

REM Ping to see if it is on, if not then turn on using MAC address

ping -n 1 -w 5000 black > nul
if errorlevel 1 goto TURNON

goto ISON

:TURNON
cls
echo.
echo ===================================================
echo Black is not ON, trying to power up...
echo ===================================================
echo.
wol.exe 001FC620ABFA

REM Wait 120 seconds to allow Black to finish loading and settle workaround

ping 1.1.1.1 /n 1 /w 200000 > NUL

REM Is Black on? If not report failure (i.e. if laptop is not connected to home network)

ping -n 1 -w 5000 black > nul
if errorlevel 1 goto BIGFAIL
cls
echo.
echo ===================================================
echo Black is ON, Running Sync...
echo ===================================================
echo.

REM Put time and results in log file
echo Backup last run: > e:\MyDocs\lastbackuplog.txt
echo %date% %time% >> e:\MyDocs\lastbackuplog.txt
echo Result: >> e:\MyDocs\lastbackuplog.txt

"C:\Program Files\SyncToy 2.1\SyncToy.exe" -d(left=e:\MyDocs,right="\\black\Backup$\MyDocs",name="MyDocs",operation=echo)
"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "My Docs"
cls
echo.
echo ===================================================
echo Sync Complete, Powering Off...
echo ===================================================
echo.

REM Send shutdown command, give 25 seconds notice on the remote PC and option to cancel, incase someone has started using it.

psshutdown \\black -u Administrator -p passwordgoeshere -c -s -t 25 -m "Another user has requested that this machine shutsdown, press Cancel to stop."
echo.
goto END


:ISON
cls
echo.
echo ===================================================
echo Black is already ON, Running Sync...
echo ===================================================
echo.

REM Put time and results in log file
echo Backup last run: > e:\MyDocs\lastbackuplog.txt
echo %date% %time% >> e:\MyDocs\lastbackuplog.txt
echo Result: >> e:\MyDocs\lastbackuplog.txt

"C:\Program Files\SyncToy 2.1\SyncToy.exe" -d(left=e:\MyDocs,right="\\black\Backup$\MyDocs",name="MyDocs",operation=echo)
"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "My Docs"
cls
echo.
echo ===================================================
echo Sync Complete, Leaving Blacks power ON...
echo ===================================================
echo.
goto END


:BIGFAIL
cls
echo.
echo ===================================================
echo Could not find Black on the network...
echo ===================================================
echo.
goto END

:END

REM Insert pause here if you want the window to remain open with the result

Last Edit: January 16, 2012, 10:03:27 AM by XEntity #187;

    • Tekforums.net - It's new and improved!
  • Offline Clock'd 0Ne

  • Clockedtastic
  • Posts: 10,937
  • Administrator
  • Hero Member
Re:Batch Script to Turn on, backup to and turn off second PC
Reply #1 on: October 16, 2009, 01:09:33 AM
Im very happy to see you reposted this, many thanks :)

If anyone is interested I also have two decent little command line programs that will do FTP file uploads/downloads that would be useful for a backup application or Scheduled task remote backup.

  • Offline Quixoticish

  • Posts: 2,953
  • Hero Member
  • Slayer of ninjas, pirates and vikings.
Re:Batch Script to Turn on, backup to and turn off second PC
Reply #2 on: October 16, 2009, 10:15:29 AM
Quote from: Clockd 0Ne
Im very happy to see you reposted this, many thanks :)

If anyone is interested I also have two decent little command line programs that will do FTP file uploads/downloads that would be useful for a backup application or Scheduled task remote backup.


Post away.

Re:Batch Script to Turn on, backup to and turn off second PC
Reply #3 on: October 18, 2009, 13:12:37 PM
No probs and ye post it up!

Re:Batch Script to Turn on, backup to and turn off second PC
Reply #4 on: October 19, 2009, 17:05:28 PM
Thanks Xentity - We use synctoy for our remote users. Now I have a command line to push out over VPN and get all their data downloaded instead of rellying on windows sync or user actioned sync process! Looking forward to the FTP batch [hint] :cheers:

Re:Batch Script to Turn on, backup to and turn off second PC
Reply #5 on: October 19, 2009, 19:24:47 PM
No problem, would be interested in the FTP push too, ideally would like it to only push changed files, but I think thatd be a whole new piece of software!

    • Tekforums.net - It's new and improved!
  • Offline Clock'd 0Ne

  • Clockedtastic
  • Posts: 10,937
  • Administrator
  • Hero Member
Re:Batch Script to Turn on, backup to and turn off second PC
Reply #6 on: October 19, 2009, 21:29:26 PM
Quote from: Chris H
Quote from: Clockd 0Ne
Im very happy to see you reposted this, many thanks :)

If anyone is interested I also have two decent little command line programs that will do FTP file uploads/downloads that would be useful for a backup application or Scheduled task remote backup.


Post away.


Im trying to find the blasted things now! :lol:

Ill post them soon

Re: Batch Script to Turn on, backup to and turn off second PC
Reply #7 on: February 11, 2011, 19:00:05 PM
UPDATE:

Will post a revised version soon, that uses the new SyncToy 2.1 - Which means you don't have to configure the backup separately in the Sync Toy program, you can just add it to the script itself!

BUT first the psshutdown script doesn't appear to work with Win 7 Home Premium, and the inbuilt Win 7 remote shutdown command doesn't appear to work either, so does anyone use remote shutdown that would be batch script compatible?

As at the moment, I am relying on the power saving features of the second PC that puts it to sleep/hibernate after an hour. Would much prefer that it was instant!

Re: Batch Script to Turn on, backup to and turn off second PC
Reply #8 on: February 11, 2011, 20:11:41 PM
neat batch script XEntity, with a couple of modifications that could come in really handy on my 'rents network. Thanks!

Re: Batch Script to Turn on, backup to and turn off second PC
Reply #9 on: February 11, 2011, 21:55:16 PM
neat batch script XEntity, with a couple of modifications that could come in really handy on my 'rents network. Thanks!

Cheers, like I say the shutdown doesn't work on W7, and if you get the newer version of Sync toy then you don't need to do any setup.

The one up top isn't the latest version I'm using, just trying to sort my shutdown issue, once I have that done I'll post the updated version....

...Then to start work on my backup over internet scripts and hardware setup....

Re: Batch Script to Turn on, backup to and turn off second PC
Reply #10 on: February 11, 2011, 22:07:04 PM
actually thinking about it this would be so much easier to do on my parents system. They have a box which exists solely to back-up photos off Dads Lappy. It's all handled with startup scripts and cron jobs under Ubuntu Server, which means if I were to adjust the server side script to fire up, back up and shut down again I'd only need the first few lines of your script on the windows laptop to fire up the backup box, and I don't have to worry about how to tell a Linux box to shut down from a Windows client (I'm sure that can be done pretty easily, but if I don't have to do it it saves me figuring it out!)

Re: Batch Script to Turn on, backup to and turn off second PC
Reply #11 on: February 12, 2011, 01:28:06 AM
Nice, glad to help ;)

    • Tekforums.net - It's new and improved!
  • Offline Clock'd 0Ne

  • Clockedtastic
  • Posts: 10,937
  • Administrator
  • Hero Member
Re: Batch Script to Turn on, backup to and turn off second PC
Reply #12 on: January 11, 2012, 23:06:03 PM
Only a few years late on this one :lol:

I finally found those command line programs I was on about for uploading/downloading:

download.exe
upload.exe

upload is as simple as:
Code: [Select]
upload.exe C:\file-to-upload.zip ftp://10.0.0.1 username password
download is:
Code: [Select]
download.exe http://www.url.com/file.ext username password /output:C:\downloads\
Both tools have a number of very useful/powerful command line features.


Re: Batch Script to Turn on, backup to and turn off second PC
Reply #13 on: January 11, 2012, 23:14:41 PM
Cool cheers, just realised I didn't update this for the new sync toy, I'll add that when I'm not on the iPad, also will add some other useful bits like automatically connecting to the VPN if the local network isn't available, but will need to use IP then or update your hosts file.

It might just be worth a new thread for useful batch scripts..

Oh and just to add I'd you are only trying to sync a few gb of data then dropbox would actually be the easiest option, I'm however syncing about 150gb at the moment!
Last Edit: January 11, 2012, 23:24:56 PM by XEntity #187;

Re: Batch Script to Turn on, backup to and turn off second PC
Reply #14 on: January 12, 2012, 18:34:00 PM
Updated guide to include automatic creation of SyncToy job, and also logging date time and SyncToy results to log file..

> creates new log
>> Adds additional lines to that log

I'm in the process of writing a guide specifically on batch files so people can assemble their own scripts..

0 Members and 1 Guest are viewing this topic.