-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrestore-original.sh
executable file
·159 lines (139 loc) · 4.95 KB
/
restore-original.sh
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# Module : restore-original.sh
# Author : Sriharsha B S, [email protected]
# Date : 13th August 2018
# Description : BASH form of Restore-AzureRmOriginalVM powershell command.
help="\n
========================================================================================\n
restore-original.sh --> BASH form of Restore-AzureRmOriginalVM powershell command\n
========================================================================================\n\n\n
========================================================================================\n
Disclaimer\n
========================================================================================\n\n
Do not use this script on an Encrypted VM. This script does not store the encrypted settings.
Running this on an encrypted VM may render your VM potentially useless\n\n\n
========================================================================================\n
Description\n
========================================================================================\n\n
You may run this script once the troubleshooting of the OS Disk is performed on a Rescue VM.\n
This Script Performs the following operation :\n
1. Detach the Attached OS Disk to the Rescue VM\n
2. Stop and Deallocate the Problematic Original VM\n
3. Perform OS Disk Swap with Detached OS Disk with the Problematic VM\n
4. Start the Fixed (Problematic) Original VM with the swapped OS Disk\n\n\n
=========================================================================================\n
Arguments and Usage\n
=========================================================================================\n\n
All the arguments are mandatory. However, arguments may be passed in any order\n
1. --rescue-vm-name : Name of the Rescue VM Name\n
2. --rescue-resource-group : Rescue VM's resource group\n
3. -g or --resource-group : Problematic Original VM's Resource Group\n
4. -n or --name : Problematic Original VM\n
5. --os-disk : Name of the fixed OS Disk which is currently attached to the Rescue VM and is to be swapped with the Original VM.\n
6. -s or --subscription : Subscription Id where the respective resources are present.\n\n
Usage Example: ./restore-original.sh -s xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --rescue-vm-name Ubuntu16042 --rescue-resource-group Redhat -g debian -n debian9 --os-disk myvm-os-disk \n\n\n
"
POSITIONAL=()
#echo $# for debugging purpose only
if [[ $# -ne 12 ]]
then
echo -e $help
exit;
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-g|--resource-group)
g="$2"
shift # past argument
shift # past value
;;
-n|--name)
vm="$2"
shift # past argument
shift # past value
;;
-s|--subscription)
subscription="$2"
shift # past argument
shift # past value
;;
--rescue-resource-group)
rg="$2"
shift # past argument
shift # past value
;;
--rescue-vm-name)
rn="$2"
shift # past argument
shift
;;
--os-disk)
osdisk="$2"
shift # past argument
shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
echo $rg
echo $rn
echo $subscription
echo $g
echo $vm
# Check whether user has an azure account
acc=$(az account show)
echo $acc
if [[ -z $acc ]]
then
echo "Please login using az login command"
exit;
fi
# Check if user has a valid azure subscription. If yes, select the subscription as the default subscription
subvalid=$(az account list | jq ".[].id" | grep -i $subscription)
if [[ $(echo "${subvalid//\"}") != "$subscription" || -z $subvalid ]]
then
echo "No Subscription $subscription exists"
exit;
fi
az account set --subscription $subscription
# get the OS disk uri for the problematic os disk from the Rescue VM which is currently attached
datadisks=$(az vm show -g $rg -n $rn | jq ".storageProfile.dataDisks")
managed=$(echo $datadisks | jq ".[0].managedDisk")
#echo $managed
disk_uri="null"
if [[ $managed = "null" ]]
then
disk_uri=$(echo $datadisks | jq ".[].vhd.uri" | grep -i $osdisk)
else
disk_uri=$(echo $datadisks | jq ".[].managedDisk.id" | grep -i $osdisk)
fi
if [[ -z disk_uri ]]
then
echo "The rescue VM does not contain the Problematic OS disk"
exit;
fi
# Detach the Problematic OS disk from the Rescue VM
echo "Detaching the OS disk from the rescue VM"
az vm disk detach -g $rg --vm-name $rn -n $osdisk
# OS Disk Swap Procedure.
echo "Preparing for OS disk swap"
# Stop the Problematic VM
echo "Stopping and deallocating the Problematic Original VM"
az vm deallocate -g $g -n $vm
# Perform the disk swap and verify
echo "Performing the OS disk Swap"
swap=$(az vm update -g $g -n $vm --os-disk $(echo "${disk_uri//\"}") | jq ".storageProfile.osDisk.name")
if [[ $(echo "${swap//\"}") != "$osdisk" ]]
then
echo "Problem with Disk Swapping"
exit;
fi
echo "Successfully swapped the OS disk. Now starting the Problematic VM with OS disk $swap"
# Start the Fixed VM after disk swap
az vm start -g $g -n $vm
echo "Start of the VM $vm Successful"