Skip to content

Commit ebd6dc9

Browse files
Feature/b2 backup support (hestiacp#1534)
* Add download and validation routine for b2 backup support. * Add config part, example and credential validation * Add upload function, thanks to @rez0n. * Fix a small issue with var definition. * Small fixes * Increase version number to 1.4.0 * Add WebUI support Add check at v-add-backup-host Added information v-list-backup-host * Replaced remote backup with input box Fixed minor issue with saving * Update code formating. * Drop commented error reporting. Co-authored-by: Jaap Marcus <9754650+jaapmarcus@users.noreply.github.com>
1 parent 970dc0e commit ebd6dc9

File tree

10 files changed

+367
-149
lines changed

10 files changed

+367
-149
lines changed

bin/v-add-backup-host

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# labels:
55
#
66
# example: v-add-backup-host sftp backup.acme.com admin p4$$w@Rd
7+
# v-add-backup-host b2 bucketName keyID applicationKey
78
#
89
# This function adds a backup host
910

@@ -25,6 +26,10 @@ port=$6
2526
source $HESTIA/func/main.sh
2627
source $HESTIA/conf/hestia.conf
2728

29+
# Paths
30+
b2cli="/usr/local/bin/b2"
31+
b2lnk="https://f000.backblazeb2.com/file/backblazefiles/b2/cli/linux/b2"
32+
2833
# Defining ftp command function
2934
ftpc() {
3035
ftp -p -n $host $port <<EOF
@@ -103,9 +108,11 @@ if [ "$type" != 'local' ];then
103108
which expect >/dev/null 2>&1
104109
check_result $? "expect command not found" $E_NOTEXIST
105110
fi
106-
if ! (is_ip_format_valid "$host" >/dev/null); then
107-
host "$host" >/dev/null 2>&1
108-
check_result $? "host connection failed" "$E_CONNECT"
111+
if [ "$type" != 'b2' ]; then
112+
if ! (is_ip_format_valid "$host" >/dev/null); then
113+
host "$host" >/dev/null 2>&1
114+
check_result $? "host connection failed" "$E_CONNECT"
115+
fi
109116
fi
110117
fi
111118

@@ -146,6 +153,7 @@ if [ "$type" = 'ftp' ]; then
146153
exit $E_FTP
147154
fi
148155
fi
156+
149157
if [ "$type" = 'sftp' ]; then
150158
if [ -z $port ]; then
151159
port=22
@@ -173,14 +181,46 @@ if [ "$type" = 'sftp' ]; then
173181
fi
174182
fi
175183

184+
if [ "$type" = 'b2' ]; then
185+
# Download b2 binary
186+
if [ ! -f "$b2cli" ]; then
187+
wget -O $b2cli $b2lnk > /dev/null 2>&1
188+
chmod +x $b2cli > /dev/null 2>&1
189+
if [ ! -f "$b2cli" ]; then
190+
echo "Error: Binary download failed, b2 doesnt work as expected."
191+
exit 3
192+
fi
193+
fi
194+
195+
# Validate b2 binary
196+
b2version="$(b2 version)"
197+
if [[ ! "$b2version" =~ "b2 command line tool" ]]; then
198+
echo "Error: Binary download failed, b2 doesnt work as expected."
199+
exit 3
200+
fi
201+
202+
b2 clear-account > /dev/null 2>&1
203+
b2 authorize-account "$user" "$raw_password"> /dev/null 2>&1
204+
b2 ls --long $host $user > /dev/null 2>&1
205+
206+
if [ $? -ne 0 ]; then
207+
check_result "$E_CONNECT" "b2 failed to verify connection"
208+
fi
209+
fi
176210

177211
# Adding backup host
178-
if [ $type != 'local' ]; then
212+
if [ $type != 'local' ] && [ $type != 'b2' ]; then
179213
new_timestamp
180214
str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'"
181215
str="$str\nBPATH='$path'\nPORT='$port'\nTIME='$time'\nDATE='$date'"
182216
echo -e "$str" > $HESTIA/conf/$type.backup.conf
183217
chmod 660 $HESTIA/conf/$type.backup.conf
218+
elif [ $type == 'b2' ]; then
219+
new_timestamp
220+
str="BUCKET='$host'\nB2_KEYID='$user'\nB2_KEY='$password'"
221+
str="$str\nTIME='$time'\nDATE='$date'"
222+
echo -e "$str" > $HESTIA/conf/$type.backup.conf
223+
chmod 660 $HESTIA/conf/$type.backup.conf
184224
fi
185225

186226

bin/v-backup-user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
594594
ftp) ftp_backup ;;
595595
sftp) sftp_backup ;;
596596
google) google_backup ;;
597+
b2) b2_backup ;;
597598
esac
598599
done
599600

bin/v-list-backup-host

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ source $HESTIA/func/main.sh
2121

2222
# JSON list function
2323
json_list() {
24+
case $type in
25+
'sftp' | 'ftp')
2426
echo '{'
2527
echo ' "'$type'": {
2628
"HOST": "'$HOST'",
@@ -32,28 +34,67 @@ json_list() {
3234
"DATE": "'$DATE'"
3335
}'
3436
echo '}'
37+
;;
38+
'b2')
39+
echo '{'
40+
echo ' "'$type'": {
41+
"BUCKET": "'$BUCKET'",
42+
"TYPE": "'$type'",
43+
"B2_KEY_ID": "'$B2_KEYID'",
44+
"TIME": "'$TIME'",
45+
"DATE": "'$DATE'"
46+
}'
47+
echo '}'
48+
;;
49+
esac
3550
}
3651

3752
# SHELL list function
3853
shell_list() {
54+
case $type in
55+
'sftp' | 'ftp')
3956
echo "HOST: $HOST"
4057
echo "USERNAME: $USERNAME"
4158
echo "PORT: $PORT"
4259
echo "TYPE: $type"
4360
echo "PATH: $BPATH"
4461
echo "TIME: $TIME"
4562
echo "DATE: $DATE"
63+
;;
64+
'b2')
65+
echo "BUCKET $BUCKET"
66+
echo "TYPE $type"
67+
echo "B2_KEY_ID $B2_KEYID"
68+
echo "TIME: $TIME"
69+
echo "DATE: $DATE"
70+
;;
71+
esac
4672
}
4773

4874
# PLAIN list function
4975
plain_list() {
76+
case $type in
77+
'sftp' | 'ftp')
5078
echo -e "$HOST\t$USERNAME\t$PORT\t$type\t$BPATH\t$TIME\t$DATE"
79+
;;
80+
'b2')
81+
echo -e "$BUCKET\t$type\t$B2_KEYID\t$TIME\t$DATE"
82+
;;
83+
esac
5184
}
5285

5386
# CSV list function
5487
csv_list() {
88+
case $type in
89+
'sftp' | 'ftp')
5590
echo "HOST,USERNAME,PORT,TYPE,PATH,TIME,DATE"
5691
echo "$HOST,$USERNAME,$PORT,$type,$BPATH,$TIME,$DATE"
92+
;;
93+
'b2')
94+
echo "BUCKET,TYPE,B2_KEYID,TIME,DATE"
95+
echo "$BUCKET,$type,$B2_KEYID,$TIME,$DATE"
96+
;;
97+
esac
5798
}
5899

59100
# Type format validator

func/backup.sh

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,43 @@ google_download() {
418418
if [ "$?" -ne 0 ]; then
419419
check_result "$E_CONNECT" "gsutil failed to download $1"
420420
fi
421-
}
421+
}
422+
423+
# BackBlaze B2 backup function
424+
b2_backup() {
425+
# Defining backblaze b2 settings
426+
source $HESTIA/conf/b2.backup.conf
427+
428+
# Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
429+
b2 clear-account > /dev/null 2>&1
430+
b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
431+
432+
# Uploading backup archive
433+
echo -e "$(date "+%F %T") Upload to B2: $user/$user.$backup_new_date.tar"
434+
if [ "$localbackup" = 'yes' ]; then
435+
cd $BACKUP
436+
b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
437+
else
438+
cd $tmpdir
439+
tar -cf $BACKUP/$user.$backup_new_date.tar .
440+
cd $BACKUP/
441+
b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
442+
rc=$?
443+
rm -f $user.$backup_new_date.tar
444+
if [ "$rc" -ne 0 ]; then
445+
check_result "$E_CONNECT" "b2 failed to upload $user.$backup_new_date.tar"
446+
fi
447+
fi
448+
449+
# Checking retention
450+
backup_list=$(b2 ls --long $BUCKET $user | cut -f 1 -d ' ' 2>/dev/null)
451+
backups_count=$(echo "$backup_list" |wc -l)
452+
if [ "$backups_count" -ge "$BACKUPS" ]; then
453+
backups_rm_number=$((backups_count - BACKUPS))
454+
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
455+
backup_file_name=$(b2 get-file-info $backup | grep fileName | cut -f 4 -d '"' 2>/dev/null)
456+
echo -e "$(date "+%F %T") Rotated b2 backup: $backup_file_name"
457+
b2 delete-file-version $backup > /dev/null 2>&1
458+
done
459+
fi
460+
}

install/hst-install-debian.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ HESTIA_INSTALL_DIR="$HESTIA/install/deb"
2323
VERBOSE='no'
2424

2525
# Define software versions
26-
HESTIA_INSTALL_VER='1.3.3~alpha'
26+
HESTIA_INSTALL_VER='1.4.0~alpha'
2727
pma_v='5.0.4'
2828
rc_v="1.4.10"
2929
multiphp_v=("5.6" "7.0" "7.1" "7.2" "7.3" "7.4" "8.0")

install/hst-install-ubuntu.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ HESTIA_INSTALL_DIR="$HESTIA/install/deb"
2323
VERBOSE='no'
2424

2525
# Define software versions
26-
HESTIA_INSTALL_VER='1.3.3~alpha'
26+
HESTIA_INSTALL_VER='1.4.0~alpha'
2727
pma_v='5.0.4'
2828
rc_v="1.4.10"
2929
multiphp_v=("5.6" "7.0" "7.1" "7.2" "7.3" "7.4" "8.0")

src/deb/hestia/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Source: hestia
22
Package: hestia
33
Priority: optional
4-
Version: 1.3.3~alpha
4+
Version: 1.4.0~alpha
55
Section: admin
66
Maintainer: HestiaCP <info@hestiacp.com>
77
Homepage: https://www.hestiacp.com

0 commit comments

Comments
 (0)