Update 'Virtual_box_backup'
This commit is contained in:
parent
b294b4530a
commit
0d16e72fde
2 changed files with 137 additions and 59 deletions
137
Virtual_box_backup
Normal file
137
Virtual_box_backup
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
#############################
|
||||||
|
#
|
||||||
|
# Backup Intranet
|
||||||
|
#
|
||||||
|
#############################
|
||||||
|
|
||||||
|
# Setting path for vboxmanage
|
||||||
|
$Env:Path = "c:\Program Files\Oracle\VirtualBox\"
|
||||||
|
|
||||||
|
# creating backup paths
|
||||||
|
$remoteBackupPath = "\\freenas\programas\VM_Backup"
|
||||||
|
$backupDriveLetterMount = "L"
|
||||||
|
$backupFilename = $backupDriveLetterMount + ":\" + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + "_Intranet_Backup.ova"
|
||||||
|
$vmName = "2018_intranet"
|
||||||
|
$logFile = "c:\logs\vm_backup.log"
|
||||||
|
|
||||||
|
"################ Starting Intranet Backup at $Time ######################" | out-file $logFile -append
|
||||||
|
|
||||||
|
# mounting backup path New-PSDrive -Name L -Root \\freenas\programas\VM_Backup\ -Persist -PSProvider FileSystem
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
New-PSDrive -Name $backupDriveLetterMount -Root $remoteBackupPath -Persist -PSProvider FileSystem
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Success] Mounting Backup Path at $Time" | out-file $logFile -append
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Error] Mounting Backup Path at $Time" | out-file $logFile -append
|
||||||
|
$_.Exception.Message | out-file $logFile -append
|
||||||
|
$_.Exception.ItemName | out-file $logFile -append
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
|
||||||
|
# Stopping VM
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
$vmanagerStopVM = "VBoxManage.exe"
|
||||||
|
$vmanagerStopVM += @(" controlvm ")
|
||||||
|
$vmanagerStopVM += @($vmName)
|
||||||
|
$vmanagerStopVM += @(" poweroff")
|
||||||
|
Write-host "$vmanagerStopVM"
|
||||||
|
Invoke-Expression "$vmanagerStopVM"
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Success] Stop VM at $Time" | out-file $logFile -append
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Error] to Stop VM at $Time" | out-file $logFile -append
|
||||||
|
$_.Exception.Message | out-file $logFile -append
|
||||||
|
$_.Exception.ItemName | out-file $logFile -append
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
|
||||||
|
# Backing Up VM
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
$vmanagerBackup = "VBoxManage.exe"
|
||||||
|
$vmanagerBackup += @(" export ")
|
||||||
|
$vmanagerBackup += @($vmName)
|
||||||
|
$vmanagerBackup += @(" --ovf10")
|
||||||
|
$vmanagerBackup += @(" -o ")
|
||||||
|
$vmanagerBackup += @([string]$backupFilename)
|
||||||
|
Write-host "$vmanagerBackup"
|
||||||
|
Invoke-Expression "$vmanagerBackup"
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Success] Backing Up VM at $Time" | out-file $logFile -append
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Error] Backing Up VM at $Time" | out-file $logFile -append
|
||||||
|
$_.Exception.Message | out-file $logFile -append
|
||||||
|
$_.Exception.ItemName | out-file $logFile -append
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
|
||||||
|
# Start VM after Backup
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
$vmanagerStart = "VBoxManage.exe"
|
||||||
|
$vmanagerStart += @(" startvm ")
|
||||||
|
$vmanagerStart += @($vmName)
|
||||||
|
$vmanagerStart += @(" -type headless")
|
||||||
|
Write-host "$vmanagerStart"
|
||||||
|
Invoke-Expression "$vmanagerStart"
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Success] Starting VM at $Time" | out-file $logFile -append
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Error] Starting VM at $Time" | out-file $logFile -append
|
||||||
|
$_.Exception.Message | out-file $logFile -append
|
||||||
|
$_.Exception.ItemName | out-file $logFile -append
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
# 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)"
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
Remove-Item -Path $($Item.FullName) -Force
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Success] Deleting old backup at $Time" | out-file $logFile -append
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Error] Deleting old backup at $Time" | out-file $logFile -append
|
||||||
|
$_.Exception.Message | out-file $logFile -append
|
||||||
|
$_.Exception.ItemName | out-file $logFile -append
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
$backupFileCount = ( Get-ChildItem -Filter *.ova $remoteBackupPath | Measure-Object ).Count
|
||||||
|
} While ($backupFileCount -gt 3)
|
||||||
|
|
||||||
|
# removing Network drive
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
Remove-PSDrive -Name $backupDriveLetterMount
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Time=Get-Date
|
||||||
|
"[Error] Umounting Network drive at $Time" | out-file $logFile -append
|
||||||
|
$_.Exception.Message | out-file $logFile -append
|
||||||
|
$_.Exception.ItemName | out-file $logFile -append
|
||||||
|
Break
|
||||||
|
}
|
||||||
|
|
59
all
59
all
|
@ -1,59 +0,0 @@
|
||||||
#############################
|
|
||||||
#
|
|
||||||
# Backup Intranet
|
|
||||||
#
|
|
||||||
#############################
|
|
||||||
|
|
||||||
# Setting path for vboxmanage
|
|
||||||
$Env:Path = "c:\Program Files\Oracle\VirtualBox\"
|
|
||||||
|
|
||||||
# creating backup paths
|
|
||||||
$remoteBackupPath = "\\freenas\programas\VM_Backup"
|
|
||||||
$backupDriveLetterMount = "L"
|
|
||||||
$backupFilename = $backupDriveLetterMount + ":\" + [string](Get-Date).Year + [string](Get-Date).Month + [string](Get-Date).Day + "_Intranet_Backup.ova"
|
|
||||||
$vmName = "2018_intranet"
|
|
||||||
|
|
||||||
# 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"
|
|
||||||
|
|
||||||
#backing up vm
|
|
||||||
$vmanagerBackup = "VBoxManage.exe"
|
|
||||||
$vmanagerBackup += @(" export ")
|
|
||||||
$vmanagerBackup += @($vmName)
|
|
||||||
$vmanagerBackup += @(" --ovf10")
|
|
||||||
$vmanagerBackup += @(" -o ")
|
|
||||||
$vmanagerBackup += @([string]$backupFilename)
|
|
||||||
Write-host "$vmanagerBackup"
|
|
||||||
Invoke-Expression "$vmanagerBackup"
|
|
||||||
|
|
||||||
# start vm
|
|
||||||
$vmanagerStart = "VBoxManage.exe"
|
|
||||||
$vmanagerStart += @(" startvm ")
|
|
||||||
$vmanagerStart += @($vmName)
|
|
||||||
$vmanagerStart += @(" -type headless")
|
|
||||||
Write-host "$vmanagerStart"
|
|
||||||
Invoke-Expression "$vmanagerStart"
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
# removing backup path
|
|
||||||
Remove-PSDrive -Name $backupDriveLetterMount
|
|
Loading…
Reference in a new issue