Ubuntu Pastebin

Paste from mercuryfs at Sat, 21 May 2016 10:06:55 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
echo "hello world"

DATE=`date +%d%B%y`
XSNAME=`echo $HOSTNAME`

#create parent directory mnt1
mkdir -p /ZCGXenBackup

#set BACKUPPATH
BACKUPPATH=/ZCGXenBackup/$XSNAME/$DATE

#set UUID FILE location
UUIDFILE=/tmp/uuids.txt

echo "BACKUPPATH " + $BACKUPPATH
echo "DATE " + $DATE
echo "HOSTNAME " + $HOSTNAME

##mount fileserver NFS,CIFS,or External HDD
###############################################################
###############################################################
##Only need to change this part, whether its to nas or to external harddisk

#if mnt1 exist(not) then we quit the script, if mnt1 exist then we mount ext hdd to /mnt1
#if [ ! -d /mnt1 ]
if [ ! -d /ZCGXenBackup ]
then
        echo "No Mount point /ZCGXenBackup found, thus quiting backup now.."
        exit 0
else
        echo "Mount point /ZCGXenBackup found, mounting NFS or ext HDD to"
	echo "/ZCGXenBackup now.."
        mount -t ntfs-3g /dev/sdb1 /ZCGXenBackup
        ##NEED TO CHECK IF HARDDISK IS PHYSICALLY PLUGGED IN & is it correct /dev/sdb1 via dmesg
fi
#echo "end of IF mount point /ZCGXenBackup exist checking"

##Only need to change this part, whether its to nas or to external harddisk
##################################################################
##################################################################

##create backup location
mkdir -p $BACKUPPATH
echo "BACKUPPATH created"

##check if backup path if it does not exist, if  quit script
## -d $BACKUPPATH returns true if it exists
##[ ! -d $BACKUPPATH ] && echo "BACKUPPATH does not exist"; exit 0 || 
echo "BACKUPPATH exists, continuing.."

if [ ! -d $BACKUPPATH ]
then
        echo "BACKUPPATH does not exits, thus quiting backup now.."
echo "Please check backup location"; exit 0
else
echo "BACKUPPATH exists"
fi
#echo "end of IF BACKUPPATH checking"

##Fetch UUIDs, find VMs UUID
#xe vm-list is-control-domain=false is-a-snapshot=false
##first part is get the vms running, 2nd part get the uuid results only, 3rd part is cutting using the delimiter :
##returning only the uuid which is part 2 result of the cut as first part is uuid ( RO) and 2nd part is the uuid numbers
xe vm-list is-control-domain=false is-a-snapshot=false | grep uuid | cut -d":" -f2 > $UUIDFILE

##check if there is VM found on the xenserver
if [ ! -f $UUIDFILE ]
then
        echo "UUIDFILE does not exits, thus quiting backup now.."
        exit 0
else
        echo "UUID VMs exists"
fi
#echo "end of IF UUIDS VM exist checking"

##create VM snap shot
#xe vm-snapshot uuid=8ac95696-94f3-83c1-bc89-8bb2603f832b new-name-label=testvmsnapshot

##convert from snapshot to vm
#xe template-param-set is-a-template=false ha-always-run=false uuid=b15c0531-88a5-98a4-e484-01bc89131561

##export to destination
#xe vm-export vm=b15c0531-88a5-98a4-e484-01bc89131561 filename=vm-backup.xva

##remove the snapshot
#xe vm-uninstall uuid=b15c0531-88a5-98a4-e484-01bc89131561 force=true

##test if else
#echo "start of IF"
#testvar=1
#if [ "$testvar" = 2 ]
#then
#       echo "this is true"
#else
#       echo "this is not true, ELSE"
#fi
#echo "end of IF"

echo "begining while loop"

##since this is a while loop, it will first check if $UUIDFILE is 
##empty, if not empty then we go thur all the UUID inside
##the UUIDFILE variable and for each UUID we do the listing, 
##create snapshot, convert to vm, exporting the snapshot, then
##deleting the snapshot
while read VMUUID
do
    ##listing all the VMs UUID in the xenserver which will include the 
    ##HOST UUID and as well as snapshots, 2nd part or pipe is we only 
    ##grep the name-label of the vm
    ##3rd part or pipe is to do substitution with the returned 
    ##name-label string
    ##` inside there is to tell the system to treat it as a command` 
    ##once the command is executed and result is assigned to the 
    ##variable $VMNAME
    echo "doing VMNAME part"
echo $VMUUID
   VMNAME=`xe vm-list uuid=$VMUUID | grep name-label | cut -d":" -f2 | sed 's/^ *//g'`
echo $VMNAME

    ##setting the name for snapshot and creating a snapshot
    ##WE NEED TO MAKE SURE VM is RUNNING then this snapshot and export process will work
        echo "doing snapshot part"
        SNAPUUID=`xe vm-snapshot uuid=$VMUUID new-name-label="SNAPSHOT-$VMUUID-$DATE"`

    ##converting the snapshot to a VM so that this VM that is going to be exported can be 
    ##use for import to other xenserver next time
       echo "doing conversion part"
        xe template-param-set is-a-template=false ha-always-run=false uuid=$SNAPUUID

    ##exporting the VM to destination
        echo "doing export part"
        xe vm-export vm=$SNAPUUID filename="$BACKUPPATH/$VMNAME-$DATE.xva"

   ##removing the created snapshot
        echo "doing removing snapshot part"
        xe vm-uninstall uuid=$SNAPUUID force=true

done < $UUIDFILE
##end of do while loop

echo "end of script"
Download as text