Gestione di Windows Backup su Windows Server 2008

From RVM Wiki
Revision as of 17:54, 18 February 2013 by Gabriele.vivinetto (talk | contribs) (Created page with "* Installare l'ultima versione di Powershell 2.0 da [http://support.microsoft.com/kb/968929 Windows Management Framework (Windows PowerShell 2.0, WinRM 2.0, and BITS 4.0)] * Abi…")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
  • Abilitare l'esecuzione di script powershell:
powershell "Set-ExecutionPolicy Unrestricted"
  • Se non è installato, installare il servizio backup:
ServerManagerCmd.exe -install Backup –allSubFeatures
  • Ecco lo script:
####################################################

#  WBBackup.ps1

#  Backup Windows 2008 with WindowsBackup

#  Version 1.0

#  modified: 19/02/2010

#  Autore: Gianluca Ferri

####################################################

# Mapping network share

#(New-Object -ComObject Wscript.Network.MapNetworkDrive)("X:", "\\nas01\Backup", $false, "BackupUser","BackupPassword")

 

# Var

$Customer="Customer"

$DestFolder="\\Nas01\Backup\" # Backup destination

$LogFile="C:\Dati\Logs\Backup\" # Logfile

$LogsFolder="C:\Dati\Logs\Backup\" # Logs archive folder

[Array] $Services="" #"Virtual Machine Management Hyper-V" # Array of services to stop/start

$SMTPServer="SMTP server"

$SMTPServerUser="SMTPUser"

$SMTPServerPass=ConvertTo-SecureString "SMTPPassword" -AsPlainText -Force

 

$MailFrom=$Customer + " Backup <indirizzo mittente>"

$MailTo="indirizzo destinatario"

$MailBcc="indirizzo destinatario"

$Emailsubject="[" + $Customer + "] Report backup " + (get-wmiobject win32_computersystem).name

$DaysOld=15 # Logs retain days

$Culture= New-Object Globalization.CultureInfo "it-IT"

 

$today=Get-Date

$DestFolder=$DestFolder + $today.dayofweek.tostring($Culture)

if (!(Test-Path $DestFolder))

    { 

    # Make folder

    New-Item $DestFolder -type directory

    }

# Stop services

if ($Service.count>0) {foreach ($serv in $Services) {stop-service $serv}}

 

# Start backup

$commandline="wbadmin.exe"

$params={start backup `-backuptarget:$DestFolder `-include:"C:" `-VSSFull -allcritical -quiet}

$LogFile=$LogFile + $Customer + (Get-Date -f yyyyMMdd) + ".log"

$exec = "$commandline $params >$LogFile"

Invoke-Expression $exec

 

# Restart services

if ($Service.count>0) {foreach ($serv in $Services) {start-service $serv}}

 

# Send email report

$Credential=New-Object System.Management.Automation.PSCredential ($SMTPServerUser, $SMTPServerPass)

send-mailmessage -to $MailTo -bcc $MailBcc -from $MailFrom -subject $Emailsubject -Attachments $LogFile -smtpServer $SMTPServer -Credential $Credential

 

# Delete older logs

dir $LogsFolder -recurse | where {!$_.PsIsContainer -AND $_.lastWriteTime -lt (Get-Date).AddDays(-$DaysOld) } | select LastWriteTime,@{n="Path";e={convert-path $_.PSPath}} | Remove-Item -force
  • Lanciarlo come:
powershell.exe -command "& 'percorsodelloscript\WBBackup.ps1'"

Riferimenti