-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.sh
164 lines (146 loc) · 5.67 KB
/
run.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
#!/usr/bin/env bashio
set +u
export BORG_BASE_DIR=/config/borg
export BORG_CACHE_DIR=${BORG_BASE_DIR}/cache
export BORG_PASSPHRASE=$(bashio::config 'borg_passphrase')
export BORG_REPO=""
export _BORG_TOBACKUP=/backup/borg_unpacked
export _BORG_SSH_KNOWN_HOSTS=${BORG_BASE_DIR}/known_hosts
export _BORG_SSH_KEY=${BORG_BASE_DIR}/keys/borg_backup
export _BORG_REPO_URL=$(bashio::config 'borg_repo_url')
export _BORG_USER=$(bashio::config 'borg_user')
export _BORG_HOST=$(bashio::config 'borg_host')
export _BORG_REPONAME=$(bashio::config 'borg_reponame')
export _BORG_COMPRESSION=$(bashio::config 'borg_compression')
export _BORG_BACKUP_DEBUG="$(bashio::config 'borg_backup_debug')"
export _BORG_BACKUP_KEEP_SNAPSHOTS="$(bashio::config 'borg_backup_keep_snapshots')"
export _BORG_DEBUG=''
export borg_error=0
export BORG_RSH="ssh -o UserKnownHostsFile=${_BORG_SSH_KNOWN_HOSTS} -i ${_BORG_SSH_KEY} $(bashio::config 'borg_ssh_params')"
mkdir -p $(dirname ${_BORG_SSH_KEY}) ${BORG_CACHE_DIR}
##### passwords crap
if [ ${#BORG_PASSPHRASE} -eq 0 ];then
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
unset BORG_PASSPHRASE
fi
# set zstd as default compression
if [ ${#_BORG_COMPRESSION} -eq 0 ];then
_BORG_COMPRESSION="zstd"
fi
if [ ${#BORG_BACKUP_DEBUG} -ne 0 ];then
_BORG_DEBUG="--debug"
fi
function sanity_checks {
if [[ ( ${#_BORG_REPO_URL} -eq 0 ) && ( ${#_BORG_HOST} -eq 0 )]];then
bashio::log.error "both 'borg_repo_url' 'borg_host' undefined"
bashio::log.error "please define one of them"
borg_error=$(($borg_error + 1))
elif [[ ( ${#_BORG_REPO_URL} -gt 0 ) && ( ${#_BORG_HOST} -gt 0 )]];then
bashio::log.error "'borg_repo_url' and 'borg_host' are definded"
bashio::log.error "please define only one of them"
borg_error=$(($borg_error + 1))
else
bashio::log.info "sanity preserved"
fi
}
function set_borg_repo_path {
bashio::log.debug "Setting BORG_REPO"
if [ ${#_BORG_REPO_URL} -gt 0 ]; then
BORG_REPO=${_BORG_REPO_URL}
bashio::log.debug "BORG_REPO set"
return
elif [ ${#_BORG_USER} -gt 0 ];then
BORG_REPO+="${_BORG_USER}@"
fi
if [ ${#_BORG_USER} -eq 0 ];then
BORG_REPO+="${_BORG_HOST}/${_BORG_REPONAME}"
else
BORG_REPO+="${_BORG_HOST}:${_BORG_REPONAME}"
fi
bashio::log.debug "BORG_REPO set"
return
}
function add_borg_host_to_known_hosts {
bashio::log.info "in add_borg_host_to_known_hosts"
if ! bashio::fs.file_exists ${_BORG_SSH_KNOWN_HOSTS}; then
if [[ ( ${#_BORG_USER} -gt 0 ) ]];then
bashio::log.info "Adding host $1 into ${_BORG_SSH_KNOWN_HOSTS}"
ssh-keyscan ${_BORG_HOST} >> ${_BORG_SSH_KNOWN_HOSTS}
else
bashio::log.info "Local path ignoring ssh and unseting BORG_RSH"
unset BORG_RSH
fi
fi
}
function generate_ssh_key {
if ! bashio::fs.file_exists "${_BORG_SSH_KEY}"; then
bashio::log.info "Generating borg backup ssh keys..."
ssh-keygen -P '' -f ${_BORG_SSH_KEY}
bashio::log.info "key generated"
fi
}
function show_ssh_key {
bashio::log.info "Your ssh key to use for borg backup host"
bashio::log.info "************ SNIP **********************"
echo
cat ${_BORG_SSH_KEY}.pub
echo
bashio::log.info "************ SNIP **********************"
}
function init_borg_repo {
if ! bashio::fs.directory_exists "${BORG_BASE_DIR}/.config/borg/security"; then
bashio::log.info "Initializing backup repository"
borg init --encryption=repokey-blake2 --debug
fi
}
function borg_create_backup {
export BACKUP_TIME=$(date +'%Y-%m-%d-%H:%M')
bashio::log.info "Creating snapshot"
ha snapshots new --name borg-${BACKUP_TIME} --raw-json --no-progress |tee /tmp/borg_backup_$$
bashio::log.info "Snapshot done"
export SNAP_RES=$(jq < /tmp/borg_backup_$$ .result -r)
# if it is not ok something failed and should be logged anyway
if [ $SNAP_RES != 'ok' ];then
bashio::log.error "Failed creating ha snapshot"
exit -1
fi
export SNAP_SLUG=$(jq < /tmp/borg_backup_$$ -r .data.slug)
mkdir -p ${_BORG_TOBACKUP}/${SNAP_SLUG}
tar -C ${_BORG_TOBACKUP}/${SNAP_SLUG} -xf /backup/${SNAP_SLUG}.tar
for targz in ${_BORG_TOBACKUP}/${SNAP_SLUG}/*.tar.gz ; do
TGZDIR=$(echo ${targz}|sed -e 's/.tar.gz//g')
mkdir -p ${TGZDIR}
tar -C ${TGZDIR} -zxf $targz
rm -f $targz # remove compressed file
done
bashio::log.info "Start borg create"
borg create ${_BORG_DEBUG} --compression ${_BORG_COMPRESSION} --stats ::"${BACKUP_TIME}" ${_BORG_TOBACKUP}/${SNAP_SLUG}
bashio::log.info "End borg create --stats..."
# cleanup
rm -rf ${_BORG_TOBACKUP} /tmp/borg_backup_$$
}
function clean_old_snapshots {
ha snapshots reload
export ALL_SNAPS=$(ha snapshots --raw-json|jq '.data.snapshots[].name' -r| sort | wc -l)
export DISCARD_SNAPS=$(($ALL_SNAPS - $_BORG_BACKUP_KEEP_SNAPSHOTS))
export ALL_SNAPS=$(ha snapshots --raw-json|jq '.data.snapshots[].name' -r| sort | head -n ${DISCARD_SNAPS})
for snap in $ALL_SNAPS ; do
SLUG=$(ha snapshots --raw-json |jq -r '.data.snapshots[]|select (.name=="'${snap}'")|.slug')
bashio::log.info "Removing snapshot ${snap} with slug id $SLUG started"
ha snapshots remove $SLUG
bashio::log.info "Removed snapshot ${snap} with slug id $SLUG"
done
bashio::log.info "Cleanup of old snapshots done"
}
sanity_checks
if [[ $borg_error -gt 0 ]];then
bashio::log.warning "error state bailing out..."
exit -1
fi
generate_ssh_key
set_borg_repo_path
add_borg_host_to_known_hosts
init_borg_repo
show_ssh_key
borg_create_backup
clean_old_snapshots