Puisqu'un script AutoIt est requis ...
Voici une version tel que je la ferait pour moi (Il y a même l'envoi du mail que l'on peux activer ou pas ^^).
J'utilise 2 fichiers afin de ne pas avoir à recompiler le script en cas de changement sur la config des serveurs ou du mail ...
J'ai pas pu tester la copie des fichiers, mais le reste fonctionne.
Il est très certainement possible de l'améliorer encore. Je ne doute pas que les lecteurs de ce sujet feront part de leur avis.
Code : Tout sélectionner
#AutoIt3Wrapper_Outfile=archivage_sauvegarde_serveurs_mensuelle.exe
#include <File.au3>
#include <Inet.au3>
Global $sFilePath, $sDestPath
Global $sMonthFr = GetMonNameFr(@MON) ; Mois en litéral Français
Global $fSRVList = @ScriptDir & "\SRVList.ini" ; Chemin du fichier de config
Global $sLogFile = IniRead($fSRVList, "LOG_FILE", "LOGFILE", "") & "_" & @YEAR & @MON & ".log" ; Fichier LOG
Global $aSRVList = IniReadSectionNames($fSRVList) ; Tableau des sections
; Info pour mail
Global $SendMail = IniRead($fSRVList, "MAIL_CFG", "SendMail", 0)
Global $ToAddress = IniRead($fSRVList, "MAIL_CFG", "ToAddress", "")
Global $FromAddress = IniRead($fSRVList, "MAIL_CFG", "FromAddress", "")
Global $SmtpServer = IniRead($fSRVList, "MAIL_CFG", "SmtpServer", "")
Global $Username = IniRead($fSRVList, "MAIL_CFG", "Username", "")
Global $Password = IniRead($fSRVList, "MAIL_CFG", "Password", "")
Global $AttachFiles = IniRead($fSRVList, "MAIL_CFG", "AttachFiles", $sLogFile)
Global $FromName = IniRead($fSRVList, "MAIL_CFG", "FromName", "")
Global $Subject = IniRead($fSRVList, "MAIL_CFG", "Subject", "") & " - " & @MDAY & "-" & @MON & "-" & @YEAR
Global $Body = IniRead($fSRVList, "MAIL_CFG", "Body", "")
Global $CcAddress = IniRead($fSRVList, "MAIL_CFG", "CcAddress", "")
Global $BccAddress = IniRead($fSRVList, "MAIL_CFG", "BccAddress", "")
Global $Importance = IniRead($fSRVList, "MAIL_CFG", "Importance", "0")
Global $IPPort = IniRead($fSRVList, "MAIL_CFG", "IPPort", "25")
Global $ssl = IniRead($fSRVList, "MAIL_CFG", "ssl", "0")
_FileWriteLog($sLogFile, "Exécution de la tâche planifiée de sauvegarde complète.")
; Boucle de traitement du tableau des sections
For $i = 1 To UBound($aSRVList) - 1
If $aSRVList[$i] = "LOG_FILE" Then ContinueLoop ; Passe la section [LOG_FILE]
If $aSRVList[$i] = "MAIL_CFG" Then ContinueLoop ; Passe la section [MAIL_CFG]
$sFilePath = IniRead($fSRVList, $aSRVList[$i], "FilePath", "") ; Récup du chemin source
$sDestPath = IniRead($fSRVList, $aSRVList[$i], "DestPath", "") ; Récup du chemin destination
; On vérifie que la source existe
If Not FileExists($sFilePath) Then
_FileWriteLog($sLogFile, "*** Erreur la source de " & $aSRVList[$i] & " n'existe pas !")
ContinueLoop
EndIf
; Si le dossier de destination existe et contient des fichiers .vbk, on les supprime
If FileExists($sDestPath & "\$sMon\*.vbk") Then FileDelete($sDestPath & "\$sMon\*.vbk")
_FileWriteLog($sLogFile, "Début de la sauvegarde de " & $aSRVList[$i])
; On copie les nouveaux fichiers de sauvegarde en créant le dossier si nécessaire
If FileCopy($sFilePath & "\*.vbk", $sDestPath & "\" & $sMonthFr & "\", 9) Then
_FileWriteLog($sLogFile, "Fin de la sauvegarde de " & $aSRVList[$i])
Else
_FileWriteLog($sLogFile, "*** Erreur lors de la sauvegarde de " & $aSRVList[$i])
EndIf
_FileWriteLog($sLogFile, "------------------------------------------------------")
_FileWriteLog($sLogFile, "Fin de la sauvegarde de " & $aSRVList[$i])
Next
_FileWriteLog($sLogFile, "Fin de la tâche planifiée de sauvegarde complète." & @CRLF & _
"=======================================================================")
; Envoi du Mail si SendMail=1
If $SendMail = 1 Then
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
If @error Then Exit 1
EndIf
Exit 0
Func GetMonNameFr($szMN)
Switch $szMN
Case 01
$Mon = "Janvier"
Case 02
$Mon = "Fevrier"
Case 03
$Mon = "Mars"
Case 04
$Mon = "Avril"
Case 05
$Mon = "Mai"
Case 06
$Mon = "Juin"
Case 07
$Mon = "Juillet"
Case 08
$Mon = "Aout"
Case 09
$Mon = "Septembre"
Case 10
$Mon = "Octobre"
Case 11
$Mon = "Novembre"
Case 12
$Mon = "Decembre"
Case Else
$Mon = $szMN
EndSwitch
Return $Mon
EndFunc ;==>GetMonNameFr
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
; Source : forum us
Global $objEmail = ObjCreate("CDO.Message")
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail.To = $s_ToAddress
Global $i_Error = 0
Global $i_Error_desciption = ""
If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
$objEmail.Subject = $s_Subject
If StringInStr($as_Body, "<") And StringInStr($as_Body, ">") Then
$objEmail.HTMLBody = $as_Body
Else
$objEmail.Textbody = $as_Body & @CRLF
EndIf
If $s_AttachFiles <> "" Then
Global $S_Files2Attach = StringSplit($s_AttachFiles, ";")
For $x = 1 To $S_Files2Attach[0]
$S_Files2Attach[$x] = __PathFull($S_Files2Attach[$x])
ConsoleWrite('@@ Debug(62) : $S_Files2Attach = ' & $S_Files2Attach & @LF & '>Error code: ' & @error & @LF) ;### Debug Console
If FileExists($S_Files2Attach[$x]) Then
$objEmail.AddAttachment($S_Files2Attach[$x])
Else
ConsoleWrite('!> File not found to attach: ' & $S_Files2Attach[$x] & @LF)
SetError(1)
Return 0
EndIf
Next
EndIf
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
If Number($IPPort) = 0 Then $IPPort = 25
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
If $s_Username <> "" Then
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
EndIf
If $ssl Then
$objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
;Update settings
$objEmail.Configuration.Fields.Update
; Set Email Importance
Switch $s_Importance
Case "High"
$objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "High"
Case "Normal"
$objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
Case "Low"
$objEmail.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
EndSwitch
$objEmail.Fields.Update
; Sent the Message
$objEmail.Send
If @error Then
SetError(2)
Return $oMyRet[1]
EndIf
$objEmail = ""
EndFunc ;==>_INetSmtpMailCom
Func MyErrFunc()
; Source : forum us
$HexNumber = Hex($oMyError.number, 8)
$oMyRet[0] = $HexNumber
$oMyRet[1] = StringStripWS($oMyError.description, 3)
ConsoleWrite( "! " & @ScriptFullPath & " (" & $HexNumber & ") :==> ### COM Error !" & @CRLF & "! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF)
Return SetError(1); something to check for when this function returns
EndFunc ;==>MyErrFunc
Func __PathFull($sRelativePath, $sBasePath = @WorkingDir)
; Source : forum us
If Not $sRelativePath Or $sRelativePath = "." Then Return $sBasePath
; Normalize slash direction.
Global $sFullPath = StringReplace($sRelativePath, "/", "\")
Global Const $sFullPathConst = $sFullPath
Global $sPath
Global $bRootOnly = StringLeft($sFullPath, 1) = "\" And StringMid($sFullPath, 2, 1) <> "\"
If $sBasePath = Default Then $sBasePath = @WorkingDir
For $i = 1 To 2
$sPath = StringLeft($sFullPath, 2)
If $sPath = "\\" Then
$sFullPath = StringTrimLeft($sFullPath, 2)
Global $nServerLen = StringInStr($sFullPath, "\") - 1
$sPath = "\\" & StringLeft($sFullPath, $nServerLen)
$sFullPath = StringTrimLeft($sFullPath, $nServerLen)
ExitLoop
ElseIf StringRight($sPath, 1) = ":" Then
$sFullPath = StringTrimLeft($sFullPath, 2)
ExitLoop
Else
$sFullPath = $sBasePath & "\" & $sFullPath
EndIf
Next
; If this happens, we've found a funky path and don't know what to do
; except for get out as fast as possible. We've also screwed up our
; variables so we definitely need to quit.
; If $i = 3 Then Return ""
; A path with a drive but no slash (e.g. C:Path\To\File) has the following
; behavior. If the relative drive is the same as the $BasePath drive then
; insert the base path. If the drives differ then just insert a leading
; slash to make the path valid.
If StringLeft($sFullPath, 1) <> "\" Then
If StringLeft($sFullPathConst, 2) = StringLeft($sBasePath, 2) Then
$sFullPath = $sBasePath & "\" & $sFullPath
Else
$sFullPath = "\" & $sFullPath
EndIf
EndIf
; Build an array of the path parts we want to use.
Global $aTemp = StringSplit($sFullPath, "\")
Global $aPathParts[$aTemp[0]], $j = 0
For $i = 2 To $aTemp[0]
If $aTemp[$i] = ".." Then
If $j Then $j -= 1
ElseIf Not ($aTemp[$i] = "" And $i <> $aTemp[0]) And $aTemp[$i] <> "." Then
$aPathParts[$j] = $aTemp[$i]
$j += 1
EndIf
Next
; Here we re-build the path from the parts above. We skip the
; loop if we are only returning the root.
$sFullPath = $sPath
If Not $bRootOnly Then
For $i = 0 To $j - 1
$sFullPath &= "\" & $aPathParts[$i]
Next
Else
$sFullPath &= $sFullPathConst
; If we detect more relative parts, remove them by calling ourself recursively.
If StringInStr($sFullPath, "..") Then $sFullPath = __PathFull($sFullPath)
EndIf
; Clean up the path.
Do
$sFullPath = StringReplace($sFullPath, ".\", "\")
Until @extended = 0
Return $sFullPath
EndFunc ;==>_PathFull