2018-11-15 14:22:03 +01:00
|
|
|
|
#############################
|
|
|
|
|
#
|
|
|
|
|
# Backup Intranet
|
|
|
|
|
#
|
|
|
|
|
#############################
|
|
|
|
|
|
|
|
|
|
# Setting path for vboxmanage
|
2018-11-15 20:51:17 +01:00
|
|
|
|
$Env:Path = "c:\Program Files\Oracle\VirtualBox\"
|
2018-11-15 14:22:03 +01:00
|
|
|
|
|
|
|
|
|
# creating backup paths
|
2018-11-15 20:51:17 +01:00
|
|
|
|
$remoteBackupPath = "\\freenas\programas\VM_Backup"
|
|
|
|
|
$backupDriveLetterMount = "L"
|
|
|
|
|
$backupFilename = " -o " + $backupDriveLetterMount + ":\" + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + "_Intranet_Backup.ova"
|
|
|
|
|
$vmName = "2018_intranet"
|
2018-11-15 14:22:03 +01:00
|
|
|
|
# setting path var to VirtualBox folder
|
|
|
|
|
$Reg = "Registry::HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
|
|
|
|
|
$regex = "[cC]\:\\Program Files\\Oracle\\VirtualBox"
|
|
|
|
|
$OldPath = (Get-ItemProperty -Path "$Reg" -Name PATH).Path
|
|
|
|
|
|
|
|
|
|
# check if PATH is already set
|
|
|
|
|
if ( -Not ($OldPath -match $regex)){
|
|
|
|
|
Write-host "`nSetting Path Variable to $AddedLocation"
|
|
|
|
|
Set-ItemProperty -Path "$Reg" -Name PATH -Value $virtualBoxPath
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 20:51:17 +01:00
|
|
|
|
# mounting backup path New-PSDrive -Name L -Root \\freenas\programas\VM_Backup\ -Persist -PSProvider FileSystem
|
|
|
|
|
New-PSDrive -Name $backupDriveLetterMount -Root $remoteBackupPath -Persist -PSProvider FileSystem
|
|
|
|
|
|
|
|
|
|
# stoping vm
|
|
|
|
|
$vmanagerStopVM = "VBoxManage.exe"
|
|
|
|
|
$vmanagerStopVM += @(" controlvm ")
|
|
|
|
|
$vmanagerStopVM += @(@vmName)
|
|
|
|
|
$vmanagerStopVM += @(" poweroff")
|
|
|
|
|
Write-host "$vmanagerStopVM"
|
|
|
|
|
Invoke-Expression "$vmanagerStopVM"
|
2018-11-15 14:22:03 +01:00
|
|
|
|
|
2018-11-15 20:51:17 +01:00
|
|
|
|
#backing up vm
|
|
|
|
|
$vmanagerBackup = "VBoxManage.exe"
|
|
|
|
|
$vmanagerBackup += @(" export ")
|
|
|
|
|
$vmanagerBackup += @($vmName)
|
|
|
|
|
$vmanagerBackup += @(" --ovf10")
|
|
|
|
|
$vmanagerBackup += @([string]$backupFilename)
|
|
|
|
|
Write-host "$vmanagerBackup"
|
|
|
|
|
Invoke-Expression "$vmanagerBackup"
|
2018-11-15 14:22:03 +01:00
|
|
|
|
|
|
|
|
|
# remove old Backups
|
|
|
|
|
$backupFileCount = ( Get-ChildItem -Filter *.ova $remoteBackupPath | Measure-Object ).Count
|
|
|
|
|
$limitDate = (get-date).AddDays(-12).ToString("yyyMMdd")
|
2018-11-15 20:51:17 +01:00
|
|
|
|
$backupPath = $backupDriveLetterMount + ":\"
|
|
|
|
|
|
|
|
|
|
# start vm
|
|
|
|
|
$vmanagerStart = "VBoxManage.exe"
|
|
|
|
|
$vmanagerStart += @(" startvm ")
|
|
|
|
|
$vmanagerStart += @($vmName)
|
|
|
|
|
$vmanagerStart += @(" -type headless")
|
|
|
|
|
Write-host "$vmanagerStart"
|
|
|
|
|
Invoke-Expression "$vmanagerStart"
|
|
|
|
|
|
|
|
|
|
#if ($backupFileCount -gt 3) {
|
|
|
|
|
# Get-ChildItem $backupPath |
|
|
|
|
|
# ForEach-Object {
|
|
|
|
|
# # get actual
|
|
|
|
|
# $lasWriteTime = (Get-Content $_.LastWriteTime).ToString("yyMMdd")
|
|
|
|
|
#
|
|
|
|
|
# # delete backup files older than 12 days
|
|
|
|
|
# if ($lasWriteTime -lt $limitDate ) {
|
|
|
|
|
# $fileNameFull = Get-Content $_.FullName
|
|
|
|
|
# Remove-Item –path $fileNameFull
|
|
|
|
|
# }
|
|
|
|
|
# }
|
|
|
|
|
# }
|
2018-11-15 14:22:03 +01:00
|
|
|
|
|
|
|
|
|
# removing backup path
|
2018-11-15 20:51:17 +01:00
|
|
|
|
Remove-PSDrive -Name $backupDriveLetterMount
|