Simple powershell network backup script using wbadmin

I needed a simple script to backup a server running Windows Server 2008 to a network location with email reports.

At first I did some prerequisites(the steps marked with * at the beginning aren’t absolutely necessary):

*1. Create a CNAME on my dns for the server where the backups reside: backup.mydomain.com .

2. Create a share on the backup server.

3. Create the folder structure for the backups: on the root of the share create a folder named exactly as the server (the result of hostname command on the backed up server); create in this folder 7 other folders named by each day of the week -monday, tuesday,wednesday, thursday, friday, saturday, sunday.

 

The script:

#—————————————————–Start script—————————————————————-

function SendEmail($To, $From, $Subject, $Body, $attachment, $smtpServer)
{
        Send-MailMessage -To $To -Subject $Subject -From $From -Body $Body -Attachment $attachment -SmtpServer $smtpServer
}
$emailto=”your_email@yourdomain.com
$emailfrom=”backup@yourdomain.com
$day=(get-date).dayofweek
$hname=hostname
$backuplocation="\\backup.yourdomain.lan\backup$\$hname\$day\"
$backuplog="$backuplocation"+(get-date -f MM-dd-yyyy)+"-backup-$hname.log"
$emailserver="mail.yourdomain.com"
Write-Output ("———————– Backup started on – $(Get-Date –f o) ————————-") | Out-File "$backuplog" -Append
$Error.Clear()
wbadmin start backup -backupTarget:$backuplocation -include:"d:,e:" -allcritical -vssfull -quiet | Out-File "$backuplog" -Append
if(!$?)
    {
        Write-Output ("———————– An error has occurred! Check it please!. – $(Get-Date –f o) ————————-") | Out-File "$backuplog" -Append
        SendEmail -To "$emailto" -From "$emailfrom" -Subject "backup failed" -Body "The backup has failed! Please check attached log." -attachment "$backuplog" -smtpServer "$emailserver"
        break
       
    }

Write-Output ("———————– Everything is OK! – $(Get-Date –f o) ————————-") | Out-File "$backuplog" -Append
SendEmail -To "$emailto" -From "$emailfrom" -Subject "backup $hname ok" -Body "The backup has succeeded!" -attachment "$backuplog" -smtpServer "$emailserver" .

#——————————————————–End script——————————————————————

Save it as a yourname.ps1 file and create the scheduled task accordingly to your needs. Below how you should start the task from the schedule tasks Actions.

scheduled

If your smtp server requires authentication create a separate account for this and update the SendEmail function so that it includes the

“–Credential” switch.

As you can see all the variables are defined at the start of the script:

$hname – is the name of the host that’s backed up

The rest of the variables are pretty straightforward.

What does the script:

1. Backup to a network location of your choice.

2. Create a backup log.

3. Check if the backup ended successfully/unsuccessfully.

4. Send an email with the result: success/failure and the log is attached.

Of course if you need to keep the backups more than a week you’ll have to copy them from there since wbadmin clears the folder where it creates the backup.

About these ads

8 thoughts on “Simple powershell network backup script using wbadmin

  1. What language is this code written in? If i want to timestamp the log file before and after the backup starts, how would i do that?

    Thanks,
    Jeremy

  2. i messed with your code so that i could add port and credentials for smtp server..hope it helps someone

    #—————————————————–start Backup—————————————————————-

    function SendEmail($To, $From, $Subject, $Body, $attachment, $smtpServer)
    {
    Send-MailMessage -To $To -Subject $Subject -From $From -Body $Body -Attachment $attachment -SmtpServer $smtpServer
    }
    $emailto = “me@example.com”
    $emailfrom = “srv01client@example.com”
    $day=(get-date).dayofweek
    $hname=hostname
    $backuplocation=”\\srv01\users\”
    $backuplog=”$backuplocation”+(get-date -f MM-dd-yyyy)+”-backup-$hname.log”
    $emailserver = “mail.server.com”
    $secpasswd = ConvertTo-SecureString “Passwordforsmtp” -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential (“srv01client@example.com”, $secpasswd )
    Write-Output (“———————– Backup started on – $(Get-Date –f o) ————————-“) | Out-File “$backuplog” -Append
    $Error.Clear()
    wbadmin start backup -backupTarget:$backuplocation -include:”c:,d:,e:” -allcritical -vssfull -quiet | Out-File “$backuplog” -Append
    if(!$?)
    {
    Write-Output (“———————– An error has occurred! Check it please!. – $(Get-Date –f o) ————————-“) | Out-File “$backuplog” -Append
    SendEmail -To “$emailto” -From “$emailfrom” -Subject “backup failed” -Body “The backup has failed! Please check attached log.” -attachment “$backuplog” -smtpServer “$emailserver” -credential $mycreds
    break

    }

    Write-Output (“———————– Everything is OK! – $(Get-Date –f o) ————————-“) | Out-File “$backuplog” -Append
    SendEmail -To “$emailto” -From “$emailfrom” -Subject “backup $hname ok” -Body “The backup has succeeded!” -attachment “$backuplog” -smtpServer “$emailserver” -credential $mycreds out string

    #——————————————————–finish BU script——————————————————————
    #make sure the shared drive has HOSTNAMEFOLDER| and sub folders mon-sun,see X example
    #obtained from http://abofh.wordpress.com/2011/06/16/simple-powershell-network-backup-script-using-wbadmin/

    first ps script, thanks!

  3. Do you know MKSBackup ?
    It’s free, the mail report is display a short resume, but includ the logfile and details from the event viewer.
    You can use SSL, TLS, and authentication to send your email or even your GMAIL account.
    The installation procedure create the task for you.

  4. Wow that was unusual. I just wrote an very long comment
    but after I clicked submit my comment didn’t appear. Grrrr… well I’m
    not writing all that over again. Anyways, just wanted to say great blog!

  5. Hello,

    I’m trying to create a script to backup an Exchange server I have, but I’m having problems with Wbadmin.

    The directory where the foundation has the name with space, for example: D:\Exchange Databases\

    Thus WBADMIN not accept and generates the following error:

    ERROR – Command syntax incorrect. Error: Exchange\Starter. See the command syntax below.

    I can’t change the name of the folder window and not have to make a move maintainability bases.

    Could you help me?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s