-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpLxdct.sh
278 lines (231 loc) · 8.44 KB
/
expLxdct.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#! /usr/bin/env bash
##
## Export LXD Instances.
##
## By RungBa
## Created: 2021-10-18
## Revised: 2025-02-13
##
#
logFile=`basename $0 | cut -d. -f1`.log
## Show help
if [ -z "$1" ] || [ "$1" == "-h" ]; then
echo
echo " *** Hint: "
echo " 1. Export All: $0 -a"
echo " 2. Export 1 or many: $0 ct1 ct2 "
echo " 3. Show help: $0 -h"
echo
exit 1
fi
## *************************************************
## Block for Function define
## *************************************************
## Function for creating backup directory for specific instance
CreBakDir(){
local lxcName=$1
echo "Creating backup folder ..."
toPath=`pwd`/lxdct-bak/`date -I`-$lxcName
## Make sure the backup directory is existed
if [ ! -d "$toPath" ]; then
mkdir -p "$toPath" 2>&1 | tee -a $logFile
printf " -- Backup folder: $toPath ...\n" 2>&1 | tee -a $logFile
else
echo " -- Already Exists Backup Folder [$toPath]!!!" | tee -a $logFile
fi
}
## Function for checking instance existence
ChkInstName(){
# check lxc instance
local lxcName=$1
echo "Instance [$lxcName] Checking ... " | tee -a $logFile
lxcNameChk=`lxc ls | grep -w " $lxcName "`
if [ -z "$lxcNameChk" ]; then
echo " -- Error: Lxc instance [$lxcName] NOT found!!!" | tee -a $logFile
echo | tee -a $logFile
exit 1
else
echo " -- [$lxcName] is OK!" | tee -a $logFile
fi
}
## Function for checking instance is running or not
ChkInstStatus(){
## check lxc instance
local lxcName=$1
echo "Instance [$lxcName] Running Status Checking ... " | tee -a $logFile
## shutdown the instance if it is running
isRunning=`lxc ls | grep -w " $lxcName " | grep 'RUNNING'`
if [ -n "${isRunning}" ]; then
echo " -- The instance [$lxcName] is running!" | tee -a $logFile
echo " -- Shutting down [$lxcName] ... " | tee -a $logFile
lxc stop $lxcName 2>&1 | tee -a $logFile
isStoped="1"
echo " -- [$lxcName] is DOWN now!" | tee -a $logFile
else
echo " -- [$lxcName] is NOT running!" | tee -a $logFile
fi
}
## Function for Restoring Lxd Instances Running Status
restoreInstStatus(){
local lxcName=$1
## Bring back online
if [ ! -z "$isRunning" ]; then
echo "Bring the [$lxcName] back ONLINE ..." | tee -a $logFile
lxc start $lxcName 2>&1 | tee -a $logFile
echo "The [$lxcName] is back ONLINE. " | tee -a $logFile
else
echo "The [$lxcName] was NOT RUNNING so let it stay OFFLINE. " | tee -a $logFile
fi
## Log the datetime at end of this script
#echo
#echo "Done at" `date +"%Y-%m-%d %H:%M:%S"` | tee -a $logFile
#echo | tee -a $logFile
}
## Function for export profiles
expProfile(){
# check lxc instance
local lxcName=$1
local profileName=""
echo "Exporting all the profiles that [$lxcName] is using ... " | tee -a $logFile
for i in "${profileAllNames[@]}"
do
profileName=$i
#echo "Loop = "$i
isFound=""
#echo " -- Exporting Profile: $i" | tee -a $logFile
isFound=`lxc profile show $i | grep "/instances/$lxcName"`
#echo "isFound = "$isFound
if [ ! -z "$isFound" ]; then
if [ "$i" != "default" ]; then
profilesUsed[${#profilesUsed[@]}]="$profileName"
echo " -- Profile found = [$profileName]"
echo " -- Removing profile [$profileName] from [$lxcName] before exporting ... " | tee -a $logFile
lxc profile remove $lxcName $profileName 2>&1 | tee -a $logFile
echo " -- Done with Removing profile [$profileName] "
echo " -- Export profile [$profileName] for backup ..." | tee -a $logFile
lxc profile show $profileName > $toPath/$profileName.yaml 2>&1 | tee -a $logFile
echo " -- Done with Exporting profile [$profileName]"
#return
else
## We don't need to export the "default" profile. So just skip it.
echo " -- Found profile [$profileName]. We don't need to export it!!!"
fi
fi
done
}
## Function for adding profiles back to an Instance
## Ref: https://stackoverflow.com/questions/23605963/starting-for-loop-from-second-element-shell-script
##
addProfilesBack(){
local lxcName=$1
local profileName=""
echo "Add all the profiles back to [$lxcName] ... " | tee -a $logFile
#for i in "${profilesUsed[@]:1}" # start iterating from 2nd element
for i in "${profilesUsed[@]}" # start iterating from 1st element
do
#echo "Loop = "$i
profileName=$i
echo " -- ProfileName = [$profileName]" | tee -a $logFile
if [ $profileName == "default" ]; then
echo " -- Found profile [default], just skip adding it!!!"
continue
else
#echo
echo " -- Now Adding Profile [$profileName] back to [$lxcName] ... " | tee -a $logFile
# Add profile back to instance
lxc profile add $lxcName $profileName 2>&1 | tee -a $logFile
fi
done
echo " -- Done!" | tee -a $logFile
}
## Function for executing export
ExecExport(){
local lxcName=$1
#local hS2=" "
#local hS3=" "
#if [ ! -z "$profileName" ]; then
# echo "$hS2 Profile found = "$profileName
# echo "$hS2 => Temporarily removing profile $profileName from $lxcName before exporting ... " | tee -a $logFile
# lxc profile remove $lxcName $profileName 2>&1 | tee -a $logFile
# echo "$hS2 => Export profile: $profileName " | tee -a $logFile
# lxc profile show $profileName > $toPath/$profileName.yaml 2>&1 | tee -a $logFile
#fi
## Exporting the instance
echo "Now Exporting [$lxcName] to [$bakFile] " | tee -a $logFile
echo " -- Start at: "`date +"%Y-%m-%d %H:%M:%S"`" ..." | tee -a $logFile
lxc export $lxcName $toPath/$bakFile --compression bzip2 2>&1 | tee -a $logFile
#lxc export $lxcName $toPath/$bakFile --optimized-storage 2>&1 | tee -a $logFile
echo " -- Done at: "`date +"%Y-%m-%d %H:%M:%S"`"!!!" | tee -a $logFile
#date | tee -a $logFile
#echo " -- "`date +"%Y-%m-%d %H:%M:%S"` | tee -a $logFile
#echo | tee -a $logFile
}
## Function for export Instances
ExpLoop(){
local lxcName=""
for i in "${lxdInstances[@]}"
do
#echo "Loop = "$i
lxcName=$i
## Set the backup file name.
bakFile=$lxcName"_`date -I`.tar.bz2"
#echo " bakFile= $bakFile"
## Check the existence of a instance.
ChkInstName $lxcName
## 記錄 lxc container is running or not. Default is not running, ie isStoped="".
## Need to showdown the container if it is running.
ChkInstStatus $lxcName
## Create Backup directory. '$toPath' will get the value here.
CreBakDir $lxcName
## Set default instance status tag. '0':STOPPED, '1':RUNNING.
isStop="0"
## remove profiles before export
expProfile $lxcName
## Begin to export the instance.
ExecExport $lxcName
## add profiles back before start the instance
addProfilesBack $lxcName
## Restore Lxd Instances Running Status
restoreInstStatus $lxcName
## Log the datetime at end of this script
#echo
echo "Export Done at" `date +"%Y-%m-%d %H:%M:%S"` | tee -a $logFile
echo | tee -a $logFile
done
printf "Exporting Lxd instances complete!!!\n\n" | tee -a $logFile
}
## *************************************************
## Main Process - Block for variables define
## *************************************************
## Define Variables
bakFile=""
## Set default instance status tag. '':STOPPED, '1':RUNNING.
isRunning=""
##
declare -a profileAllNames=(`lxc profile ls -f csv | cut -d, -f1`)
## for keeping profile that is used by Instance
declare -a profilesUsed=()
## *************************************************
## Main Process
## *************************************************
if [ "$1" == "-a" ]; then
#echo " \$# = $#, Export All lxd containers."
## Export all Version 1.
#declare -a lxdInstances=(`lxc profile show default | grep "\/instances\/" | cut -d/ -f4`)
## Export all Version 2.
declare -a lxdInstances=(`lxc ls -c n --format csv`)
#echo $lxdInstances
else
declare -a lxdInstances=("$@")
#echo $lxdInstances
fi
echo | tee -a $logFile
echo "`date -I`: Begin to Export LXD Instances ---" | tee -a $logFile
echo | tee -a $logFile
#printf "\n*** `date -I` Begin to Export LXD instances\n" | tee -a $logFile
#printf "*** To folder: $toPath\n\n" | tee -a $logFile
## Export Lxd Instances
ExpLoop $lxdInstances
lxdInstances=""
profileAllNames=""
#echo | tee -a $logFile