Mundy

IT Knowledge Base

User Tools

Site Tools


script_to_copy_backups_from_windows_to_usb

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
script_to_copy_backups_from_windows_to_usb [2017/01/23 11:22]
Dan Mundy created
script_to_copy_backups_from_windows_to_usb [2018/04/09 09:56] (current)
Line 1: Line 1:
 ====== Script to copy backups from Windows to USB ====== ====== Script to copy backups from Windows to USB ======
  
 +This could be used on a NAS running Windows, ie the NAS is a staging area for the Windows image backups, and then this script will copy them to a USB disk that is changed daily.
 +
 +In this example, ​
 +<code winbatch>​
 +:: Configure Variables
 +SET CUSTNAME="​Customer Name"
 +SET BACKUPDRIVE=F:​\
 +SET ERR_TO=support@mundy.co
 +SET ALL_TO=dan@mundy.co
 +SET FROM=nas@customer.com
 +SET FAILSUBJECT="​Error syncing backups to USB Drive"
 +SET SUCCESSSUBJECT="​Sync to USB Drive Log"
 +SET SMTPSERVER=192.168.0.1
 +SET AGETOKEEP=8
 +
 +::SET BELOW TO '​NO'​ TO DISABLE SUCCESS EMAILS
 +SET TEST=YES
 +
 +
 +::​****************************************************
 +::DONT EDIT BELOW THIS LINE
 +::​****************************************************
 +SET LOGDATE=%DATE:​~10,​4%%DATE:​~7,​2%%DATE:​~4,​2%
 +SET LOGTIME=%TIME:​~0,​2%%TIME:​~3,​2%
 +SET LOGFILE="​D:​\USBSyncLogs\USBSyncLog-%LOGDATE%-%LOGTIME%.txt"​
 +
 +cd C:​\Scripts\SyncToUSB
 +
 +MD %LOGFILE%
 +RD /S /Q %LOGFILE%
 +
 +echo %CUSTNAME% USB Sync Log>​%LOGFILE%
 +
 +forfiles -p %BACKUPDRIVE% -s -d -%AGETOKEEP% -c "cmd /c del /f /q @PATH"
 +for /f "​delims="​ %%i in ('dir %BACKUPDRIVE% /ad /b /s ^| sort /R') do rd "​%%i"​ 1>NUL 2>&1
 +
 +robocopy D:\Backups %BACKUPDRIVE% /S /PURGE /NFL /NDL /COPY:D /MAXAGE:7 /XD "​System Volume Information"​ "​$Recycle.Bin"​ /TEE /​LOG+:​%LOGFILE% /R:2 /W:30
 +if %ERRORLEVEL% GTR 4 (
 + blat.exe %LOGFILE% -to %ERR_TO% -f %FROM% -subject %FAILSUBJECT% -server %SMTPSERVER%
 +)
 +If %TEST% EQU YES (
 + blat.exe %LOGFILE% -to %ALL_TO% -f %FROM% -subject %SUCCESSSUBJECT% -server %SMTPSERVER%
 +)
 +
 +copy %LOGFILE% %BACKUPDRIVE%USBSyncLogs\
 +</​code>​
script_to_copy_backups_from_windows_to_usb.txt · Last modified: 2018/04/09 09:56 (external edit)