Automated Vault backup with emailer

Marketing
Marketing
  • Updated

by Chris Smith

The Autodesk Vault batch driven backup system allows server admins to automate the in-built backup system in Vault using a batch script and Windows Task Scheduler and has been a standard offering in all Vault QuickStart and Upgrade projects.

This document will explain how to include a smtp driven emailer using PowerShell for when the backup system fails to run/create a backup, removing the need for daily checks by the system admins.

Vault Backup script

vault back 1.png

The script used to automatically trigger a Vault backup procedure is a simple .bat (Batch) text file. The script will start the backup process automatically when this script file is triggered (usually from a Windows Task) and save the backup files in folder A. The next time the file is triggered, it will copy the files from folder A to folder B and save a new backup in folder A. This ensures there should always be 1 “good” backup should the backup process fail.

The latter portion of the script will check the system property value %errorlevel% for a value other than 0 and using additional PowerShell code send a notification email.

A copy of this batch script has been provided below. It can be copied into a simple notepad text file and saved with the .bat file extension, e.g. “Vault Backup.bat” though I would recommend using a viewer like Notepad++ or VS Code, both of which are free and display code in an easier to digest format than Notepad. For this document I will be using Notepad++.

Set AdminUser=YourAdminUsername

Set AdminPass=YourAdminPassword

Set BackupPath=D:\Vault Backup

Set PathToEmailerFile="D:\Vault Backup\Emailer.ps1"

:: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

REM Start the backup process

:: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

RMDIR /Q /S "%BackupPath%\B"

MOVE /Y "%BackupPath%\A" "%BackupPath%\B

MKDIR "%BackupPath%\A\"

Set BackupLocation=%BackupPath%\A

Set LogFilePath=%BackupPath%\VaultBackup.txt

"C:\Program Files\Autodesk\Vault Server 2025\ADMS Console\Connectivity.ADMSConsole.exe" -Obackup -B"%BackupLocation%" -L"%LogFilePath%" -VU%AdminUser% -VP%AdminPass% -VAL -S -DBSC

set "errorcode=%errorlevel%"

::Check if backup was successful (Errorlevel will have a value of 0 if successful)

(

              :: Check if the backup process was successful

              IF %errorcode% equ 0 (

                             REM Backup was successful, log success (optional)

                             echo Backup completed successfully at %DATE% %TIME% >> %LogFilePath%

              ) ELSE (

                             REM Backup failed, send email notification (Make sure the path in this line is correct)

                             powershell.exe -ExecutionPolicy Bypass -File %PathToEmailerFile%

              )

)

 

Backup Script breakdown

The code in this simple Batch script has been created so that minimal changes are needed from the user. The only changes that are required are the top 4 Set commands as shown below.

2vault back 1.png

For the backup operation to run it needs Vault administrator privileges. These are the first 2 Set options, an admin username, and the password for that Vault administrator account.

The third line is the path in which you are saving the backup data created by the ADMS backup, in this example D:\Vault Backup. Change the value after the = to your preferred location.

vault back 3.png

The fourth Set option is the path to the emailer PowerShell file that we will look at next. It is important to note that the file path (in my example D:\VaultBackup\Emailer.ps1) is wrapped in quote marks (“”), this is important as the value must be passed as a string to function. Change the value inside the quote marks to the location of the ps1 file. This can be done after the PowerShell script has been created.

Backup failure notification emailer

The emailer is another short script file written using Windows PowerShell which gives a little more “freedom” with what we can automate than simple Batch script. In this case, sending an email.

A copy of this script is available below and can again be saved in a Notepad text file and saved with the file extension .ps1. Again, Notepad++ or VS Code will make it easier to read.

 

# This script will email a notification message should the backup fail

# Define paths

$logFilePath = "D:\Vault Backup\VaultBackup.txt" #Set to same path as batch script

$tempFilePath = "D:\Vault Backup\A\FailLog.txt"

# Define email settings

$emailFrom = "YourSenderEmail" # Sender email address

$emailTo = "RecieverEmailAddress" # Recipient email address

$emailDate = Get-date -Format "dd-MM-yyyy"

$smtpServer = "smtp.YourSmtpServer.com" # smtp Server address

$smtpPort = 587 # smtp Port

$emailSubject = "Vault Backup Failed: " + $emailDate

$emailBody = "The Autodesk Vault backup failed. Please check the logs for more details."

$smtpUsername = "SMTP-Username" # Valid smtp user account(Usually sender email account)

$smtpPassword = "SMTP-Password" # Password for smtp account

# Get the last 30 lines from the VaultBackup.txt log file and add to text file for email attachment

get-content $logFilePath | select-object -last 30 > $tempFilePath

# Create a new MailMessage object

$mailMessage = New-Object System.Net.Mail.MailMessage

$mailMessage.From = $emailFrom

$mailMessage.To.Add($emailTo)

$mailMessage.Subject = $emailSubject

$mailMessage.Body = $emailBody

$mailMessage.Attachments.Add($tempFilePath)

# Create and configure the SmtpClient

$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer, $smtpPort)

$smtpClient.EnableSsl = $true

$smtpClient.Credentials = New-Object System.Net.NetworkCredential($smtpUsername, $smtpPassword)

# Send email to emailTo recipient with attachment

$smtpClient.Send($mailMessage)

$mailMessage.Dispose()

$smtpClient.Dispose()

# Delete the temporary file

Remove-Item -Path $tempFilePath -ErrorAction SilentlyContinue

Emailer code breakdown

This second script has also been created to make it as easy as possible to edit with little coding experience. There are 2 sections that will need to be updated. The first are some simple file paths.

vault back 4.png

The $logFilePath should match the path you set up in the previous batch script. Note again that it is wrapped in quote marks.

The $tempFilePath is a location where a temporary .txt file will be generated that will extract the last 30 lines from the backup log and attach it to the email we will send. This isn’t a necessary step but will give the email receiver an idea as to why the backup failed without having to log onto the server which can be useful for servers controlled by external suppliers or for users without credentials to sign into the server. Once the file has been created and attached it will get deleted at the end of this script.

Emailer SMTP details

vault back 5.png

The next part that we need to change requires SMTP (Simple Mail Transfer Protocol) details. These are basically an email address and a password for that email account, an email server and a port for that server. This information can usually be given by your IT provided but for some public SMTP server you can find the details using a web browser.

vault back 6.png

Figure 1 - Gmail SMTP details found using web search

 

The next details we need to update are:

$emailFrom This is the address the recipient will see as sender (e.g. VaultBackup@gmail.com)

$emailTo is the recipient of the notification email (e.g. VaultUser@company.org)

$smtpServer is the server address for the email server (e.g. smtp.Server.com)

$smtpPort is the port number required for the smtp server (e.g. 587)

$smtpUsername is the username that can access the smtp server. This is usually the sender email address as the email is coming from a valid address on that server.

$smptPassword is the password for the above account

Once the above details have been updated with valid information the script can be saved.

Testing – Emailer trigger

Providing all the file paths are correct in both scripts and the smtp details are valid, the code should all work. You can test this by editing the .bat file we created earlier and changing the valid username, like removing the last character  and saving the batch script with this edit.

vault back 7.png

Then run the batch script (double click or RMB and run as Administrator). This should start the backup process, and we should see the command console as it tries to work…

vault back 8.png

But it will fail due to the incorrect username, and we should get an email with an attachment.

vault back 9.pngvault back 10.png

And the attachment will show the last 30 lines of information in the log file.

vault back 11.png

This should verify that the emailer triggers when the backup fails due to errors.

Make sure to set the admin username value back to the correct value in the batch script.

vault back 12.png

Testing – Backup process

 

Testing the backup process section of the batch script may not be as simple as testing the emailer trigger depending on Vault size. If the Vault is new, then the backup can be tested by setting the batch script values to the correct values required and running the batch script a second time. This will generate the backup folder with the backup files and folders.

vault back 13.png

Manually running the script before setting up an automatic trigger is the best way to test that the script is functioning, however this may not be feasible for existing Vaults as a backup could take several hours. In this case we would test the script in a “trial by fire” approach by creating the automatic trigger and checking the results after the expected backup should have run.

 

Automatic backup task – Windows Task Scheduler

vault back 14.png

vault back 15.png

The easiest method of triggering the script automatically is with Windows task scheduler. It is simple to set up the task but there are some important steps that will ensure your task runs.

First, we need to create the task that will run our script. This can be done by clicking the Create Basic Task… option in the Actions panel and following the instructions in the creation wizard.

vault back 16.png

The creation wizard is broken down into 4 sections. The first section is general information about the task. The task must have a name to identify the task. It is also a good idea to fill in the Description box so that other users know what the task is doing without having to interrogate the task later.

vault back 17.png

The next page in the wizard is for the timing of the task trigger. I recommend setting this up as Weekly as this allows the selection of specific days during the week. Most companies do not work over the weekend so running a backup on those days is wasted effort. I have also chosen to trigger my task at 2am as there is a very low chance there will still be active Vault users at that time (Vault cannot be accessed during a backup).

vault back 18.pngvault back 19.png

The third option is the Action we want to trigger. To run our script, we will need the Start a program option on the first page…

vault back 20.png

And then select our .bat backup script on the following page.

vault back 21.png

The final Finish page allow us to review the details we provided for the task before committing it to the task library. I will also check the box for the Open the Propertied dialog for this task when I click Finish. This is an important step when first creating the task.

vault back 22.png

Opening the Properties panel will allow us to change the Run only when user is logged on option to Run whether user is logged on or not. By selecting the latter option we need to provide admin user credentials so that the task can run when the users are offline.

vault back 23.png

Once the task has been set up you will need to wait for the trigger in the task to run it, though you could manually trigger the task, it will both verify the time trigger works, and the user credentials are ok by letting the task trigger automatically.

Conclusion

By creating an automated backup system for Autodesk Vault, we are ensuring our data is as safe as possible while removing the manual task of checking and verifying the data is regularly backed up.

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.