Chat > Guides & Projects

Batch Script to Turn on, backup to and turn off second PC

(1/3) > >>

XEntity:
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 onIf not sends WOL packet to turn onThen 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 fileThen sends shutdown request, giving 25 seconds notice and option to cancel in case someone has started using itIf 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 PowertoysWOL.exe - Gammadyne Corporationpsshutdown.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: ---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

--- End code ---

Clock'd 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.

Quixoticish:

--- 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.
--- End quote ---


Post away.

XEntity:
No probs and ye post it up!

dogbert:
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:

Navigation

[0] Message Index

[#] Next page

Go to full version