union calls for FTP/SFTP backups

This commit is contained in:
Serghey Rodin 2015-05-29 01:45:15 +03:00
parent 7a9c937ca1
commit 003cbbea47
5 changed files with 106 additions and 206 deletions

View file

@ -1,106 +0,0 @@
#!/bin/bash
# info: add backup ftp host
# options: HOST USERNAME PASSWORD [PATH] [PORT]
#
# The function adds ftp host for system backups
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Argument defenition
host=$1
ftp_user=$2
password=$3
ftp_path=${4-/backup}
ftp_port=${5-21}
# 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 $password
binary
$1
$2
$3
quit
EOF
}
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]"
validate_format 'host' 'ftp_user'
is_password_valid
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Checking network connection
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 server"
log_event "$E_CONNECT" "$EVENT"
exit $E_CONNECT
fi
# Checking write permissions
ftpc "mkdir $ftp_path" > /dev/null 2>&1
ftmpdir="$ftp_path/vst.bK76A9SUkt"
ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir"|grep -v Trying)
if [ ! -z "$ftp_result" ] ; then
echo "$ftp_result"
rm -rf $tmpdir
echo "Error: can't create temp folder on the ftp"
log_event "$E_FTP" "$EVENT"
exit $E_FTP
fi
# Adding backup host
echo "HOST='$host'
USERNAME='$ftp_user'
PASSWORD='$password'
BPATH='$ftp_path'
PORT='$ftp_port'
TIME='$TIME'
DATE='$DATE'" > $VESTA/conf/ftp.backup.conf
chmod 660 $VESTA/conf/ftp.backup.conf
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Update vesta.conf
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
else
bckp=$(echo "$BACKUP_SYSTEM,ftp" |\
sed "s/,/\n/g"|\
sort -r -u |\
sed "/^$/d"|\
sed ':a;N;$!ba;s/\n/,/g')
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
fi
# Logging
log_event "$OK" "$EVENT"
exit

View file

@ -1,34 +1,49 @@
#!/bin/bash #!/bin/bash
# info: add backup sftp host # info: add backup host
# options: HOST USERNAME PASSWORD [PATH] [PORT] # options: TYPE HOST USERNAME PASSWORD [PATH] [PORT]
# #
# The function adds sftp host for system backups # The function adds backup host
#----------------------------------------------------------# #----------------------------------------------------------#
# Variable&Function # # Variable&Function #
#----------------------------------------------------------# #----------------------------------------------------------#
# Argument defenition # Argument defenition
sftp_host=$1 type=$1
sftp_user=$2 host=$2
password=$3 user=$3
sftp_path=${4-backup} password=$4
sftp_port=${5-22} path=${5-/backup}
port=$6
# Includes # Includes
source $VESTA/func/main.sh source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf source $VESTA/conf/vesta.conf
# Hiding password # Hiding password
A3='******' A4='******'
EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9" EVENT="$DATE $TIME $SCRIPT $A1 $A2 $A3 $A4 $A5 $A6 $A7 $A8 $A9"
# sftp command function # Defining ftp command function
ftpc() {
ftp -p -n $host $port <<EOF
quote USER $user
quote PASS $password
binary
$1
$2
$3
quit
EOF
}
# Defining sftp command function
sftpc() { sftpc() {
expect -f "-" <<EOF "$@" expect -f "-" <<EOF "$@"
set count 0 set count 0
spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o \ spawn /usr/bin/sftp -o StrictHostKeyChecking=no -o \
Port=$sftp_port $sftp_user@$sftp_host Port=$port $user@$host
expect { expect {
"password:" { "password:" {
send "$password\r" send "$password\r"
@ -81,42 +96,78 @@ EOF
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH] [PORT]" check_args '4' "$#" "TYPE HOST USERNAME PASSWORD [PATH] [PORT]"
which expect >/dev/null 2>&1 validate_format 'host' 'user'
if [ $? -ne 0 ];then
echo "Error: expect utility not found"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
is_password_valid is_password_valid
if [ "$type" = 'sftp' ]; then
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
fi
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
# Checking network connection and write permissions # Checking network connection
sftmpdir="$sftp_path/vst.bK76A9SUkt" if [ "$type" = 'ftp' ]; then
sftpc "mkdir $sftp_path" > /dev/null 2>&1 if [ -z $port ]; then
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1 port=21
rc=$? fi
if [[ "$rc" != 0 ]]; then fconn=$(ftpc 2>&1)
case $rc in ferror=$(echo $fconn |\
$E_CONNECT) echo "Error: can't login to sftp host";; grep -i -e failed -e error -e "can't" -e "not conn" -e "incorrect")
$E_FTP) echo "Error: can't create temp folder on the sftp host";; if [ ! -z "$ferror" ]; then
esac echo "Error: can't login to ftp server"
log_event "$rc" "$EVENT" log_event "$E_CONNECT" "$EVENT"
exit "$rc" exit $E_CONNECT
fi
# Checking write permissions
ftpc "mkdir $path" > /dev/null 2>&1
ftmpdir="$path/vst.bK76A9SUkt"
ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir"|grep -v Trying)
if [ ! -z "$ftp_result" ] ; then
echo "$ftp_result"
rm -rf $tmpdir
echo "Error: can't create $ftmpdir folder on the ftp"
log_event "$E_FTP" "$EVENT"
exit $E_FTP
fi
fi
if [ "$type" = 'sftp' ]; then
if [ -z $port ]; then
port=22
fi
sftmpdir="$sftp_path/vst.bK76A9SUkt"
sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
rc=$?
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"
fi
fi fi
# Adding sftp backup config file
echo "HOST='$sftp_host' # Adding backup host
USERNAME='$sftp_user' if [ $type != 'local' ]; then
PASSWORD='$password' echo "HOST='$host'
BPATH='$sftp_path' USERNAME='$user'
PORT='$sftp_port' PASSWORD='$password'
TIME='$TIME' BPATH='$path'
DATE='$DATE'" > $VESTA/conf/sftp.backup.conf PORT='$port'
chmod 660 $VESTA/conf/sftp.backup.conf TIME='$TIME'
DATE='$DATE'" > $VESTA/conf/$type.backup.conf
chmod 660 $VESTA/conf/$type.backup.conf
fi
#----------------------------------------------------------# #----------------------------------------------------------#
@ -125,9 +176,9 @@ chmod 660 $VESTA/conf/sftp.backup.conf
# Update vesta.conf # Update vesta.conf
if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then if [ -z "$(grep BACKUP_SYSTEM $VESTA/conf/vesta.conf)" ]; then
echo "BACKUP_SYSTEM='sftp'" >> $VESTA/conf/vesta.conf echo "BACKUP_SYSTEM='$type'" >> $VESTA/conf/vesta.conf
else else
bckp=$(echo "$BACKUP_SYSTEM,sftp" |\ bckp=$(echo "$BACKUP_SYSTEM,$type" |\
sed "s/,/\n/g"|\ sed "s/,/\n/g"|\
sort -r -u |\ sort -r -u |\
sed "/^$/d"|\ sed "/^$/d"|\

View file

@ -1,45 +0,0 @@
#!/bin/bash
# info: delete backup sftp server
# options: NONE
#
# The function deletes sftp backup host
#----------------------------------------------------------#
# Variable&Function #
#----------------------------------------------------------#
# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Delete configuration file
rm -f $VESTA/conf/sftp.backup.conf
#----------------------------------------------------------#
# Vesta #
#----------------------------------------------------------#
# Update vesta.conf
bckp=$(echo "$BACKUP_SYSTEM" |\
sed "s/,/\n/g"|\
sed "s/sftp//" |\
sed "/^$/d"|\
sed ':a;N;$!ba;s/\n/,/g')
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
# Logging
log_event "$OK" "$EVENT"
exit

View file

@ -1,8 +1,8 @@
#!/bin/bash #!/bin/bash
# info: list backup host # info: list backup host
# options: [FORMAT] # options: TYPE [FORMAT]
# #
# The function for obtaining the list of back fto host. # The function for obtaining the list of backup host parameters.
#----------------------------------------------------------# #----------------------------------------------------------#
@ -10,7 +10,8 @@
#----------------------------------------------------------# #----------------------------------------------------------#
# Argument defenition # Argument defenition
format=${1-shell} TYPE=$1
format=${2-shell}
# Includes # Includes
source $VESTA/func/main.sh source $VESTA/func/main.sh
@ -19,19 +20,16 @@ source $VESTA/func/main.sh
json_list_ftp_host() { json_list_ftp_host() {
i=1 i=1
fileds_count=$(echo "$fields" | wc -w) fileds_count=$(echo "$fields" | wc -w)
ip_data=$(cat $VESTA/conf/ftp.backup.conf) ip_data=$(cat $VESTA/conf/$TYPE.backup.conf)
echo '{' echo '{'
echo -e "\t\"$TYPE\": {"
eval $ip_data eval $ip_data
for field in $fields; do for field in $fields; do
eval value=$field eval value=$field
if [ $i -eq 1 ]; then if [ $fileds_count -eq $i ]; then
echo -e "\t\"$value\": {" echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
else else
if [ $fileds_count -eq $i ]; then echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
else
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
fi
fi fi
(( ++i)) (( ++i))
done done
@ -43,7 +41,7 @@ json_list_ftp_host() {
# Shell function # Shell function
shell_list_ftp_host() { shell_list_ftp_host() {
line=$(cat $VESTA/conf/ftp.backup.conf) line=$(cat $VESTA/conf/$TYPE.backup.conf)
eval $line eval $line
for field in $fields; do for field in $fields; do
eval key="$field" eval key="$field"
@ -59,17 +57,19 @@ shell_list_ftp_host() {
# Verifications # # Verifications #
#----------------------------------------------------------# #----------------------------------------------------------#
check_args '1' "$#" 'TYPE [FORMAT]'
#----------------------------------------------------------# #----------------------------------------------------------#
# Action # # Action #
#----------------------------------------------------------# #----------------------------------------------------------#
if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then if [ ! -e "$VESTA/conf/$TYPE.backup.conf" ]; then
exit exit
fi fi
# Defining fileds to select # Defining fileds to select
fields='$HOST $USERNAME $PORT $TIME $DATE' fields='$HOST $USERNAME $PORT $TYPE $BPATH $TIME $DATE'
# Listing database # Listing database
case $format in case $format in