Skip to content

Instantly share code, notes, and snippets.

@marisks
Last active June 13, 2018 05:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marisks/5747855 to your computer and use it in GitHub Desktop.
Save marisks/5747855 to your computer and use it in GitHub Desktop.
Backup RavenDB all DBs by providing tenants in parameter. Works with PowerShell 2. For usage see backup_dbs.ps1 and restore_dbs.ps1 Original script works only with PowerShell 3: http://ravendb.net/kb/46/backing-up-all-databases-using-powershell
param(
[parameter(Mandatory=$true)]
[string]
$ravenUrl,
[parameter(Mandatory=$true)]
[string]
$backupDir,
[parameter(Mandatory=$true)]
[string]
$ravenBackupTool,
[parameter(Mandatory=$true)]
[string[]]
$tenantDBs,
[string]
$systemDbName = "System",
[string]
$dateFormat = "yyyy-MM-dd_HHmm",
[switch]
$noLog = $false
)
function WriteToLog
{
param ([string]$msg, [System.Diagnostics.EventLogEntryType]$type, [switch]$tohost=$false)
if ($noLog -eq $false)
{
$log=Get-WmiObject win32_nteventlogfile -filter "filename='application'"
if ($log.Sources -notcontains "RavenDBBackup")
{
try
{
New-EventLog -logname Application -source RavenDBBackup
}
catch [System.InvalidOperationException] {}
}
Write-EventLog -logname Application -source RavenDBBackup -eventid 8080 -entrytype $type -Message $msg
}
if ($tohost)
{
Write-host $msg
}
}
$backupDir = $backupDir.TrimEnd('\')
$ravenBackupTool = $ravenBackupTool.TrimEnd('\')
$withErrors = $false
#Create directory
$currentDate = Get-date -format $dateFormat
if (-not (test-path "$backupDir\$currentDate")) {
new-item -path $backupDir -name $currentDate -itemtype directory | out-null
}
$backupDirThis = "$backupDir\$currentDate"
$backupMsg = ""
$backupSuccess = $false
#Backup default database
Write-host "Backing up default database"
& "$ravenBackupTool\Raven.Backup.exe" --url=$ravenUrl --dest=$backupDirThis\$systemDbName | Tee-Object -Variable backupMsg | select-string "esent backup complete" -Quiet -OutVariable backupSuccess | Out-Null
if ([bool]$backupSuccess -eq $false)
{
$withErrors = $true
WriteToLog -msg "Failed to backup $backupDirThis\$systemDbName with message: $backupMsg" -type Warning -tohost $true
}
else
{
Write-host "Successfully backed up $backupDirThis\$systemDbName"
}
#Backup tenants
Write-host "Backing up tenants"
foreach ($tenant in $tenantDBs)
{
& "$ravenBackupTool\Raven.Backup.exe" --url=$ravenUrl/databases/$tenant --dest=$backupDirThis\$tenant | Tee-Object -Variable backupMsg | select-string "esent backup complete" -Quiet -OutVariable backupSuccess | Out-Null
if ([bool]$backupSuccess -eq $false)
{
$withErrors = $true
WriteToLog -msg "Failed to backup $backupDirThis\$tenant with message: $backupMsg" -type Warning -tohost $true
}
else
{
Write-host "Successfully backed up $backupDirThis\$tenant"
}
}
#Write final status to log
if ($withErrors -eq $false)
{
WriteToLog -msg "Backup completed without errors." -type Information -tohost $true
}
else
{
WriteToLog -msg "Backup completed with errors" -type Warning -tohost $true
}
& '.\backup.ps1' -ravenUrl http://localhost:8080 -backupDir d:\backup -ravenBackupTool d:\ravendb\tools -tenantDBs MyDB1,MyDB2
& '.\Raven.Server.exe' -src D:\RavenDbBackup\2013-06-10_1333\System -dest "C:\Program Files\RavenDB\Server\Data" -restore
& '.\Raven.Server.exe' -src D:\RavenDbBackup\2013-06-10_1333\MyDB1 -dest "C:\Program Files\RavenDB\Server\Tenants\MyDB1" -restore
& '.\Raven.Server.exe' -src D:\RavenDbBackup\2013-06-10_1333\MyDB2 -dest "C:\Program Files\RavenDB\Server\Tenants\MyDB2" -restore
@bobmclaren
Copy link

Great script! I have implemented this in our environment with one small change in order to support Voron databases. Just needed to change your two select-string commands to use the following instead: "(esent backup complete|voron backup db finished)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment