Skip to content

Commit c2ccccf

Browse files
committed
tools for ftp backup
1 parent a52993f commit c2ccccf

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

bin/v-add-backup-ftp-host

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/bin/bash
2+
# info: add backup ftp host
3+
# options: HOST USERNAME PASSWORD [PATH]
4+
#
5+
# The function adds ftp host for system backups
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
host=$1
14+
ftp_user=$2
15+
ftp_password=$3
16+
ftp_path=${4-/backup}
17+
A3='******'
18+
19+
# Includes
20+
source $VESTA/conf/vesta.conf
21+
source $VESTA/func/main.sh
22+
23+
# Defining ftp command function
24+
ftpc() {
25+
ftp -n $host <<EOF
26+
quote USER $ftp_user
27+
quote PASS $ftp_password
28+
binary
29+
$1
30+
$2
31+
$3
32+
quit
33+
EOF
34+
}
35+
36+
37+
#----------------------------------------------------------#
38+
# Verifications #
39+
#----------------------------------------------------------#
40+
41+
check_args '3' "$#" "HOST USERNAME PASSWORD [PATH]"
42+
validate_format 'host' 'ftp_user' 'ftp_password'
43+
44+
45+
#----------------------------------------------------------#
46+
# Action #
47+
#----------------------------------------------------------#
48+
49+
# Checking network connection
50+
if [ ! -z "$(ftpc)" ]; then
51+
echo "Error: can't login to ftp"
52+
log_event "$E_FTP" "$EVENT"
53+
exit $E_FTP
54+
fi
55+
56+
# Checking write permissions
57+
ftpc "mkdir $ftp_path" > /dev/null 2>&1
58+
ftmpdir=$(mktemp -u -p "$ftp_path")
59+
ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir")
60+
if [ ! -z "$ftp_result" ] ; then
61+
rm -rf $tmpdir
62+
echo "Error: can't create temp folder on the ftp"
63+
log_event "$E_FTP" "$EVENT"
64+
exit $E_FTP
65+
fi
66+
67+
# Adding backup host
68+
echo "HOST='$host'
69+
USERNAME='$ftp_user'
70+
PASSWORD='$ftp_password'
71+
TIME='$TIME'
72+
DATE='$DATE'" > $VESTA/conf/ftp.backup.conf
73+
chmod 660 $VESTA/conf/ftp.backup.conf
74+
75+
76+
#----------------------------------------------------------#
77+
# Vesta #
78+
#----------------------------------------------------------#
79+
80+
# Update vesta.conf
81+
if [ -z "$(grep LANGUAGE $VESTA/conf/vesta.conf)" ]; then
82+
echo "BACKUP_SYSTEM='ftp'" >> $VESTA/conf/vesta.conf
83+
else
84+
bckp=$(echo "$BACKUP_SYSTEM,ftp" |\
85+
sed -e "s/,/\n/g"|\
86+
sort -r -u |\
87+
sed -e "/^$/d"|\
88+
sed -e ':a;N;$!ba;s/\n/,/g')
89+
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
90+
fi
91+
92+
# Logging
93+
log_event "$OK" "$EVENT"
94+
95+
exit

bin/v-delete-backup-ftp-host

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
# info: delete backup ftp server
3+
# options: NONE
4+
#
5+
# The function deletes ftp backup host
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Includes
13+
source $VESTA/conf/vesta.conf
14+
source $VESTA/func/main.sh
15+
16+
17+
#----------------------------------------------------------#
18+
# Verifications #
19+
#----------------------------------------------------------#
20+
21+
22+
#----------------------------------------------------------#
23+
# Action #
24+
#----------------------------------------------------------#
25+
26+
# Checking network connection
27+
rm -f $VESTA/conf/ftp.backup.conf
28+
29+
30+
#----------------------------------------------------------#
31+
# Vesta #
32+
#----------------------------------------------------------#
33+
34+
# Update vesta.conf
35+
bckp=$(echo "$BACKUP_SYSTEM" |\
36+
sed -e "s/,/\n/g"|\
37+
sed -e "s/ftp//" |\
38+
sed -e "/^$/d"|\
39+
sed -e ':a;N;$!ba;s/\n/,/g')
40+
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $VESTA/conf/vesta.conf
41+
42+
# Logging
43+
log_event "$OK" "$EVENT"
44+
45+
exit

bin/v-list-backup-ftp-host

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
# info: list backup host
3+
# options: [FORMAT]
4+
#
5+
# The function for obtaining the list of back fto host.
6+
7+
8+
#----------------------------------------------------------#
9+
# Variable&Function #
10+
#----------------------------------------------------------#
11+
12+
# Argument defenition
13+
format=${1-shell}
14+
15+
# Includes
16+
source $VESTA/func/main.sh
17+
18+
# Json function
19+
json_list_ftp_host() {
20+
i=1
21+
fileds_count=$(echo "$fields" | wc -w)
22+
ip_data=$(cat $VESTA/conf/ftp.backup.conf)
23+
echo '{'
24+
eval $ip_data
25+
for field in $fields; do
26+
eval value=$field
27+
if [ $i -eq 1 ]; then
28+
echo -e "\t\"$value\": {"
29+
else
30+
if [ $fileds_count -eq $i ]; then
31+
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\""
32+
else
33+
echo -e "\t\t\"${field//$/}\": \"${value//,/, }\","
34+
fi
35+
fi
36+
(( ++i))
37+
done
38+
if [ -n "$value" ]; then
39+
echo -e ' }'
40+
fi
41+
echo -e '}'
42+
}
43+
44+
# Shell function
45+
shell_list_ftp_host() {
46+
line=$(cat $VESTA/conf/ftp.backup.conf)
47+
eval $line
48+
for field in $fields; do
49+
eval key="$field"
50+
if [ -z "$key" ]; then
51+
key='NULL'
52+
fi
53+
echo "${field//$/}: $key "
54+
done
55+
}
56+
57+
58+
#----------------------------------------------------------#
59+
# Verifications #
60+
#----------------------------------------------------------#
61+
62+
63+
#----------------------------------------------------------#
64+
# Action #
65+
#----------------------------------------------------------#
66+
67+
if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then
68+
exit
69+
fi
70+
71+
# Defining fileds to select
72+
fields='$HOST $USERNAME $TIME $DATE'
73+
74+
# Listing database
75+
case $format in
76+
json) json_list_ftp_host ;;
77+
plain) nohead=1; shell_list_ftp_host;;
78+
shell) shell_list_ftp_host | column -t ;;
79+
*) check_args '2' '0' '[FORMAT]'
80+
esac
81+
82+
83+
#----------------------------------------------------------#
84+
# Vesta #
85+
#----------------------------------------------------------#
86+
87+
exit

0 commit comments

Comments
 (0)