How to Use PowerShell to backup and delete AX application and database

There are many VPCs running in our Virtual Server for the Client Development purpose. As we develop a lot on the AX applicatoins, we need to backup the application and database everyday and also before the main changes in AX.

Here is the powershell scripts we are using to backup AX Database and application backup files.

1. Run the SQL Server maintenance Plan to set up the automatic SQL Server backup.

2. create the Powershell Scripts in the beneath: 

// You can also copy the files to some folders if you don’t like to map network driver

net use Z: \\10.0.0.236\databasebackup pass@word1 /user:Contoso\administrator

move-item -path Z:\* -Destination C:\DatabaseBackUp

net use Y: \\10.0.0.236\AX593 pass@word1 /user:Contoso\administrator

$a = (get-date).month

$b= (get-date).day

new-item C:\Test$a$b -type directory

copy-item -Recurse Y:\* C:\Test$a$b

$Now = Get-Date

//only keep the last three days backup.

$Days=3

$TargetDatabaseFolder = C:\DatabaseBackUp

$TargetApplicationFolder=C:\Test$a$b

$LastWrite = $Now.AddDays(-$days)

Get-ChildItem $TargetDatabaseFolder | where {$_.LastWriteTime -le "$LastWrite"} | remove-item

Get-ChildItem $TargetApplicationFolder | where {$_.LastWriteTime -le "$LastWrite"} | remove-item -recurse –force

3. Add the Powershell scripts to the Task Manager to run everyday or weeekdays.

Advertisements
This entry was posted in Dynamics AX and SQL Server. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s