NETGEAR is aware of a growing number of phone and online scams. To learn how to stay safe click here.
Forum Discussion
Sandshark
Dec 30, 2018Sensei - Experienced User
A Windows backup script you can use -- a gift from me to you
I have created a batch file that you can run interactively or call from the Windows Task Scheduler to backup your Windows Users folder (or any other, if that's your choice). It uses the free programs WOLCmd and Putty to turn on and off the NAS, but it leaves the NAS on if it found it on. See the initial REMarks for where to get those programs and other assumptions and details regarding the set-up and running of the file. Before you set it up as an automated task, I recommend you run it interactively to make sure everything works and to make the first (typically long) backup. You may want to test it on a directory smaller than your Users folder.
I have included/excluded files that are appropriate for most users' home folders. But if you are knowlegeable in the use of the RoboCopy command, you can change that. You can even add additional folders if you want.
This should work with all OS6 ReadyNASes. It should also work with others, though I don't know if the power off works on those. (Let us all know if you try it.)
Since the user and admin passwords are included in plain text, you may want to encrypt the file. See the opening REMarks for where to get a free BAT to EXE compiler that will accomplish that.
There is some error checking. I believe I have properly accounted for all cases of the directory names containing spaces, but perhaps not all potential special characters (that you really should be avoiding, anyway). BULog.txt, overwritten each time the backup runs, is created in the same directory the batch file is in and can be used to check that it's working or to help in any troubleshooting.
WARNING: Use a folder on your NAS as the target that is not used for anything else. This file uses the /PURGE option in Robocopy to delete files and folders in the NAS backup folder that no longer exist on the Windows source. If you have files or folders other than those from the backup in the destination NAS folder, THEY WILL BE DELETED. So, use care in chosing the backup share and folder or remove this argument. Don't come back and say my program deleted all your files, YOU HAVE BEEN WARNED.
Copy the code below into a text editor (like Notepad, not WordPad or Word) and save it with a ".bat" extension. Change the parameters at the beginning to whatever is appropriate to your NAS and Windows installations. Be mindful of quotes and spaces -- don't add or delete any, as they are are there for a reason.
If you do not understand how to add this as a scheduled task in the task scheduler, then Google it or just continue to run it manually.
EDIT: Patches in the posts below are applied to this post also (including the formatting in the patches) - StephenB
@echo off echo Windows to ReadyNAS backup batch file. 12/30/2018 by Sandshark on Netgear forum. REM Free for any use or modification. Provided as-is without warrantee. Use at your own risk. REM Turns your NAS on if it is off. REM Leaves NAS on if it was on when the batch file started and turns it off if it was off. REM Increase NASWait (in seconds) if your NAS takes more than 5 minutes to boot. REM System, Hidden, and Offline files and those locked as in use will not be backed up. REM Assumes Wake On LAN enabled on NAS. REM Assumes SSH enabled on NAS. REM Assumes Putty installed in default directory of C:\Program Files (x86)\Putty. (see https://www.putty.org/) REM Assumes WOLCmd is in the same directory as batch file. (see https://www.depicus.com/wake-on-lan/wake-on-lan-cmd) REM Change variables at start as appropriate for your NAS and Windows installation. Be mindful of quotes and spaces. REM Map to a drive letter that you do not normally use for anything else. REM WARNING: Use a unique directory (folder) for the destination on the NAS or risk losing files you did not intend. REM Remove "/PURGE" from ROBOCOPY command to keep files deleted on source. REM Change other ROBOCOPY arguments as needed to include/exclude files or directories. REM Nothing in this will keep your NAS from powering down manually or by schedule during the copy. REM A log file "BULog.txt is created in the same directory as the batch file to verify it's working or assist in troubleshooting.
REM Because of the log file, the user executing the backup must have write access to the folder contaiing the batch file. REM To encrypt so NAS admin password is not visible, use Advanced BAT to EXE converter. (https://www.battoexeconverter.com/) REM To run minimized use CMD /C START /MIN <filename.bat or filename.exe>. SETLOCAL REM Make changes here for your NAS and Windows configuration. ADD NO SPACES around equal signs. REM set NAS IP Address (or name): SET NASIP=192.168.0.xxx REM set NAS MAC Address: SET NASMAC=123456789AB REM Set NAS Admin Password: SET NASAdminPW=password REM Set NAS User name and passsword (can be admin, if desired): SET NASUser=User SET NASUserPW=password REM Set wait time in seconds for NAS to power on: SET NASWait=300 REM Set Directory to be backed up. Insure it is the FULL path (e.g. starts with C:\). SET WinDir=C:\Users\User REM Set Windows Mapped drive Set MapDrive=Z: REM Set share and directory on NAS to back up to. NOTE: Share and directory must already exist. REM WARNING! If this directory is used by other processes, files you don't want erased may be lost forever! SET NASDir=User\WinBackup REM Don't change anything below this unless you are sure you know what you are doing. Echo Backup started: >"%~dp0BULog.txt" DATE /T >>"%~dp0BULog.txt" TIME /T >>"%~dp0BULog.txt" IF NOT EXIST "%WinDir%" ( ECHO Windows backup path "%WinDir%" not found. >>"%~dp0BULog.txt" GOTO Done ) Echo . >"%~dp0BULog.txt" Echo Pinging NAS: >>"%~dp0BULog.txt" PING -n 1 %NASIP% >>"%~dp0BULog.txt" PING -n 1 %NASIP% | find "TTL=" >nul IF errorlevel 1 ( SET NAS=WasOff "%~dp0WOLCmd" %NASMAC% 255.255.255.255 255.255.255.255 4343 >>"%~dp0BULog.txt" TIMEOUT /T %NASWait% PING -n 1 %NASIP% | find "TTL=" >nul IF errorlevel 1 ( GOTO Done ) ELSE ( SET NAS=PoweredOn ) ) ELSE ( set NAS=WasOn ) ECHO . >>"%~dp0BULog.txt" ECHO NAS Status: %NAS% >>"%~dp0BULog.txt" ECHO . >>"%~dp0BULog.txt" NET USE %MapDrive% /DELETE /Y >>"%~dp0BULog.txt" ECHO Mapping "\\%NASIP%\%NASDir%" to %MapDrive% >>"%~dp0BULog.txt" NET USE %MapDrive% "\\%NASIP%\%NASDir%" /PERSISTENT:NO /USER:%NASUser% %NASUserPW% >>"%~dp0BULog.txt" 2>>&1 IF Errorlevel 1 GOTO Done
ROBOCOPY "%WinDir%" %MapDrive% /S /R:2 /W:10 /XO /XA:SHO /XJD /Z /MIN:1 /FFT /PURGE /NP /XD AppData .* /XF *.lnk /LOG+:"%~dp0BULog.txt" ECHO Removing mapped drive: >>"%~dp0BULog.txt" NET USE %MapDrive% /DELETE >>"%~dp0BULog.txt" :Done If %NAS%==PoweredOn ( TIMEOUT /T 60 "C:\Program Files (x86)\Putty\plink.exe" -ssh root@%NASIP% -pw %NASAdminPW% systemctl poweroff ) Echo Backup ended >>"%~dp0BULog.txt" DATE /T >>"%~dp0BULog.txt" TIME /T >>"%~dp0BULog.txt" ENDLOCAL EXIT
Merry belated Christmas.
6 Replies
- Retired_Member
Thanks a lot, dear Santa, uh Sandshark and happy new year :-)
- StephenBGuru - Experienced User
Thx for posting.
The forum software turned two text strings into emoticons, which of course won't work. I took the liberty of bolding the two :s that triggered this, so users can cut/paste the bat file into notepad with no problems.
- SandsharkSensei - Experienced User
Thanks. Yeah, that would have thrown some folks off.
Wow, it does that when you put it in a CODE box? Just one more thing that makes this really bad software. And it doesn't show you them when you edit, either -- so make that two more things.
There is one more thing I decided I should add to help in troubleshooting. While I said to use a drive mapping the you don't use for anything else, it turns out that having a window open to the target NAS folder, even if not mapped, causes an error I can't avoid. But changing line 74 as follows (adding the 2>>&1) will at least put the relevant information in the log:
NET USE %MapDrive% "\\%NASIP%\%NASDir%" /PERSISTENT:NO /USER:%NASUser% %NASUserPW% >>"%~dp0BULog.txt" 2>>&1
Thanks, too, for the info that bolding can work around the problem. I'll have to look out for the conversion and use that if there is a next time.
Related Content
NETGEAR Academy

Boost your skills with the Netgear Academy - Get trained, certified and stay ahead with the latest Netgear technology!
Join Us!