Monday, June 13, 2016

PowerShell Backup Script For VMware Server (Clone VMs using 'NEW-VM')

Backup Script Info /Surapong Naowasate v 1.0

Home path
=========
c:\vmware\

Folder:

- Script
- Hostlist
- Log-backup

File:
sc_xxx.ps1 - Script For backup
hl_xxx.txt - Host List For backup
VMware-Backup-xxx.log - Log Backup

Daily Backup info file path
===========================
c:\vmware\Script\Daily\sc_daily.ps1
c:\vmware\Hostlist\hl_daily.txt
c:\vmware\Log-backup\Daily\VMware-Backup-daily-$backup_date-$backup_timestamp

Weekly Backup info file path
===========================
c:\vmware\Script\Weekly\sc_weekly.ps1
c:\vmware\Hostlist\hl_weekly.txt
c:\vmware\Log-backup\Weekly\VMware-Backup-weekly-$backup_date-$backup_timestamp

Monthly Backup info file path

===========================
c:\vmware\Script\Monthly\sc_monthly.ps1
c:\vmware\Hostlist\hl_monthly.txt
c:\vmware\Log-backup\Monthly\VMware-Backup-Monthly-$backup_date-$backup_timestamp

EXP:Daily Backup Script


-------------------------------
Add-PSSnapin VMware.VimAutomation.Core
#Call VMware library

$VM = Get-Content c:\vmware\Hostlist\hl_daily.txt
#get hosting name for backup

$BackupvCenter = "vCenter.domain.local"
#set vCenter Name for connected to Backup

$BackupHost = "esxiHost.domain.local"
#Destination Host for Stored  backup VMs ( by clone VMs)

$backupDate = Get-Date -Format yyyyMMdd-HHmm
#set date format

$backupDatastore = "VMware-Backup-Storage"
#set DataStore for backup

Connect-VIServer -Server $BackupvCenter -User backupuser@domain.local -Password backupuserpassword
#Connet to vCenter

$VM_LOG = 'c:\vmware\Log-backup\Daily\VMware-Backup-daily-'+$backupDate+'.log'
#set log path and backup filename

foreach ($line in $VM) {

Get-VM -Server $BackupvCenter  | where { $_.PowerState -like 'PoweredOn' -and $_.name -like $line } | select name, vmhost, guestid, numcpu,memorymb | Out-File $VM_LOG -Append
#get information Guest VMs for backup

$VM_TMP = 'Backup-VMs-daily-'+$backupDate+'-'+$line
#set newname Guest VMs for backup

New-VM -Name $VM_TMP -VM $line -VMHost $BackupHost -Datastore $backupDatastore -Location Daily | Out-File $VM_LOG -Append
#clone VMs to backup Storage
 


$last7day = (get-date (get-date).AddDays(-7) -Format yyyyMMdd)
$VM_OLD_BACKUP = 'Backup-VMs-daily-'+$last7day+'-0000-'+$line
Remove-VM $VM_OLD_BACKUP -DeleteFromDisk -Confirm:$false
#remove VM from Datastore Last 7 day (dailybackup)

}
---------------------------------------


ADD to Windows TASK Schedule
============================
- Create Task
- Set name of  Schedule JOB
- Set Account for run backup schedule (DOMAIN\backupuser)
- Set Run whether user is logged on or not
- Create Trigger for job ex. daily, weekly, monthly
- Set Action for job ex. run powershell
    - start a program 'powershell'
    - add argument '-file c:\vmware\script\powershell_script'
    - start in directory 'c:\vmware\script'
- Click OK , Scheduled Task will asked for password of backup user (DOMAIN\backupuser)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

ALCATEL 6900

write memory copy running certified reload from working no rollback-timeout