-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsync-lxd
executable file
·117 lines (90 loc) · 3.06 KB
/
rsync-lxd
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
#!/bin/bash
#
# sync install bs & install path to LXD containers
######################## GLOBAL FUNCTIONS
#S_TRACE=debug
S_GLOBAL_FUNCTIONS="${S_GLOBAL_FUNCTIONS:-/usr/local/bs/inc-functions}"
! . "${S_GLOBAL_FUNCTIONS}" && echo -e "[error] - Unable to source file '${S_GLOBAL_FUNCTIONS}' from '${BASH_SOURCE[0]}'" && exit 1
######################## VARIABLES
usage="rsync-lxd, sync files between host and LXD containers
rsync-lxd -h, --help
options:
-h, --help Print usage of this command
-v, --verbose Print details of files transfer
-p, --create-dirs Create any directories necessary
-r, --recursive Recursively transfer files
-d, --delete Delete files before pushing
--publish publish container fater sync
--bs sync only the path ${S_PATH_SCRIPT}
--install sync only the path ${S_PATH_INSTALL}
"
######################## FUNCTION
# check enabled configuration files
__push() {
_echod "${FUNCNAME}::${LINENO} IN \$@=$@"
local path path_to path_from path_tmp ct
# path
paths_sel="${paths_sel:-${S_PATH_SCRIPT}}"
# cts_sel
cts_sel="${cts_sel:-`lxc list -f csv -c n status=Running`}"
for path in ${paths_sel}; do
path_to="${path//${S_PATH_INSTALL}/\/usr/\local\/install}" # for desktop host
path="${path//-desktop/}" # for desktop host
path_from="/tmp/${path##*/}"
[ -d "${path_from}" ] && _eval rm -fR ${path_from}
_eval cp -a ${path} ${path_from}
[ -d "${path_from}/.git" ] && _eval rm -fR "${path_from}/.git"
for ct in ${cts_sel}; do
_echo "${path} -> ${ct}"
[ "${delete}" ] && _lxc_exec ${ct} "[ -d "${path_to}" ] && rm -fR ${path_to}"
_eval lxc file push ${args} ${path_from} $ct${path_to%/*}
done
done
}
__publish() {
_echod "${FUNCNAME}::${LINENO} IN \$@=$@"
local ct
for ct in ${cts_sel}; do
_echo "publish - ${ct}"
# rename
lxc image list -f csv -c l | grep -q ^${ct}$ && _eval lxc image alias rename ${ct} ${ct}-${_SDATE}
_eval lxc publish --force ${ct} --alias ${ct}
done
}
__opts() {
_echod "${FUNCNAME}::${LINENO} IN \$@=$@"
opts_short="hvrpd"
opts_long="help,verbose,recursive,create-dirs,delete,publish,bs,install"
opts=$(getopt -o ${opts_short} -l ${opts_long} -n "${0##*/}" -- "$@") || _exit 1
eval set -- ${opts}
_echod "${FUNCNAME}::${LINENO} opts=${*}"
args=" --quiet"
while [ "$1" != "--" ]; do
case "$1" in
-h|--help) echo "${usage}" && exit ;;
-v|--verbose) args="${args/ --quiet/}" ;;
-r|--recursive) args+=" --recursive" ;;
-p|--create-dirs) args+=" --create-dirs" ;;
-d|--delete) delete=y ;;
--publish) publish=y ;;
--bs) paths_sel+=" ${S_PATH_SCRIPT}" ;;
--install) paths_sel+=" ${S_PATH_INSTALL}" ;;
esac
shift
done
shift
cts_sel=$*
_echod "${FUNCNAME}::${LINENO} cts_sel=${cts_sel}"
}
__main() {
_echod "======================================================"
_echod "$(ps -o args= ${PPID})"
local opts_short opts_long opts
local paths_sel args delete cts_sel
! type lxc >/dev/null 2>&1 && _exite "LXC command not found !"
__opts "$@"
__push
[ "${publish}" ] && __publish
}
######################## MAIN
__main "$@"