# This script was provided by a customer for backing up the KilnRepositories
# directory. It can be set up as a Windows Scheduled Task and run daily
# to copy the entire directory to a different location. It shuts down
# all appropriate services and processes before beginning the copy.
#
# NOTE: Newer versions of Kiln may use different services and processes,
# so make sure that the list is complete.
# Live values
$servicesToStop = "Apache Tomcat 6", "FogBugz Maintenance Service", "Kiln Queuing Service", "KilnStorageService", "W3SVC"
$processesToEnsureStopped = "backend", "FogBugzMaint", "QueueService", "redis-server", "tomcat6", "memcached"
$processTimeoutSeconds = 60
$backupLogLocation = "E:\Backup Scripts"
$liveRepoLocation = "D:\KilnRepositories"
$cloneRepoLocation = "E:\Kiln Backup"
$backupRepoLocation = "\\somewhere-on-network\Kiln Backup"
$smtpServer = "smtp.yourserver.com"
$fromEmail = "from@example.com"
$toEmail = "to@example.com"
# Start logging output, so we can send it in the email
Start-Transcript -Path "$backupLogLocation\BackupScript.txt"
# Stop all the services.
$servicesToStop | Stop-Service
# Give everything a little time to stop before we have to forcefully shut
# them down.
Start-Sleep -s $processTimeoutSeconds
# Kill any remaining processes, to ensure we can get a consistent snapshop.
# Necessary evil! :(
Stop-Process -Name $processesToEnsureStopped -ErrorAction SilentlyContinue
# Clone the repos to the other local drive (to minimise downtime, this is
# faster than copy to network!).
robocopy "$liveRepoLocation" "$cloneRepoLocation" /MIR /MT /W:3 /R:100 /LOG:"$backupLogLocation\BackupKiln.txt"
# Bring the services back up.
$servicesToStop | Start-Service
# Copy the repos over to the network location.
robocopy "$cloneRepoLocation" "$backupRepoLocation" /MIR /MT /W:3 /R:100 /LOG:"$backupLogLocation\CopyToNetwork.txt"
# Stop logging output, so we can embed it in the email.
Stop-Transcript
# Email the resulting logs.
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg = new-object Net.Mail.MailMessage
$msg.From = $fromEmail
$msg.To.Add($toEmail)
$msg.subject = "Kiln Automated Backup"
$msg.body = [string]::Join("`r`n",(Get-Content "$backupLogLocation\BackupScript.txt"))
$logfile1 = new-object Net.Mail.Attachment("$backupLogLocation\BackupKiln.txt")
$logfile2 = new-object Net.Mail.Attachment("$backupLogLocation\CopyToNetwork.txt")
$msg.Attachments.Add($logfile1)
$msg.Attachments.Add($logfile2)
$smtp.Send($msg)
|
Loading...