Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

How to dynamically backup daily ssas tabular database files for the past 7 days?

is there any way to dynamically backup daily ssas tabular database files for the past 7 days?

Daily backup, only retaining backup files from the past 7 days, files more than past 7 days will be deleted.

for example, today is 0613, only backup 0606-0612 daily backup files, tomorrow, 0614, only backup 0607-0613, and 0606 file will be deleted.

I had tried powershell command to do it. but the script cannot dynamically delete files, only backup no drop files. I don't know why.

this is the script:

$server = "localhost"
$database = "TabularProject2"
$backupPath = "D:\Backup\"

# Connect to SSAS server
$conn = New-Object Microsoft.AnalysisServices.Server
$conn.connect($server)

# Get database object
$db = $conn.databases[$database]

# Check if database object is null
if ($db -eq $null) {
Write-Host "Could not connect to database $database on server $server"
} else {
# Create backup file name
$backupFileName = "awdb-" + (Get-Date).ToString("yyyyMMdd-HHmmss") + ".abf"

# Execute backup operation
$backupFile = $backupPath + $backupFileName
$backup = $db.Backup($backupFile)
Write-Host "Backup created at $backupFile"

# Delete backup files older than 7 days
$dateToKeep = (Get-Date).AddDays(-7)
Get-ChildItem -Path $backupPath -Include "awdb-*.abf" | Where-Object { $_.LastWriteTime -lt $dateToKeep } | Remove-Item -Force

# Disconnect from server
$conn.Disconnect()
}

 

looking forward to your answer, thanks a lot.

 

1 Reply

  • You are passing a file object to Remove-Item which expects a string parameter. That line should probably be something like the following:

     

    Get-ChildItem -Path $backupPath -Include "awdb-*.abf" | Where-Object { $_.LastWriteTime -lt $dateToKeep } | % { Remove-Item $_.FullName -Force }