stuffy_stuff/all

60 lines
1.9 KiB
Text
Raw Normal View History

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"
2018-11-29 19:34:44 +01:00
$backupFilename = $backupDriveLetterMount + ":\" + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + "_Intranet_Backup.ova"
2018-11-15 20:51:17 +01:00
$vmName = "2018_intranet"
2018-11-15 14:22:03 +01:00
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 ")
2018-11-29 19:34:44 +01:00
$vmanagerStopVM += @($vmName)
2018-11-15 20:51:17 +01:00
$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")
2018-11-29 19:34:44 +01:00
$vmanagerBackup += @(" -o ")
$vmanagerBackup += @([string]$backupFilename)
2018-11-15 20:51:17 +01:00
Write-host "$vmanagerBackup"
Invoke-Expression "$vmanagerBackup"
2018-11-15 14:22:03 +01:00
2018-11-15 20:51:17 +01:00
# start vm
$vmanagerStart = "VBoxManage.exe"
$vmanagerStart += @(" startvm ")
$vmanagerStart += @($vmName)
$vmanagerStart += @(" -type headless")
Write-host "$vmanagerStart"
Invoke-Expression "$vmanagerStart"
2018-11-29 19:34:44 +01:00
# remove old Backups
$backupFileCount = ( Get-ChildItem -Filter *.ova $remoteBackupPath | Measure-Object ).Count
$backupPath = $backupDriveLetterMount + ":\"
DO
{
$Item = Get-ChildItem -Path $remoteBackupPath -Filter *.ova | Sort CreationTime | select -First 1
Write-Host "Oldest file/folder in $FolderName is $($Item.FullName)"
Remove-Item -Path $($Item.FullName) -Force
$backupFileCount = ( Get-ChildItem -Filter *.ova $remoteBackupPath | Measure-Object ).Count
} While ($backupFileCount -gt 3)
2018-11-15 14:22:03 +01:00
# removing backup path
2018-11-29 19:34:44 +01:00
Remove-PSDrive -Name $backupDriveLetterMount