Citrix Xenserver Script to Make Backup of VM's
System Administrators who want to do their Xenservers VM's backups automatically they can use this script.
Code starts
#!/bin/bash
VmName=$1
if [ ! -d "/mnt/vmshare" ]
then
mkdir /mnt/vmshare;
fi
mount -t cifs //sharename or IP address/VMBACKUP /mnt/vmshare -o username=username,password=password,domain=domain_name;
if [ `echo $?` -eq 0 ]
then
cd /mnt/vmshare;
xe vm-suspend vm=$VmName;
xe vm-export vm=$VmName filename=/mnt/vmshare/`echo -e $VmName`_`date +%d-%B-%Y`.xva compress=true preserve-power-state=true;
xe vm-resume vm=$VmName;
cd;
umount /mnt/vmshare;
fi
Code Ends.
Please save the code in filename.sh and provide the script exception permission
#chmod 755 filename.sh
Syntax to Execute the script
#filename.sh vmname
This script will suspend your VM temporary and make the backups of the VM's where it won't consumes space to make the VM's backup.
If you have sufficient space and want to make the VM's backup without suspending the VM's you can use the below script.
Code starts
#!/bin/bash
if [ ! -d "/mnt/vmshare" ]
then
mkdir /mnt/vmshare;
fi
mount -t cifs //sharename or IP address/VMBACKUP /mnt/vmshare -o username=username,password=password,domain=domain_name;
if [ `echo $?` -eq 0 ]
then
cd /mnt/vmshare;
uidsnap=$(xe vm-snapshot uuid=db99ad46-30ae-93d4-0699-9b60f84527f0 new-namelabel=vmtest-$date);
c="dc01.domain.com_$(date +%d-%B-%Y)".xva;
xe template-param-set is-a-template=false ha-always-run=false uuid=$uids nap;
xe vm-export vm=$uidsnap filename=/mnt/vmshare/$c compress=true;
cd /root
umount /mnt/vmshare;
xe vm-uninstall uuid=$uidsnap force=true;
fi
Code Ends.
Here to make the backup of the VM's first we need to find out the UUID of the VM.
# Xe vm-list is the command to find out the list of VM's running on Xenserver.
Copy the UUID of the desired VM you want to make the backup and use the UUID in the script.
You can also use this script to make an automatic schedule.
#crontab -l
List the current schedules
#crontab -e
Edit the scheduler to add your scripts in automatic backups.
minute hour day-of-month month day-of-week /bin/bash/ /scriptpath/scriptname.sh
Comments
Post a Comment