Skip to content

Instantly share code, notes, and snippets.

@OakRaven
Last active September 10, 2015 10:36
Show Gist options
  • Save OakRaven/cfd1a00d4f9179c45184 to your computer and use it in GitHub Desktop.
Save OakRaven/cfd1a00d4f9179c45184 to your computer and use it in GitHub Desktop.
PowerShell Script for performing hotcopy Subversion backup and archiving.
# -------------------------------------------------------------------------------------------------
# SETTINGS
# -------------------------------------------------------------------------------------------------
$REPO_PATH = "X:\SVNREPOS"
$TEMP_BACKUP_PATH = "X:\SVNREPOS-BACKUP"
$PERM_BACKUP_PATH = "X:\SVN-BACKUPS"
$SVNADMIN_EXE_PATH = "C:\Program Files (x86)\VisualSVN Server\bin\svnadmin.exe"
$7ZA_EXE_PATH = "X:\SVN-BACKUPS\7za.exe"
$DAYS_TO_KEEP_BACKUPS = 14
# -------------------------------------------------------------------------------------------------
# REMOVE FILES IN TEMPORARY BACKUP LOCATION IF THEY EXIST
# -------------------------------------------------------------------------------------------------
if ([IO.Directory]::Exists($TEMP_BACKUP_PATH)) {
Remove-Item $TEMP_BACKUP_PATH -Recurse -Force
}
mkdir $TEMP_BACKUP_PATH | Out-Null
# -------------------------------------------------------------------------------------------------
# HOT COPY REPOSITORY TO TEMPORARY LOCATION
# -------------------------------------------------------------------------------------------------
Write-Output "Hot Copy $REPO_PATH"
& $SVNADMIN_EXE_PATH hotcopy $REPO_PATH $TEMP_BACKUP_PATH
if ($lastExitCode -ne 0) {
throw "Error: failed to 'svnadmin hotcopy' $srcDir to $destDir"
}
# -------------------------------------------------------------------------------------------------
# ARCHIVE BACKUP TO PERMINATE LOCATION
# -------------------------------------------------------------------------------------------------
$ARCHIVE_NAME = [string]::format("{0}.7z", (Get-Date -format yyyyMMddhhmm))
$ARCHIVE_PATH = [IO.Path]::Combine($PERM_BACKUP_PATH, $ARCHIVE_NAME)
& $7ZA_EXE_PATH a $ARCHIVE_PATH $TEMP_BACKUP_PATH\* -r
if ($LastExitCode -ne 0) {
throw "Error: failed to compress $TEMP_BACKUP_PATH\* using 7-zip"
}
# -------------------------------------------------------------------------------------------------
# REMOVE OLD ARCHIVES
# -------------------------------------------------------------------------------------------------
$DATE_LIMIT = (Get-Date).AddDays(-1 * $DAYS_TO_KEEP_BACKUPS)
$ARCHIVES = [IO.Path]::Combine($PERM_BACKUP_PATH, "*.7z")
# Delete files older than the $DATE_LIMIT.
Get-ChildItem -Path $ARCHIVES -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $DATE_LIMIT } | Remove-Item -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment