Image

Image

Search This Blog

Saturday, February 03, 2024

backup cmd

:: *** SYNC DATA - Copy one way v3.6 ***
@echo off
title BACKUP in progress
SETLOCAL ENABLEDELAYEDEXPANSION
if not defined is_min set is_min=1 && start "" /min "%~dpnx0" %* && goto end
echo.
if not "%~1" == "" (set source=%~1)
if not "%~2" == "" (set destination=%~2)
if not "%~3" == "" (set rcptto=%~3) else (echo USAGE: %~0 "source" "destination" "mail@dom.tld; mail2@dom.tld" "mail.server(optional)" && goto end)
if not "%~4" == "" (set smtpsrv=%~4) else (set smtpsrv="smtp.dom.tld")
set mailfrom="%COMPUTERNAME%@%USERDNSDOMAIN%"
set emailer=%temp%\email_%random%.vbs
set logfile=%temp%\copy_report_%random%.log

:: *** Copy ***
echo > %logfile% %date% %time% *** STARTING COPY ***

robocopy %source% %destination% /E /FP /TS /XO /FFT /COPY:D /R:3 /W:5 /IPG:25 /X /V /NP /LOG:%logfile%
set erlvl=%ERRORLEVEL%
if %erlvl% EQU 16 echo >> %logfile% %date% %time% *** !!! FATAL ERROR - NOTHING COPIED !!! *** && set err=yes
if %erlvl% EQU 15 echo >> %logfile% %date% %time% * FAIL + MISMATCHES + XTRA + OKCOPY * && set err=yes
if %erlvl% EQU 14 echo >> %logfile% %date% %time% * FAIL + MISMATCHES + XTRA * && set err=yes
if %erlvl% EQU 13 echo >> %logfile% %date% %time% * FAIL + MISMATCHES + OKCOPY * && set err=yes
if %erlvl% EQU 12 echo >> %logfile% %date% %time% * FAIL + MISMATCHES * && set err=yes
if %erlvl% EQU 11 echo >> %logfile% %date% %time% * FAIL + XTRA + OKCOPY * && set err=yes
if %erlvl% EQU 10 echo >> %logfile% %date% %time% * FAIL + XTRA * && set err=yes
if %erlvl% EQU 9 echo >> %logfile% %date% %time% * FAIL + OKCOPY * && set err=yes
if %erlvl% EQU 8 echo >> %logfile% %date% %time% * FAIL * && set err=yes
if %erlvl% EQU 7 echo >> %logfile% %date% %time% * MISMATCHES + OKCOPY + XTRA *
if %erlvl% EQU 6 echo >> %logfile% %date% %time% * MISMATCHES + XTRA *
if %erlvl% EQU 5 echo >> %logfile% %date% %time% * MISMATCHES + OKCOPY *
if %erlvl% EQU 4 echo >> %logfile% %date% %time% * MISMATCHES *
if %erlvl% EQU 3 echo >> %logfile% %date% %time% * OKCOPY + XTRA *
if %erlvl% EQU 2 echo >> %logfile% %date% %time% * XTRA *
if %erlvl% EQU 1 echo >> %logfile% %date% %time% * OKCOPY *
if %erlvl% EQU 0 echo >> %logfile% %date% %time% * NO CHANGES / NOCOPY *

:: *** Delete files & folder older than 365 days ****
::forfiles /p %destination% /s /m *.* /c "cmd /c del @path" /d -365
::for /f "tokens=*" %d in ('dir %destination% /ad/b/s ^| sort /R') do rd "%d"
::echo Files older than 365 days deleted

:: *** Send Email ***
echo Set objNet = CreateObject("WScript.Network") >%emailer%
echo strHostName = objNet.ComputerName >>%emailer%
echo Set email = CreateObject("CDO.Message") >>%emailer%
if "%err%"=="" echo email.Subject = strHostName ^& " - Backup Report" >>%emailer%
if "%err%"=="yes" echo email.Subject = strHostName ^& " - FAILED Backup Report" >>%emailer%
echo email.From = %mailfrom% >>%emailer%
echo email.To = "%rcptto%" >>%emailer%
if "%err%"=="" echo email.TextBody = "Copy completed as %username% on " ^& strHostName ^& ".  Please check the attached report" >>%emailer%
if "%err%"=="yes" echo email.TextBody = "Copy as %username% has FAILED on " ^& strHostName ^& ".  Please check the attached report" >>%emailer%
echo email.AddAttachment "%logfile%" >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")="UserName" >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")="PassWord" >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2 >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")=%smtpsrv% >>%emailer%
echo email.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 >>%emailer%
echo email.Configuration.Fields.Update >>%emailer%
echo email.Send >>%emailer%
echo set email = Nothing >>%emailer%

start %emailer%
timeout 1 >nul /nobreak && del /q %emailer%
timeout 1 >nul /nobreak && del /q %logfile%

:end
ENDLOCAL
title
exit /B
 

No comments:

Post a Comment

Blog Archive