Skip to content

Commit a00b50a

Browse files
authored
Merge pull request hestiacp#1088 from hestiacp/fix/1083-Delete-remote-backups
hestiacp#1083 Add support for remote backups
2 parents 83141fc + d0bd5a1 commit a00b50a

File tree

5 files changed

+441
-572
lines changed

5 files changed

+441
-572
lines changed

bin/v-backup-user

Lines changed: 1 addition & 355 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ notify=${2-no}
2020
source $HESTIA/func/main.sh
2121
source $HESTIA/func/domain.sh
2222
source $HESTIA/func/db.sh
23+
source $HESTIA/func/backup.sh
2324
source $HESTIA/conf/hestia.conf
2425

2526

@@ -524,361 +525,6 @@ time=$(echo "$time_n_date" |cut -f 1 -d \ )
524525
date=$(echo "$time_n_date" |cut -f 2 -d \ )
525526
backup_new_date=$(date +"%Y-%m-%d_%H-%M-%S")
526527

527-
# Defining local storage function
528-
local_backup(){
529-
530-
rm -f $BACKUP/$user.$backup_new_date.tar
531-
532-
# Checking retention
533-
backup_list=$(ls -lrt $BACKUP/ |awk '{print $9}' |grep "^$user\." | grep ".tar")
534-
backups_count=$(echo "$backup_list" |wc -l)
535-
if [ "$BACKUPS" -le "$backups_count" ]; then
536-
backups_rm_number=$((backups_count - BACKUPS + 1))
537-
538-
# Removing old backup
539-
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
540-
backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
541-
echo -e "$(date "+%F %T") Rotated: $backup_date" |\
542-
tee -a $BACKUP/$user.log
543-
rm -f $BACKUP/$backup
544-
done
545-
fi
546-
547-
# Checking disk space
548-
disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
549-
if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
550-
rm -rf $tmpdir
551-
rm -f $BACKUP/$user.log
552-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
553-
echo "Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
554-
check_result "$E_DISK" "Not enough dsk space"
555-
fi
556-
557-
# Creating final tarball
558-
cd $tmpdir
559-
tar -cf $BACKUP/$user.$backup_new_date.tar .
560-
chmod 640 $BACKUP/$user.$backup_new_date.tar
561-
chown admin:$user $BACKUP/$user.$backup_new_date.tar
562-
localbackup='yes'
563-
echo -e "$(date "+%F %T") Local: $BACKUP/$user.$backup_new_date.tar" |\
564-
tee -a $BACKUP/$user.log
565-
}
566-
567-
568-
# Defining ftp command function
569-
ftpc() {
570-
/usr/bin/ftp -np $HOST $PORT <<EOF
571-
quote USER $USERNAME
572-
quote PASS $PASSWORD
573-
binary
574-
$1
575-
$2
576-
$3
577-
quit
578-
EOF
579-
}
580-
581-
# Defining ftp storage function
582-
ftp_backup() {
583-
# Checking config
584-
if [ ! -e "$HESTIA/conf/ftp.backup.conf" ]; then
585-
error="ftp.backup.conf doesn't exist"
586-
rm -rf $tmpdir
587-
rm -f $BACKUP/$user.log
588-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
589-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
590-
check_result "$E_NOTEXIST" "$error"
591-
fi
592-
593-
# Parse config
594-
source $HESTIA/conf/ftp.backup.conf
595-
596-
# Set default port
597-
if [ -z "$(grep 'PORT=' $HESTIA/conf/ftp.backup.conf)" ]; then
598-
PORT='21'
599-
fi
600-
601-
# Checking variables
602-
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
603-
error="Can't parse ftp backup configuration"
604-
rm -rf $tmpdir
605-
rm -f $BACKUP/$user.log
606-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
607-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
608-
check_result "$E_PARSING" "$error"
609-
fi
610-
611-
# Debug info
612-
echo -e "$(date "+%F %T") Remote: ftp://$HOST$BPATH/$user.$backup_new_date.tar"
613-
614-
# Checking ftp connection
615-
fconn=$(ftpc)
616-
ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
617-
if [ ! -z "$ferror" ]; then
618-
error="Error: can't login to ftp ftp://$USERNAME@$HOST"
619-
rm -rf $tmpdir
620-
rm -f $BACKUP/$user.log
621-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
622-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
623-
check_result "$E_CONNECT" "$error"
624-
fi
625-
626-
# Check ftp permissions
627-
if [ -z $BPATH ]; then
628-
ftmpdir="vst.bK76A9SUkt"
629-
else
630-
ftpc "mkdir $BPATH" > /dev/null 2>&1
631-
ftmpdir="$BPATH/vst.bK76A9SUkt"
632-
fi
633-
ftpc "mkdir $ftmpdir" "rm $ftmpdir"
634-
ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
635-
if [ ! -z "$ftp_result" ] ; then
636-
error="Can't create ftp backup folder ftp://$HOST$BPATH"
637-
rm -rf $tmpdir
638-
rm -f $BACKUP/$user.log
639-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
640-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
641-
check_result "$E_FTP" "$error"
642-
fi
643-
644-
# Checking retention
645-
if [ -z $BPATH ]; then
646-
backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
647-
else
648-
backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
649-
fi
650-
backups_count=$(echo "$backup_list" |wc -l)
651-
if [ "$backups_count" -ge "$BACKUPS" ]; then
652-
backups_rm_number=$((backups_count - BACKUPS + 1))
653-
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
654-
backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
655-
echo -e "$(date "+%F %T") Rotated ftp backup: $backup_date" |\
656-
tee -a $BACKUP/$user.log
657-
if [ -z $BPATH ]; then
658-
ftpc "delete $backup"
659-
else
660-
ftpc "cd $BPATH" "delete $backup"
661-
fi
662-
done
663-
fi
664-
665-
# Uploading backup archive
666-
if [ "$localbackup" = 'yes' ]; then
667-
cd $BACKUP
668-
if [ -z $BPATH ]; then
669-
ftpc "put $user.$backup_new_date.tar"
670-
else
671-
ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
672-
fi
673-
else
674-
cd $tmpdir
675-
tar -cf $BACKUP/$user.$backup_new_date.tar .
676-
cd $BACKUP/
677-
if [ -z $BPATH ]; then
678-
ftpc "put $user.$backup_new_date.tar"
679-
else
680-
ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
681-
fi
682-
rm -f $user.$backup_new_date.tar
683-
fi
684-
}
685-
686-
# sftp command function
687-
sftpc() {
688-
expect -f "-" <<EOF "$@"
689-
set timeout 60
690-
set count 0
691-
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
692-
-o Port=$PORT $USERNAME@$HOST
693-
expect {
694-
"password:" {
695-
send "$PASSWORD\r"
696-
exp_continue
697-
}
698-
699-
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
700-
set count \$argc
701-
set output "Disconnected."
702-
set rc $E_FTP
703-
exp_continue
704-
}
705-
706-
-re ".*denied.*(publickey|password)." {
707-
set output "Permission denied, wrong publickey or password."
708-
set rc $E_CONNECT
709-
}
710-
711-
-re "\[0-9]*%" {
712-
exp_continue
713-
}
714-
715-
"sftp>" {
716-
if {\$count < \$argc} {
717-
set arg [lindex \$argv \$count]
718-
send "\$arg\r"
719-
incr count
720-
} else {
721-
send "exit\r"
722-
set output "Disconnected."
723-
if {[info exists rc] != 1} {
724-
set rc $OK
725-
}
726-
}
727-
exp_continue
728-
}
729-
730-
timeout {
731-
set output "Connection timeout."
732-
set rc $E_CONNECT
733-
}
734-
}
735-
736-
if {[info exists output] == 1} {
737-
puts "\$output"
738-
}
739-
740-
exit \$rc
741-
EOF
742-
}
743-
744-
sftp_backup() {
745-
746-
# Checking config
747-
if [ ! -e "$HESTIA/conf/sftp.backup.conf" ]; then
748-
error="Can't open sftp.backup.conf"
749-
rm -rf $tmpdir
750-
rm -f $BACKUP/$user.log
751-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
752-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
753-
check_result "$E_NOTEXIST" "$error"
754-
fi
755-
756-
# Parse config
757-
source $HESTIA/conf/sftp.backup.conf
758-
759-
# Set default port
760-
if [ -z "$(grep 'PORT=' $HESTIA/conf/sftp.backup.conf)" ]; then
761-
PORT='22'
762-
fi
763-
764-
# Checking variables
765-
if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
766-
error="Can't parse sftp backup configuration"
767-
rm -rf $tmpdir
768-
rm -f $BACKUP/$user.log
769-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
770-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
771-
check_result "$E_PARSING" "$error"
772-
fi
773-
774-
# Debug info
775-
echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$backup_new_date.tar" |\
776-
tee -a $BACKUP/$user.log
777-
778-
# Checking network connection and write permissions
779-
if [ -z $BPATH ]; then
780-
sftmpdir="vst.bK76A9SUkt"
781-
else
782-
sftmpdir="$BPATH/vst.bK76A9SUkt"
783-
fi
784-
sftpc "mkdir $BPATH" > /dev/null 2>&1
785-
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
786-
rc=$?
787-
if [[ "$rc" != 0 ]]; then
788-
case $rc in
789-
$E_CONNECT) error="Can't login to sftp host $HOST" ;;
790-
$E_FTP) error="Can't create temp folder on sftp $HOST" ;;
791-
esac
792-
rm -rf $tmpdir
793-
rm -f $BACKUP/$user.log
794-
echo "$error" |$SENDMAIL -s "$subj" $email $notify
795-
sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
796-
check_result "$rc" "$error"
797-
fi
798-
799-
# Checking retention
800-
if [ -z $BPATH ]; then
801-
backup_list=$(sftpc "ls -l" |awk '{print $9}'|grep "^$user\.")
802-
else
803-
backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
804-
fi
805-
backups_count=$(echo "$backup_list" |wc -l)
806-
if [ "$backups_count" -ge "$BACKUPS" ]; then
807-
backups_rm_number=$((backups_count - BACKUPS + 1))
808-
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
809-
backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
810-
echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" |\
811-
tee -a $BACKUP/$user.log
812-
if [ -z $BPATH ]; then
813-
sftpc "rm $backup" > /dev/null 2>&1
814-
else
815-
sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
816-
fi
817-
done
818-
fi
819-
820-
# Uploading backup archive
821-
echo "$(date "+%F %T") Uploading $user.$backup_new_date.tar"|tee -a $BACKUP/$user.log
822-
if [ "$localbackup" = 'yes' ]; then
823-
cd $BACKUP
824-
if [ -z $BPATH ]; then
825-
sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
826-
else
827-
sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
828-
fi
829-
else
830-
cd $tmpdir
831-
tar -cf $BACKUP/$user.$backup_new_date.tar .
832-
cd $BACKUP/
833-
if [ -z $BPATH ]; then
834-
sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
835-
else
836-
sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
837-
fi
838-
rm -f $user.$backup_new_date.tar
839-
fi
840-
}
841-
842-
google_backup() {
843-
844-
# Defining google settings
845-
source $HESTIA/conf/google.backup.conf
846-
gsutil="$HESTIA/3rdparty/gsutil/gsutil"
847-
export BOTO_CONFIG="$HESTIA/conf/.google.backup.boto"
848-
849-
# Debug info
850-
echo -e "$(date "+%F %T") Remote: gs://$BUCKET/$BPATH/$user.$backup_new_date.tar"
851-
852-
# Checking retention
853-
backup_list=$(${gsutil} ls gs://$BUCKET/$BPATH/$user.* 2>/dev/null)
854-
backups_count=$(echo "$backup_list" |wc -l)
855-
if [ "$backups_count" -ge "$BACKUPS" ]; then
856-
backups_rm_number=$((backups_count - BACKUPS))
857-
for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
858-
echo -e "$(date "+%F %T") Rotated gcp backup: $backup"
859-
$gsutil rm $backup > /dev/null 2>&1
860-
done
861-
fi
862-
863-
# Uploading backup archive
864-
echo -e "$(date "+%F %T") Uploading $user.$backup_new_date.tar ..."
865-
if [ "$localbackup" = 'yes' ]; then
866-
cd $BACKUP
867-
${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
868-
else
869-
cd $tmpdir
870-
tar -cf $BACKUP/$user.$backup_new_date.tar .
871-
cd $BACKUP/
872-
${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
873-
rc=$?
874-
rm -f $user.$backup_new_date.tar
875-
if [ "$rc" -ne 0 ]; then
876-
check_result "$E_CONNECT" "gsutil failed to upload $user.$backup_new_date.tar"
877-
fi
878-
fi
879-
}
880-
881-
882528
echo -e "\n-- SUMMARY --" |tee -a $BACKUP/$user.log
883529

884530
# Switching on backup system types

0 commit comments

Comments
 (0)