improvements on sftp backups

This commit is contained in:
Serghey Rodin 2015-03-29 12:40:24 +03:00
commit cb99788a43
5 changed files with 134 additions and 145 deletions

View file

@ -12,20 +12,23 @@
# Argument defenition
host=$1
ftp_user=$2
ftp_password=$3
password=$3
ftp_path=${4-/backup}
ftp_port=${5-21}
A3='******'
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
# Hiding password
A3='******'
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
# Defining ftp command function
ftpc() {
ftp -p -n $host $ftp_port <<EOF
quote USER $ftp_user
quote PASS $ftp_password
quote PASS $password
binary
$1
$2
@ -40,7 +43,8 @@ EOF
#----------------------------------------------------------#
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
validate_format 'host' 'ftp_user' 'ftp_password'
validate_format 'host' 'ftp_user'
is_password_valid
#----------------------------------------------------------#
@ -48,10 +52,11 @@ validate_format 'host' 'ftp_user' 'ftp_password'
#----------------------------------------------------------#
# Checking network connection
fconn=$(ftpc)
ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
fconn=$(ftpc 2>&1)
ferror=$(echo $fconn |\
grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
if [ ! -z "$ferror" ]; then
echo "Error: can't login to ftp"
echo "Error: can't login to ftp server"
log_event "$E_CONNECT" "$EVENT"
exit $E_CONNECT
fi
@ -71,7 +76,7 @@ fi
# Adding backup host
echo "HOST='$host'
USERNAME='$ftp_user'
PASSWORD='$ftp_password'
PASSWORD='$password'
BPATH='$ftp_path'
PORT='$ftp_port'
TIME='$TIME'
@ -84,7 +89,7 @@ chmod 660 $VESTA/conf/ftp.backup.conf
#----------------------------------------------------------#
# Update vesta.conf
if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
else
bckp=$(echo "$BACKUP_SYSTEM,ftp" |\

View file

@ -1,6 +1,6 @@
#!/bin/bash
# info: add backup sftp host
# options: HOST USERNAME [PASSWORD] [PATH] [PORT]
# options: HOST USERNAME PASSWORD [PATH] [PORT]
#
# The function adds sftp host for system backups
@ -11,87 +11,84 @@
# Argument defenition
sftp_host=$1
sftp_user=$2
sftp_password=${3-******}
password=$3
sftp_path=${4-backup}
sftp_port=${5-22}
A3='******'
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
# Replace password with ******
if [[ $A3 != '******' ]]
then
EVENT="${EVENT/$sftp_password/******}"
fi
# Hiding password
A3='******'
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
# sftp command function
sftpc() {
expect -f "-" <<EOF "$@"
set count 0
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$sftp_port $sftp_user@$sftp_host
expect {
"password:" {
send "$sftp_password\r"
exp_continue
}
expect -f "-" <<EOF "$@"
set count 0
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o \
Port=$sftp_port $sftp_user@$sftp_host
expect {
"password:" {
send "$password\r"
exp_continue
}
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
set count \$argc
set output "Disconnected."
set rc $E_FTP
exp_continue
}
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
set count \$argc
set output "Disconnected."
set rc $E_FTP
exp_continue
}
-re ".*denied.*(publickey|password)." {
set output "Permission denied, wrong publickey or password."
set rc $E_CONNECT
}
-re ".*denied.*(publickey|password)." {
set output "Permission denied, wrong publickey or password."
set rc $E_CONNECT
}
"sftp>" {
if {\$count < \$argc} {
set arg [lindex \$argv \$count]
send "\$arg\r"
incr count
} else {
send "exit\r"
set output "Disconnected."
if {[info exists rc] != 1} {
set rc $OK
}
}
exp_continue
}
"sftp>" {
if {\$count < \$argc} {
set arg [lindex \$argv \$count]
send "\$arg\r"
incr count
} else {
send "exit\r"
set output "Disconnected."
if {[info exists rc] != 1} {
set rc $OK
}
}
exp_continue
}
timeout {
set output "Connection timeout."
set rc $E_CONNECT
}
}
timeout {
set output "Connection timeout."
set rc $E_CONNECT
}
}
if {[info exists output] == 1} {
puts "\$output"
}
if {[info exists output] == 1} {
puts "\$output"
}
exit \$rc
exit \$rc
EOF
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_expect=$(which expect)
if [[ ! -n $check_expect ]]
then
echo "Error: \"expect\" utility not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
which expect >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "Error: expect utility not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
is_password_valid
#----------------------------------------------------------#
# Action #
@ -100,35 +97,35 @@ check_args '2' "$#" "HOST USERNAME [PASSWORD] [PATH] [PORT]"
# Checking network connection and write permissions
sftmpdir="$sftp_path/vst.bK76A9SUkt"
sftpc "mkdir $sftp_path" > /dev/null 2>&1
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir"
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
rc=$?
if [[ "$rc" != 0 ]]
then
case $rc in
if [[ "$rc" != 0 ]]; then
case $rc in
$E_CONNECT) echo "Error: can't login to sftp host";;
$E_FTP) echo "Error: can't create temp folder on the sftp host";;
esac
log_event "$rc" "$EVENT"
exit "$rc"
$E_FTP) echo "Error: can't create temp folder on the sftp host";;
esac
log_event "$rc" "$EVENT"
exit "$rc"
fi
# Adding sftp backup config file
echo "HOST='$sftp_host'
USERNAME='$sftp_user'
PASSWORD='$sftp_password'
PASSWORD='$password'
BPATH='$sftp_path'
PORT='$sftp_port'
TIME='$TIME'
DATE='$DATE'" > $VESTA/conf/sftp.backup.conf
chmod 660 $VESTA/conf/sftp.backup.conf
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Update vesta.conf
if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
echo "BACKUP_SYSTEM='sftp'" >> $VESTA/conf/vesta.conf
else
bckp=$(echo "$BACKUP_SYSTEM,sftp" |\
sed "s/,/\n/g"|\
@ -139,7 +136,6 @@ else
fi
# Logging
echo "$sftp_host/$sftp_path successfully added as backup destination"
log_event "$OK" "$EVENT"
exit

View file

@ -689,70 +689,63 @@ ftp_backup() {
# sftp command function
sftpc() {
expect -f "-" <<EOF "$@"
set timeout 60
set count 0
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o Port=$PORT $USERNAME@$HOST
expect {
"password:" {
send "$PASSWORD\r"
exp_continue
}
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
set count \$argc
set output "Disconnected."
set rc $E_FTP
exp_continue
}
-re ".*denied.*(publickey|password)." {
set output "Permission denied, wrong publickey or password."
set rc $E_CONNECT
}
-re "\[0-9]*%" {
exp_continue
}
"sftp>" {
if {\$count < \$argc} {
set arg [lindex \$argv \$count]
send "\$arg\r"
incr count
} else {
send "exit\r"
set output "Disconnected."
if {[info exists rc] != 1} {
set rc $OK
}
expect -f "-" <<EOF "$@"
set timeout 60
set count 0
spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
-o Port=$PORT $USERNAME@$HOST
expect {
"password:" {
send "$PASSWORD\r"
exp_continue
}
-re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
set count \$argc
set output "Disconnected."
set rc $E_FTP
exp_continue
}
-re ".*denied.*(publickey|password)." {
set output "Permission denied, wrong publickey or password."
set rc $E_CONNECT
}
-re "\[0-9]*%" {
exp_continue
}
"sftp>" {
if {\$count < \$argc} {
set arg [lindex \$argv \$count]
send "\$arg\r"
incr count
} else {
send "exit\r"
set output "Disconnected."
if {[info exists rc] != 1} {
set rc $OK
}
}
exp_continue
}
timeout {
set output "Connection timeout."
set rc $E_CONNECT
}
exp_continue
}
timeout {
set output "Connection timeout."
set rc $E_CONNECT
if {[info exists output] == 1} {
puts "\$output"
}
}
if {[info exists output] == 1} {
puts "\$output"
}
exit \$rc
EOF
}
sftp_backup() {
#Checking expect installation
check_expect=$(which expect)
if [[ ! -n $check_expect ]]
then
echo "Error: \"expect\" utility not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
# Checking config
if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
@ -788,36 +781,33 @@ sftp_backup() {
fi
# Debug info
echo -e "$(date "+%F %T") Remote: $HOST/$BPATH/$user.$DATE.tar"
echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$DATE.tar"
# Checking network connection and write permissions
echo -e "$(date "+%F %T") Checking network connection and write permissions ..."
sftmpdir="$BPATH/vst.bK76A9SUkt"
sftpc "mkdir $BPATH" > /dev/null 2>&1
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
rc=$?
if [[ "$rc" != 0 ]]
then
if [[ "$rc" != 0 ]]; then
rm -rf $tmpdir
case $rc in
$E_CONNECT) echo "Error: can't login to sftp host" | $send_mail -s "$subj" $email;;
$E_FTP) echo "Error: can't create temp folder on the sftp host" | $send_mail -s "$subj" $email;;
$E_CONNECT) echo "Error: can't login to sftp host $HOST" |\
$send_mail -s "$subj" $email;;
$E_FTP) echo "Error: can't create temp folder on sftp $HOST" |\
$send_mail -s "$subj" $email;;
esac
sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
log_event "$rc" "$EVENT"
exit "$rc"
fi
echo -e "$(date "+%F %T") Connection established"
# Checking retention
echo -e "$(date "+%F %T") Checking retention ..."
backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}' |grep "^$user\.")
backups_count=$(echo "$backup_list" | wc -l)
if [ "$backups_count" -ge "$BACKUPS" ]; then
backups_rm_number=$((backups_count - BACKUPS + 1))
for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar.*$//")
if [ -z $deprecated ]; then deprecated="$backup_date"; else deprecated="$deprecated $backup_date"; fi
echo -e "$(date "+%F %T") Roated sftp backup: $backup_date"
msg="$msg\n$(date "+%F %T") Roated sftp backup: $backup_date"
sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
@ -836,7 +826,6 @@ sftp_backup() {
sftpc "cd $BPATH" "put $user.$DATE.tar" > /dev/null 2>&1
rm -f $user.$DATE.tar
fi
echo -e "$(date "+%F %T") Upload complete"
}
echo "-- SUMMARY --"

View file

@ -34,7 +34,7 @@ rm -f $VESTA/conf/ftp.backup.conf
# Update vesta.conf
bckp=$(echo "$BACKUP_SYSTEM" |\
sed "s/,/\n/g"|\
sed "s/ftp//" |\
sed "s/^ftp$//" |\
sed "/^$/d"|\
sed ':a;N;$!ba;s/\n/,/g')
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf

1
bin/v-delete-backup-sftp-host Normal file → Executable file
View file

@ -40,7 +40,6 @@ bckp=$(echo "$BACKUP_SYSTEM" |\
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
# Logging
echo "sftp host successfulle removed from configuration"
log_event "$OK" "$EVENT"
exit