L'utilizzo di WinRar per le operazioni di backup può essere utile anche nell'ambito di un dominio
Active Directory
al fine di centralizzare tutti i files in un solo punto.
A tale scopo presento qui una variante di tR1 Winrar Backup che consente di fare i backup in remoto di cartelle condivise in rete.
Per prima cosa, non si fa uso in WinRar del parametro '@' ma di un file esterno che contiene le cartelle su cui eseguire i backup.
Lo script accetta alcuni parametri a riga di comando:
-C: (Indica un backup completo)
-LOG: (indica il file di log)
-L: (indica il file di elenco delle cartelle soggette a backup)
-P:<Password> (specifica una password da dare a WinRar)
'tR1 WinRar Backup
'Copyright (C) 2005-2006 vittorio benintende <vittorio@lucullo.it>
'This file is distributed under the GPL license
'
Option Explicit
'params
Const WORK_DIR="c:\backups\install"
Const DEST_DIR="c:\backups\data"
const ForReading=1
const ForWriting=2
Const ForAppending=8
const SUFF_COMPLETO="completo"
CONST SUFF_GIORNALIERO="incrementale"
const SUFF_EXT=".rar"
Const TEMP=".tmp"
const PASSWORD=" -hp"
Const FILE_NOT_FOUND="File Not Found"
Const MAX_LOG_SIZE=1000000
Const TITLE="tR1 Server Backup"
Const VER="1.42"
'files
Const DEF_LOG_FILE="backup.log"
Const DEF_LIST_FILE="myBackups.txt"
'logging
Const SUCCESS =0
Const ERROR =1
Const WARNING=2
Const INFORMATION=4
Const AUDIT_SUCCESS=8
Const AUDIT_FAILURE=16
Const DEF_NUM=5
Const REPLACE_CHAR="+"
Const ALT_REPLACE_CHAR="-"
Dim f, WshShell, FSO,fListFile,fTemp
Dim lngResult
Dim strWinRarCmd
Dim blCompleto,blWarning,blPassword
Dim lngPos,lngCiclo
Dim WIN_RAR_CMD, WIN_RAR_CMD2
Dim objArgs
Dim strArgs
Dim strcurrent, strCurrentCMD
Dim aryCampi,aryFiles
Dim strLogFile,strListFile
Dim strPassword
Dim lngLogSize
'network
Dim objNet
strWinRarCmd=WIN_RAR_CMD2
strLogFile=WORK_DIR & "\" & DEF_LOG_FILE
strListFile=WORK_DIR & "\" & DEF_LIST_FILE
strPassword=""
Set objArgs = WScript.Arguments
For lngPos = 0 to objArgs.Count - 1
blPassword=FALSE
Select Case UCase(objArgs(lngPos))
Case "-C"
strWinRarCmd=WIN_RAR_CMD
blCompleto=TRUE
End Select
If UCase(Left(objArgs(lngPos),5))="-LOG:" Then
strLogFile=Right(objArgs(lngPos),Len(objArgs(lngPos))-5)
End If
If UCase(Left(objArgs(lngPos),3))="-L:" Then
strListFile=Right(objArgs(lngPos),Len(objArgs(lngPos))-3)
End If
If UCase(Left(objArgs(lngPos),3))="-P:" Then
strPassword=PASSWORD & Right(objArgs(lngPos),Len(objArgs(lngPos))-3)
blPassword=True
End If
If blPassword Then
strArgs=strArgs & " -P:"
Else
strargs=strArgs & " " & objArgs(lngPos)
End If
Next
'argomenti
WIN_RAR_CMD = """c:\program files\winrar\winrar.exe"" a -ac -ag -dh " & strPASSWORD & " -inul -ibck -ilog" & strLogFile & " -m5 -os -ow -r -t -ts " & DEST_DIR & "\*" & SUFF_COMPLETO & SUFF_EXT & " ""?"""
WIN_RAR_CMD2= """c:\program files\winrar\winrar.exe"" a -ac -ao -ag -dh " & strPASSWORD & " -inul -ibck -ilog" & strLogFile & " -m5 -os -ow -r -t -ts " & DEST_DIR & "\*" & SUFF_GIORNALIERO & SUFF_EXT & " ""?"""
If blCompleto Then
strWinRarCmd=WIN_RAR_CMD
Else
strWinRarCmd=WIN_RAR_CMD2
End If
blWarning=FALSE
Set WshShell = WScript.CreateObject( "WScript.Shell" )
Set FSO = CreateObject("Scripting.FileSystemObject")
If Trim(strArgs)="" Then strArgs ="no"
WshShell.LogEvent INFORMATION, TITLE & " starting with " & strArgs & " options."
'prendiamo le dimensioni del log
On Error Resume Next
Set f = fso.getfile(strLogFile)
lnglogsize=CLng(f.Size)
'grezzo tail del log
If lngLogSize>MAX_LOG_SIZE Then
f.copy strlogfile & ".old", TRUE
Set f = Nothing
fso.deletefile (strLogFile)
Else
Set f = Nothing
End If
On Error GoTo 0
'inizio
Set f = fso.OpenTextFile(strLogFile , ForAppending, True)
f.writeline "--------------------------------------------------------------------"
f.writeline "(" & Now & ") Starting program..."
f.writeline TITLE & " v." & VER
f.writeline "starting with " & strArgs & " options."
f.writeline "Environment:"
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
If Err.Number <> SUCCESS Then 'If error occured then display notice
f.writeline "(" & Now & ") Error getting network info."
Else
f.writeline "User " & objNet.UserName & vbCRLF & _
"Computer " & objNet.ComputerName & vbCRLF & _
"Domain " & objNet.UserDomain
End if
On Error GoTo 0
Set objNet = Nothing
f.writeline "Reading from file '" & strListFile & "'"
On Error Resume Next
Set fListFile = fso.OpenTextFile(strListFile, ForReading, FALSE)
If err.number<>SUCCESS Then
f.writeline "(" & Now & ") Error opening list file."
WshShell.LogEvent Error, TITLE & " Error opening file."
f.close
WScript.Quit (1)
End If
On Error GoTo 0
f.close
Set f = Nothing
Do While not fListFile.AtEndOfStream
strCurrent = Trim(fListFile.ReadLine)
if strCurrent="" then strCurrent=";"
If Left(strCurrent,1)=";" Then
'commento
Else
aryCampi= Split(strCurrent, "*", -1, 1)
lngPos=DEF_NUM
If UBound(aryCampi)>0 Then
If not IsNumeric(aryCampi(1)) Then
aryCampi(1)=DEF_NUM
Else
aryCampi(1)=CLng(aryCampi(1))
If aryCampi(1)<1>10 Then
aryCampi(1)=DEF_NUM
End If
End If
lngPos=aryCampi(1)
End If
If arycampi(0)<>"" Then
Set f = fso.OpenTextFile( strLogFile, ForAppending, True)
strCurrentCMD=replace(replace(strWinRarCmd,"*",replace(replace(aryCampi(0),":",ALT_REPLACE_CHAR),"\",REPLACE_CHAR)),"?",aryCampi(0))
f.writeline vbcrlf & "###" & aryCampi(0) & "###"
f.writeline "(" & Now & ") [WinRAR] Creating " & replace(replace(aryCampi(0),":",ALT_REPLACE_CHAR),"\",REPLACE_CHAR) & " file..."
'consentiamo a rar di scrivere nel logv
f.close
Set f = Nothing
lngResult=WshShell.Run (strCurrentCMD ,0,true)
Set f = fso.OpenTextFile(strLogFile , ForAppending, True)
Select Case lngResult
Case SUCCESS
f.writeline "(" & Now & ") [WinRAR] Done."
Case 1
f.writeline vbcrlf & "(" & Now & ") [WinRAR] Cannot create empty file."
case 6
f.writeline "(" & Now & ") [WinRAR] Cannot find source."
blWarning=TRUE
Case 255
f.writeline "(" & Now & ") [WinRAR] Aborted by user."
blWarning=TRUE
Case Else
f.writeline vbcrlf & "(" & Now & ") [WinRAR] Error (" & lngResult & ")." 'aggiungo un ritorno a capo perché winrar non lo mette.
blWarning=TRUE
End Select
'controlliamo l'eliminazione
If lngResult=SUCCESS Then
If blCompleto Then
'cancelliamo tutti i gionalieri
f.writeline "(" & Now & ") Checking old files deletion..."
lngResult=WshShell.Run ("cmd.exe /c dir /B /O:D """ & DEST_DIR & "\" & replace(replace(aryCampi(0),":",ALT_REPLACE_CHAR),"\",REPLACE_CHAR) & SUFF_GIORNALIERO & "*" & SUFF_EXT & """ > " & strListFile & TEMP ,0,true)
'f.writeline ("cmd.exe /c dir /B /O:D """ & DEST_DIR & "\" & replace(aryCampi(0),"\",REPLACE_CHAR) & SUFF_GIORNALIERO & "*" & SUFF_EXT & """ > " & strListFile & TEMP & " 2 > c:\errore.txt")
On Error Resume Next
Set fTemp = fso.OpenTextFile(strListFile & TEMP, ForReading)
lngResult=Err.number
On Error GoTo 0
If lngResult=SUCCESS Then
On Error Resume Next
aryFiles= Split(fTemp.ReadAll, vbcrlf, -1, 1)
lngResult=Err.number
On Error GoTo 0
If lngResult=SUCCESS Then
f.writeline "(" & Now & ") Deleting " & CLng(UBound (aryFiles)) & " incremental files..."
For lngCiclo=0 to CLng(UBound (aryFiles))
If aryFiles(lngCiclo)<>"" and aryFiles(lngCiclo)<>FILE_NOT_FOUND Then
On Error Resume Next
fso.DeleteFile (DEST_DIR & "\" & aryFiles(lngCiclo))
If err<>SUCCESS Then
f.write "(" & Now & ") [Delete] Error (" & err & ") deleting file "
End If
On Error GoTo 0
f.writeline (DEST_DIR & "\" & aryFiles(lngCiclo))
End If
Next
f.writeline "(" & Now & ") Done."
End If 'lngResult
Else
f.writeline "(" & Now & ") [Delete] Error (" & lngResult & ") opening temp file."
End If
Set aryFiles= Nothing
'prendiamo i bkp completi ed eliminiamo quelli in eccesso
lngResult=WshShell.Run ("cmd.exe /c dir /B /O:D """ & DEST_DIR & "\" & replace(replace(aryCampi(0),":",ALT_REPLACE_CHAR),"\",REPLACE_CHAR) & SUFF_COMPLETO & "*" & SUFF_EXT & """ > " & strListFile & TEMP ,0,true)
On Error Resume Next
Set fTemp = fso.OpenTextFile(strListFile & TEMP, ForReading)
lngResult=Err.number
On Error GoTo 0
If lngResult=SUCCESS Then
On Error Resume Next
aryFiles= Split(fTemp.ReadAll, vbcrlf, -1, 1)
lngResult=Err.number
On Error GoTo 0
If lngResult=SUCCESS Then
If UBound (aryFiles) > lngPos Then
f.writeline "(" & Now & ") Deleting " & CLng(UBound (aryFiles) - lngPos) & " complete files..."
For lngCiclo=1 to CLng(UBound (aryFiles) - lngPos)
If aryFiles(lngCiclo)<>FILE_NOT_FOUND Then
On Error Resume Next
fso.DeleteFile (DEST_DIR & "\" & aryFiles(lngCiclo))
If err<>SUCCESS Then
f.write "(" & Now & ") [Delete] Error (" & err & ") deleting file "
End If
On Error GoTo 0
f.writeline (DEST_DIR & "\" & aryFiles(lngCiclo))
End If
Next
f.writeline "(" & Now & ") Done."
End If
End If 'lngResult
Else
f.writeline "(" & Now & ") [Delete] Error (" & lngResult & ") opening temp file."
End If
fTemp.close
Set fTemp = Nothing
On Error Resume Next
fso.DeleteFile( strListFile & TEMP)
On Error GoTo 0
End If
End If
f.writeline "(" & Now & ") Backup complete."
f.close
Set f=Nothing
End If
End If ';
Loop
fListFile.close
Set fListFile = Nothing
If blWarning Then
WshShell.LogEvent WARNING, TITLE & " Done. Some errors occurred during backup (See log file in '" & strLogFile & "')."
Else
WshShell.LogEvent SUCCESS, TITLE & " Done."
End If
'ESCI
Set FSO=Nothing
Set WshShell=Nothing
'WScript.Quit (SUCCESS)
Per utilizzare correttamente lo script è necessario creare un list file contenente i percorsi da sottoporre a backup. Se non diversamente specificato il file sarà
"c:\backups\install\myBackups.txt".
di seguito un esempio di file valido:
; Server 1
\\192.168.0.1\DIR_1
\\192.168.0.1\DIR_1
\\192.168.0.1\DIR_1
\\192.168.0.1\DIR_1
; Server 2
\\mioserver\DIR_2\*3
; Locale
c:\Winnt\system32\*1
; commento
Alcuni path sono seguiti da un '*' (separatore) ed un numero. Tale numero indica quanti backup completi conservare nel repository. Se non specificato il programma conserva gli ultimi 5 completi creati.
Ogni volta che un completo viene creato, tutti i backup incrementali
vengono eliminati.
Ricordate che il carattere "\" alla fine del path è fondamentale per il corretto funzionamento dello script.
Anche in questo caso è possibile schedulare un job
per avviare le operazioni senza l'intervento di un operatore. (attenzione ai permessi di lettura/scrittura).