Skip to content

Commit 558b69f

Browse files
jaapmarcusAlecRust
andauthored
Rclone support (hestiacp#2928)
* Add support rclone * Fix naming * Fix 2 small bugs * Update v-list-backup-host * Changes to web ui * Update web/templates/pages/edit_server.html Co-authored-by: Alec Rust <me@alecrust.com> Co-authored-by: Alec Rust <me@alecrust.com>
1 parent 508d3f1 commit 558b69f

File tree

10 files changed

+167
-15
lines changed

10 files changed

+167
-15
lines changed

bin/v-add-backup-host

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ EOF
157157
#----------------------------------------------------------#
158158

159159
if [ "$type" != 'local' ];then
160-
check_args '4' "$#" "TYPE HOST USERNAME PASSWORD [PATH] [PORT]"
160+
check_args '2' "$#" "TYPE HOST USERNAME PASSWORD [PATH] [PORT]"
161161
is_format_valid 'host' 'path' 'port'
162-
is_type_valid 'sftp,ftp,b2' "$type"
162+
is_type_valid 'sftp,ftp,b2,rclone' "$type"
163163
is_username_format_valid "$user" "username"
164164
privatekey="no"
165165
if [ -f "$raw_password" ]; then
@@ -178,7 +178,7 @@ if [ "$type" != 'local' ];then
178178
which expect >/dev/null 2>&1
179179
check_result $? "expect command not found" "$E_NOTEXIST"
180180
fi
181-
if [ "$type" != 'b2' ]; then
181+
if [ "$type" != 'b2' ] && [ "$type" != 'rclone' ]; then
182182
if ! (is_ip_format_valid "$host" >/dev/null); then
183183
host "$host" >/dev/null 2>&1
184184
check_result $? "host connection failed" "$E_CONNECT"
@@ -283,8 +283,26 @@ if [ "$type" = 'b2' ]; then
283283
fi
284284
fi
285285

286+
if [ "$type" = 'rclone' ]; then
287+
curl -s https://rclone.org/install.sh | bash /dev/null > /dev/null 2>&1
288+
# Verify account exists
289+
if [ ! -z "$(cat /root/.config/rclone/rclone.conf | grep "\[$host\]")" ]; then
290+
echo "test" > /tmp/hestia-backup.txt
291+
# Try to upload a single file
292+
if [ -z "$path" ]; then
293+
rclone copy /tmp/hestia-backup $host:/hestia-backup.txt
294+
rclone delete $host:/hestia-backup.txt
295+
else
296+
rclone copy /tmp/hestia-backup $host:$path/hestia-backup.txt
297+
rclone delete $host:$path/hestia-backup.txt
298+
fi
299+
else
300+
check_result "$E_CONNECT" "Rclone config does not exits"
301+
fi
302+
fi
303+
286304
# Adding backup host
287-
if [ $type != 'local' ] && [ $type != 'b2' ]; then
305+
if [ $type == 'ftp' ] || [ $type = 'sftp' ] ; then
288306
new_timestamp
289307
str="HOST='$host'\nUSERNAME='$user'\nPASSWORD='$password'\nPRIVATEKEY='$privatekey'"
290308
str="$str\nBPATH='$path'\nPORT='$port'\nTIME='$time'\nDATE='$date'"
@@ -296,6 +314,11 @@ elif [ $type == 'b2' ]; then
296314
str="$str\nTIME='$time'\nDATE='$date'"
297315
echo -e "$str" > $HESTIA/conf/$type.backup.conf
298316
chmod 660 $HESTIA/conf/$type.backup.conf
317+
elif [ $type == "rclone" ]; then
318+
new_timestamp
319+
str="HOST='$host'\nBPATH='$path'"
320+
str="$str\nTIME='$time'\nDATE='$date'"
321+
echo -e "$str" > $HESTIA/conf/$type.backup.conf
299322
fi
300323

301324
#----------------------------------------------------------#

bin/v-backup-user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
706706
sftp) sftp_backup ;;
707707
google) google_backup ;;
708708
b2) b2_backup ;;
709+
rclone) rclone_backup ;;
709710
esac
710711
done
711712

bin/v-delete-user-backup

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ fi
5757
if [[ "$TYPE" =~ "b2" ]]; then
5858
b2_delete "$user" "$backup"
5959
fi
60+
if [[ "$TYPE" =~ "rclone" ]]; then
61+
rclone_delete "$backup"
62+
fi
6063
if [[ "$TYPE" =~ "local" ]]; then
6164
rm -f "$backup_folder/$2"
6265
fi

bin/v-download-backup

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ if [ ! -e "$BACKUP/$backup" ]; then
7474
ftp_download "$backup"
7575
downloaded='yes'
7676
fi
77+
if [[ "$BACKUP_SYSTEM" =~ "rclone" ]] && [ -z "$downloaded" ]; then
78+
rclone_download "$backup"
79+
downloaded='yes'
80+
fi
81+
if [[ "$BACKUP_SYSTEM" =~ "b2" ]] && [ -z "$downloaded" ]; then
82+
b2_download "$backup"
83+
downloaded='yes'
84+
fi
7785
if [ -z "$downloaded" ]; then
7886
subj="Download of $backup failed for $user"
7987
$BIN/v-add-user-notification $user "$subj" "<b>Unable to retrieve backup file from remote server.</b><br><b>Error:</b> $backup file doesn't exist in '${BACKUP}' directory."

bin/v-list-backup-host

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ source_conf "$HESTIA/conf/hestia.conf"
2525
# JSON list function
2626
json_list() {
2727
case $type in
28-
'sftp' | 'ftp')
28+
'sftp' | 'ftp' | 'rclone')
2929
echo '{'
3030
echo ' "'$type'": {
3131
"HOST": "'$HOST'",
@@ -55,7 +55,7 @@ json_list() {
5555
# SHELL list function
5656
shell_list() {
5757
case $type in
58-
'sftp' | 'ftp')
58+
'sftp' | 'ftp' | 'rclone')
5959
echo "HOST: $HOST"
6060
echo "USERNAME: $USERNAME"
6161
echo "PORT: $PORT"
@@ -77,7 +77,7 @@ shell_list() {
7777
# PLAIN list function
7878
plain_list() {
7979
case $type in
80-
'sftp' | 'ftp')
80+
'sftp' | 'ftp' | 'rclone')
8181
echo -e "$HOST\t$USERNAME\t$PORT\t$type\t$BPATH\t$TIME\t$DATE"
8282
;;
8383
'b2')
@@ -89,7 +89,7 @@ plain_list() {
8989
# CSV list function
9090
csv_list() {
9191
case $type in
92-
'sftp' | 'ftp')
92+
'sftp' | 'ftp' | 'rclone')
9393
echo "HOST,USERNAME,PORT,TYPE,PATH,TIME,DATE"
9494
echo "$HOST,$USERNAME,$PORT,$type,$BPATH,$TIME,$DATE"
9595
;;
@@ -113,7 +113,7 @@ is_type_format_valid() {
113113
#----------------------------------------------------------#
114114

115115
check_args '1' "$#" 'TYPE [FORMAT]'
116-
is_type_format_valid "$type"
116+
is_common_format_valid $type "type"
117117

118118
#----------------------------------------------------------#
119119
# Action #

bin/v-restore-user

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ if [ ! -e "$BACKUP/$backup" ]; then
8585
b2_download "$backup"
8686
downloaded='yes'
8787
fi
88+
if [[ "$BACKUP_SYSTEM" =~ "rclone" ]] && [ -z "$downloaded" ]; then
89+
rclone_download "$backup"
90+
downloaded='yes'
91+
fi
8892
if [ -z "$downloaded" ]; then
8993
check_result "$E_NOTEXIST" "backup file $backup doesn't exist in '${BACKUP}' folder"
9094
fi

func/backup.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,67 @@ b2_delete(){
556556

557557
b2 delete-file-version $1/$2 > /dev/null 2>&1
558558
}
559+
560+
rclone_backup(){
561+
# Define rclone config
562+
source_conf "$HESTIA/conf/rclone.backup.conf"
563+
echo -e "$(date "+%F %T") Upload With Rclone: $user.$backup_new_date.tar"
564+
if [ "$localbackup" != 'yes' ]; then
565+
cd $tmpdir
566+
tar -cf $BACKUP/$user.$backup_new_date.tar .
567+
fi
568+
cd $BACKUP/
569+
570+
if [ -z "$BPATH" ]; then
571+
rclone copy -v $user.$backup_new_date.tar $HOST
572+
if [ "$?" -ne 0 ]; then
573+
check_result "$E_CONNECT" "Unable to upload backup"
574+
fi
575+
576+
backup_list=$(rclone lsf $HOST | cut -d' ' -f1 |grep "^$user\.");
577+
backups_count=$(echo "$backup_list" |wc -l)
578+
backups_rm_number=$((backups_count - BACKUPS))
579+
if [ "$backups_count" -ge "$BACKUPS" ]; then
580+
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
581+
echo "Delete file: $backup"
582+
rclone deletefile $HOST:/$backup
583+
done
584+
fi
585+
else
586+
rclone copy -v $user.$backup_new_date.tar $HOST:$BPATH
587+
if [ "$?" -ne 0 ]; then
588+
check_result "$E_CONNECT" "Unable to upload backup"
589+
fi
590+
591+
backup_list=$(rclone lsf $HOST:$BPATH | cut -d' ' -f1 |grep "^$user\.");
592+
backups_count=$(echo "$backup_list" |wc -l)
593+
backups_rm_number=$((backups_count - BACKUPS))
594+
if [ "$backups_count" -ge "$BACKUPS" ]; then
595+
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
596+
echo "Delete file: $backup"
597+
rclone deletefile $HOST:$BPATH/$backup
598+
done
599+
fi
600+
fi
601+
}
602+
603+
rclone_delete(){
604+
# Defining rclone settings
605+
source_conf "$HESTIA/conf/rclone.backup.conf"
606+
if [ -z "$BPATH" ]; then
607+
rclone deletefile $HOST:/$1
608+
else
609+
rclone deletefile $HOST:$BPATH/$1
610+
fi
611+
}
612+
613+
rclone_download() {
614+
# Defining rclone b2 settings
615+
source_conf "$HESTIA/conf/rclone.backup.conf"
616+
cd $BACKUP
617+
if [ -z "$BPATH" ]; then
618+
rclone copy -v $HOST:/$1 ./
619+
else
620+
rclone copy -v $HOST:$BPATH/$1 ./
621+
fi
622+
}

web/edit/server/index.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@
219219
if (in_array($backup_type, array('ftp','sftp'))) {
220220
$v_backup_host = $v_remote_backup[$backup_type]['HOST'];
221221
$v_backup_type = $v_remote_backup[$backup_type]['TYPE'];
222-
$v_backup_username = $v_remote_backup[$backup_type]['USERNAME'];
222+
$v_backup_username = $v_remote_backup[$backup_type]['USERNAME'] ?? '';
223223
$v_backup_password = "";
224-
$v_backup_port = $v_remote_backup[$backup_type]['PORT'];
224+
$v_backup_port = $v_remote_backup[$backup_type]['PORT'] ?? '';
225225
$v_backup_bpath = $v_remote_backup[$backup_type]['BPATH'];
226226
$v_backup_remote_adv = "yes";
227227
} elseif (in_array($backup_type, array('b2'))) {
@@ -230,6 +230,11 @@
230230
$v_backup_application_id = $v_remote_backup[$backup_type]['B2_KEY_ID'];
231231
$v_backup_application_key = '';
232232
$v_backup_remote_adv = "yes";
233+
} elseif (in_array($backup_type, array('rclone'))) {
234+
$v_backup_type = $v_remote_backup[$backup_type]['TYPE'];
235+
$v_rclone_host = $v_remote_backup[$backup_type]['HOST'];
236+
$v_rclone_path = $v_remote_backup[$backup_type]['BPATH'];
237+
$v_backup_remote_adv = "yes";
233238
}
234239
}
235240
}
@@ -266,8 +271,11 @@
266271
if (empty($v_backup_remote_adv)) {
267272
$v_backup_remote_adv = '';
268273
}
269-
if (empty($v_backup_remote_adv)) {
270-
$v_backup_remote_adv = '';
274+
if (empty($v_rclone_host)) {
275+
$v_rclone_host = '';
276+
}
277+
if (empty($v_rclone_path)) {
278+
$v_rclone_path = '';
271279
}
272280

273281
// List ssl certificate info
@@ -812,6 +820,17 @@
812820
$v_backup_remote_adv = 'yes';
813821
}
814822
}
823+
if ($v_rclone_host == '' && !empty($_POST['v_rclone_host']) && $_POST['v_backup_type'] =="rclone" ) {
824+
$v_rclone_host = quoteshellarg($_POST['v_rclone_host']);
825+
$v_backup_type = quoteshellarg($_POST['v_backup_type']);
826+
$v_rclone_path = quoteshellarg($_POST['v_rclone_path']);
827+
exec(HESTIA_CMD."v-add-backup-host ". $v_backup_type ." ". $v_rclone_host ." '' '' ".$v_rclone_path, $output, $return_var);
828+
check_return_code($return_var, $output);
829+
unset($output);
830+
$v_backup_new = 'yes';
831+
$v_backup_adv = 'yes';
832+
$v_backup_remote_adv = 'yes';
833+
}
815834
}
816835

817836
// Change remote backup host type

web/js/pages/edit_server.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ $('#backup_type').change(function (){
22
if(this.value == 'b2'){
33
$('#backup_bucket').show();
44
$('#backup_sftp').hide();
5+
$('#backup_rclone').hide();
6+
}else if(this.value == 'rclone'){
7+
$('#backup_bucket').hide();
8+
$('#backup_sftp').hide();
9+
$('#backup_rclone').show();
510
}else{
6-
$('#backup_bucket').hide();
7-
$('#backup_sftp').show();
11+
$('#backup_bucket').hide();
12+
$('#backup_sftp').show();
13+
$('#backup_rclone').hide();
814
}
915
});
1016

web/templates/pages/edit_server.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@
661661
<option value='ftp'><?=_('ftp'); ?></option>
662662
<option value='sftp' <?php if((!empty($v_backup_type)) && (trim($v_backup_type,"'") == 'sftp' )) echo 'selected="selected"'; ?>><?=_('sftp'); ?></option>
663663
<option value="b2" <?php if((!empty($v_backup_type)) && (trim($v_backup_type,"'") == 'b2' )) echo 'selected="selected"'; ?>><?=_('Backblaze'); ?>
664+
<option value="rclone" <?php if((!empty($v_backup_type)) && (trim($v_backup_type,"'") == 'rclone' )) echo 'selected="selected"'; ?>><?=_('Rclone'); ?>
664665
</select>
665666
</td>
666667
</tr>
@@ -726,6 +727,29 @@
726727
</table>
727728
</td>
728729
</tr>
730+
<tr style="display: <?php if ((empty($v_backup_type)) || !in_array(trim($v_backup_type, "'"),array('rclone'))){ echo 'none';} else {echo 'block';} ?>" id="backup_rclone">
731+
<td>
732+
<table>
733+
<tr>
734+
<td class="vst-text">
735+
<label for="v_rclone_host" class="form-label"><?=_('Host');?></label>
736+
<input type="text" class="form-control" name="v_rclone_host" id="v_rclone_host" value="<?=trim($v_rclone_host, "'")?>">
737+
</td>
738+
</tr>
739+
<tr>
740+
<td class="vst-text">
741+
<?=_('Directory');?>
742+
</td>
743+
</tr>
744+
<tr>
745+
<td>
746+
<input type="text" size="20" class="vst-input" name="v_rclone_path" value="<?=trim($v_rclone_path, "'")?>">
747+
<br><br>
748+
</td>
749+
</tr>
750+
</table>
751+
</td>
752+
</tr>
729753
</table>
730754
</td>
731755
</tr>

0 commit comments

Comments
 (0)