53 lines
1.7 KiB
Text
53 lines
1.7 KiB
Text
#############################
|
||
#
|
||
# Backup Intranet
|
||
#
|
||
#############################
|
||
|
||
# Setting path for vboxmanage
|
||
$virtualBoxPath = "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"
|
||
|
||
# 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
|
||
}
|
||
|
||
# mounting backup path
|
||
New-SmbMapping -LocalPath "$backupDriveLetterMount" -RemotePath "$remoteBackupPath"
|
||
|
||
# backing up vm
|
||
vboxmanage controlvm poweroff
|
||
vboxmanage export -oovf10 -o "$backupFilename"
|
||
|
||
# remove old Backups
|
||
$backupFileCount = ( Get-ChildItem -Filter *.ova $remoteBackupPath | Measure-Object ).Count
|
||
$limitDate = (get-date).AddDays(-12).ToString("yyyMMdd")
|
||
$backupPath = $backupDriveLetterMount + "\"
|
||
|
||
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
|
||
}
|
||
}
|
||
}
|
||
|
||
# removing backup path
|
||
Remove-SmbMapping -RemotePath "$remoteBackupPath" -Force
|