× NETGEAR will be terminating ReadyCLOUD service by July 1st, 2023. For more details click here.
Orbi WiFi 7 RBE973
Reply

A Windows backup script you can use -- a gift from me to you

Sandshark
Sensei

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.

Message 1 of 7
Retired_Member
Not applicable

Re: A Windows backup script you can use -- a gift from me to you

Thanks a lot, dear Santa, uh @Sandshark and happy new year 🙂

Message 2 of 7
StephenB
Guru

Re: A Windows backup script you can use -- a gift from me to you

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.

Message 3 of 7
Sandshark
Sensei

Re: A Windows backup script you can use -- a gift from me to you

 

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.

Message 4 of 7
StephenB
Guru

Re: A Windows backup script you can use -- a gift from me to you


@Sandshark wrote:

 

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.

 


Yes, it is a pain, and it really shouldn't do that in a code box. Smiley Frustrated 

 

Overall, there's no need to render the common emoticon abbreviations as emoticons anywhere in the posts, since it's easy enough to use the menu to add emoticons if you want them - and the menus don't use those abbreviations, they use strings like  :smileyfrustrated: .  Bolding part of the string is the only workaround I've found, and bolding the : doesn't stand out.  

 

Message 5 of 7
Sandshark
Sensei

Re: A Windows backup script you can use -- a gift from me to you

OK, a couple more small changes.  If you follow the instructions and never use the mapped drive letter for anything else, all will be well without this change.  But I got to testing some "I didn't fully understand the instructions" scenarios and found that a previously mapped drive with the same letter and a folder or file in use will cause it to hang waiting for a response to force closure .  And if the user gave the wrong response, it could run (and end up deleting files) in that "wrong drive".  Even worse, if you happen to have a real drive with that letter, the same thing could happen on that drive instead of the mapped one, and it requires no user response.  I also added error logging for these scenarios.

 

So, replace lines 72-75 with this:

 

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

Specific changes are shown in red.  I don't know how it could ever get to the second NET USE and still have another mapped drive to the same letter exist.  But in case there is a scenario I haven't though of that can make that occur, this also fixes that.

 

If you prefer the "belt and suspenders" approach, leave in the old line 75 (IF NOT EXIST...) after the new one above.

 

I borrowed a lot of this from a script I wrote for work some time ago that runs on a user account that's never actively logged in and the computer is locked up where USB drives can never be inserted, so these scenarios could never exist there.  Hopefully, I've now got all bases covered for more common users.

 

@StephenB, maybe you can put this into the original?  This change includes the last one.

 

BTW, for those who thought the batch file "language" was dead, this certainly shows otherwise, though a  lot of the "magic" is done by RoboCopy (which can only be run from the command line and is well suited for use in batch files).

Message 6 of 7
StephenB
Guru

Re: A Windows backup script you can use -- a gift from me to you


@Sandshark wrote:

 

@StephenB, maybe you can put this into the original?  This change includes the last one.

 


Done. Note I preserved the color coding, etc that is in the patch.  Cutting/pasting to notepad will remove the formatting.

 


@Sandshark wrote:

 

BTW, for those who thought the batch file "language" was dead, this certainly shows otherwise, though a  lot of the "magic" is done by RoboCopy (which can only be run from the command line and is well suited for use in batch files).


I still have batch files I use routinely, you can do quite a lot with them.  Powershell has more capabilities, but I haven't invested much time in learning it.

Message 7 of 7
Top Contributors
Discussion stats
  • 6 replies
  • 1996 views
  • 2 kudos
  • 3 in conversation
Announcements