|
####--------------------------------------------------------------------------------
|
|
#### SVN Repository Backup
|
|
#### Author: James Coffman
|
|
#### Date Created: 6/16/2009
|
|
#### -------------------------------------------------------------------------------
|
|
#### Updates:
|
|
#### 2009/06/23 - modified zip function and added wait at the end to help make sure
|
|
#### compression has time to complete.
|
|
#### -------------------------------------------------------------------------------
|
|
#### Disclaimer: !!!! Use at your own risk. !!!!
|
|
####
|
|
#### This Script will use svnadmin to hotcopy every repository in your
|
|
#### repositories directory. For example I have all of my repositories
|
|
#### in the directory ..\SVN\Repositories. This script assumes each directory
|
|
#### under the main directory is a repository and will attempt to back it up.
|
|
#### It will zip the backedup dir and copy it to a directory named after the repository
|
|
#### under the backup path. If the directory doesnt exist will be created for you.
|
|
#### If you set the DaysToKeepBackups variable to something > 0 then this script
|
|
#### will delete the backup zipfiles older that the # of days you set.
|
|
#### 1) You need to have svnadmin.exe accessible.
|
|
#### 2) Set your paths in the global vars below
|
|
####--------------------------------------------------------------------------------
|
|
|
|
|
|
# Set up Global variables needed by script
|
|
$mySqlDatabaseName = "databasename"
|
|
$mySqlUser = "user"
|
|
$mySqlPassword = "password"
|
|
$redmineFileFolderPath = "D:\Program Files\Redmine\files"
|
|
$backupPath = "d:\dump"
|
|
$mySqlDumpExe = "D:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
|
|
$DaysToKeepBackups = 60
|
|
$sz = "$env:ProgramFiles\7-Zip\7z.exe"
|
|
|
|
#*************************************************************************
|
|
# F U N C T I O N S
|
|
#*************************************************************************
|
|
|
|
#------------------------------------------------------------------------#
|
|
# Create a new temp Directory
|
|
#------------------------------------------------------------------------#
|
|
function CreateTempDir ([string]$name)
|
|
{
|
|
#create a dir in the system temp dir for svn to copy the repository into
|
|
#before zipping it
|
|
|
|
$newDir = "Not Found"
|
|
|
|
$tempCopyPath = [System.IO.Path]::GetTempPath().ToString() + $name
|
|
|
|
#delete it first if it exists
|
|
if ( [System.IO.Directory]::Exists($tempCopyPath) )
|
|
{
|
|
Remove-Item -path $tempCopyPath -recurse -force
|
|
##[System.IO.Directory]::Delete($repoTempCopyPath, $true)
|
|
}
|
|
|
|
#then create it
|
|
if( ![System.IO.Directory]::Exists($tempCopyPath))
|
|
{
|
|
$newDir = [System.IO.Directory]::CreateDirectory($tempCopyPath)
|
|
}
|
|
|
|
$newDir.FullName
|
|
}
|
|
|
|
#------------------------------------------------------------------------#
|
|
# Run SVN Backup
|
|
#------------------------------------------------------------------------#
|
|
function RunMySqlDump ([string]$user, [string]$password, [string]$databaseName, [string]$sqlName)
|
|
{
|
|
$currentOutputEncoding = [Console]::OutputEncoding
|
|
[Console]::OutputEncoding = [Text.Encoding]::UTF8
|
|
|
|
& $mySqlDumpExe --user=$user --password=$password $databaseName | Out-File "$sqlName" -encoding UTF8
|
|
|
|
[Console]::OutputEncoding = $currentOutputEncoding
|
|
}
|
|
|
|
#------------------------------------------------------------------------#
|
|
# Zip a whole Directory
|
|
#------------------------------------------------------------------------#
|
|
function ZipDir ([string]$_dirToZip, [object]$_zipName)
|
|
{
|
|
#add zip extension if not present
|
|
if (-not $_zipName.EndsWith(".zip")) {$_zipName += ".zip"}
|
|
|
|
#make sure directory to zip exists
|
|
if (test-path $_dirToZip)
|
|
{
|
|
#make sure zip file doesnt already exist
|
|
if (-not (test-path $_zipName))
|
|
{
|
|
& $sz a -tzip "$_zipName" "$_dirToZip"
|
|
}
|
|
}
|
|
else
|
|
{
|
|
"ERROR: Path does not exist -- $_dirToZip"
|
|
}
|
|
}
|
|
|
|
#------------------------------------------------------------------------#
|
|
# Clean Backup Dirs
|
|
#------------------------------------------------------------------------#
|
|
function CleanBackupDirs ([string]$backupPathtoClean, [int]$daysToKeep)
|
|
{
|
|
" ... cleaning $backupPathtoClean"
|
|
foreach ($backupFile in Get-ChildItem -Path $backupPathtoClean)
|
|
{
|
|
if ( Test-Path $backupFile.FullName )
|
|
{
|
|
$y = ((Get-Date) - $backupFile.CreationTime).Days
|
|
if ($y -gt $daysToKeep -and $backupFile.PsISContainer -ne $True)
|
|
{
|
|
" ...... Deleting $backupFile : " + $backupFile.CreationTime
|
|
$backupFile.Delete()
|
|
}
|
|
else
|
|
{
|
|
" ...... Keeping $backupFile : " + $backupFile.CreationTime
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#*************************************************************************
|
|
# MAIN LOGIC BEGINS HERE
|
|
#*************************************************************************
|
|
|
|
|
|
if (-not (test-path "$sz"))
|
|
{
|
|
"7z est manquant"
|
|
exit(-1)
|
|
}
|
|
|
|
|
|
"*******************************************************"
|
|
"* BEGIN: Backing Up Redmine"
|
|
"*******************************************************"
|
|
"* Params: "
|
|
"* The Database Name is: $mySqlDatabaseName"
|
|
"* The Redmine File Folder is: $redmineFileFolderPath"
|
|
"* The Backup Path is: $backupPath"
|
|
$temp = [System.IO.Path]::GetTempPath().ToString()
|
|
"* The Temp Directory is: $temp"
|
|
"* "
|
|
"*------------------------------------------------------"
|
|
|
|
""
|
|
" Processing backup for: $mySqlDatabase"
|
|
$tempDir = ""
|
|
" ... Creating Temp Directory"
|
|
$tempDir = CreateTempDir $mySqlDatabaseName
|
|
" ... Running MySqlDump"
|
|
$tempFileName = $tempDir + "\" + $mySqlDatabaseName + ".sql"
|
|
RunMySqlDump $mySqlUser $mySqlPassword $mySqlDatabaseName $tempFileName
|
|
|
|
" ... Copying Redmine files"
|
|
Copy-Item $redmineFileFolderPath $tempDir -recurse
|
|
|
|
|
|
$newBackupPath = ""
|
|
# Create a Folder in the backup path for each repository if needed
|
|
if($backupPath.EndsWith("\"))
|
|
{
|
|
$newBackupPath = $backupPath + $mySqlDatabaseName
|
|
}
|
|
else
|
|
{
|
|
$newBackupPath = $backupPath + "\" + $mySqlDatabaseName
|
|
}
|
|
if( ![System.IO.Directory]::Exists($newBackupPath))
|
|
{
|
|
" ... Creating Backup Directory $newBackupPath"
|
|
[System.IO.Directory]::CreateDirectory($newBackupPath)
|
|
}
|
|
if(!$newBackupPath.EndsWith("\")){$newBackupPath = $newBackupPath + "\"}
|
|
|
|
# Zip the the backup into a zip file with datetime stamp
|
|
$timeStamp = Get-Date -uformat "%Y_%m_%d_%H%M%S"
|
|
$zipNamePath = $newBackupPath + $mySqlDatabaseName + "_" + $timeStamp + ".zip"
|
|
" ... Zipping Repository Backup to $zipNamePath"
|
|
ZipDir $tempDir $zipNamePath
|
|
|
|
# next step is to clean old zipped backups
|
|
if ($DaysToKeepBackups -gt 0)
|
|
{
|
|
CleanBackupDirs $newBackupPath $DaysToKeepBackups
|
|
}
|
|
|
|
|
|
|
|
"*******************************************************"
|
|
"* END: Redmine Archived"
|
|
"*******************************************************"
|
|
|
|
Exit(0)
|