-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlxd-backup
executable file
·138 lines (110 loc) · 3.04 KB
/
lxd-backup
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
#!/bin/bash
set -e
command -v incus >/dev/null 2>&1 && BINARY=incus ||
{
command -v lxd >/dev/null 2>&1 && BINARY=lxc ||
{
echo "did not find neither incus nor lxd binary in path." >&2;
exit 1;
}
}
function log {
echo "$(date '+%Y-%m-%d %H:%M:%S') | $1"
}
function do_import {
ARGNUM=2
if [ $# -lt $(($ARGNUM-1)) ] || [ $# -gt $ARGNUM ]; then
echo 1>&2 "$0 import {imagePath} {containerName}"
exit 2
fi
IMPORTPATH=$1
IMPORTFILE=$(basename -- "$IMPORTPATH")
DEFAULTCONTAINER=${IMPORTFILE%%.*}
CONTAINER=${2:-$DEFAULTCONTAINER}
HASH=$(echo $CONTAINER | shasum | cut -b 1-40)
IMAGE="$HASH-import"
log "importing template $IMPORTPATH as container $CONTAINER"
log "importing image file"
$BINARY image delete $IMAGE 2>/dev/null || true
$BINARY image import $IMPORTPATH --alias=$IMAGE >/dev/null
log "creating container"
$BINARY init $IMAGE $CONTAINER >/dev/null || true
log "cleaning up"
$BINARY image delete $IMAGE 2> /dev/null
log "done."
}
function do_export {
ARGNUM=2
if [ $# -lt $ARGNUM ] || [ $# -gt $ARGNUM ]; then
echo 1>&2 "$0 export {containerName} {exportPath without .tar.gz}"
exit 2
fi
if $BINARY info $1 2>/dev/null | egrep -iqc "status: running"; then
ISRUNNING=true
else
ISRUNNING=false
fi
CONTAINER=$1
EXPORTPATH=${2%/}
if [[ -d $EXPORTPATH ]]; then
EXPORTPATH+="/$CONTAINER"
fi
HASH=$(echo $CONTAINER | shasum | cut -b 1-40)
IMAGE="$HASH-export"
SNAPSHOT="$HASH-export"
touch $EXPORTPATH.tmp
rm $EXPORTPATH.tmp
log "exporting container $CONTAINER to $EXPORTPATH.tar.gz"
log "preparing snapshot"
$BINARY delete $CONTAINER/$SNAPSHOT 2>/dev/null || true
$BINARY image delete $IMAGE 2>/dev/null || true
if [ "$ISRUNNING" = true ] ; then
log "stopping container"
$BINARY stop $CONTAINER 2>/dev/null
fi
log "creating snapshot"
$BINARY snapshot $CONTAINER $SNAPSHOT >/dev/null
if [ "$ISRUNNING" = true ] ; then
log "starting container"
$BINARY start $CONTAINER
fi
log "creating image"
$BINARY publish $CONTAINER/$SNAPSHOT --alias=$IMAGE description="Exported from $CONTAINER at $(date '+%Y-%m-%d %H:%M')" >/dev/null
log "exporting image"
$BINARY image export $IMAGE $EXPORTPATH >/dev/null
log "cleaning up"
$BINARY delete $CONTAINER/$SNAPSHOT
$BINARY image delete $IMAGE
log "done."
}
function do_clean {
ARGNUM=1
if [ $# -lt $ARGNUM ] || [ $# -gt $ARGNUM ]; then
echo 1>&2 "$0 clean {containerName}"
exit 2
fi
CONTAINER=$1
EXPORTPATH=$2
HASH=$(echo $CONTAINER | shasum | cut -b 1-40)
IMAGE="$HASH-export"
log "Cleaning backup data for container $CONTAINER"
log "Removing container snapshot.."
$BINARY delete $CONTAINER/$IMAGE 2>/dev/null || true
log "Removing image.."
$BINARY image delete $IMAGE 2>/dev/null || true
log "done."
}
case $1 in
"clean")
do_clean $2
;;
"export")
do_export $2 $3
;;
"import")
do_import $2 $3
;;
*)
echo "Wrong Parameters! Usage: [ export | import | clean | help ]"
;;
esac