-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_ftp.sh
More file actions
executable file
·138 lines (128 loc) · 3.24 KB
/
backup_ftp.sh
File metadata and controls
executable file
·138 lines (128 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# variables
FTP_PASS=$FTP_STORAGE_PASS
REMOVE_OUTPUT=0
# help
function instruction {
cat << EOL
Description:
create a tar.gz file from a given directory,
then upload it to a ftp backup storage.
Usage: $0 [OPTIONS] -d <input_directory> -o <output_file.tar.gz> -u <ftp_user> -f <ftp_server_address>
Options:
-p <ftp_password> : or use FTP_STORAGE_PASS environment variable.
-r : remove the output tar.gz file from <output_directory> after a successful upload to the ftp server.
-h : show help
EOL
echo
}
# read options
if [[ $# -eq 0 ]]; then
instruction
exit
fi
while [[ $# -ne 0 ]]; do
case $1 in
-h)
instruction
exit 0
;;
-r)
REMOVE_OUTPUT=1
shift
;;
-d)
if [[ ! -d "$2" ]]; then
echo "Error: -d needs a directory path"
instruction
exit 1
fi
INPUT_DIR=$2
shift 2
;;
-o)
if [ -z "$2" ]; then
echo "Error: -o needs a directory path"
instruction
exit 1
fi
OUT_FILE=$2
shift 2
;;
-u)
if [ -z "$2" ]; then
echo "Error: -u needs a username"
instruction
exit 1
fi
USERNAME=$2
shift 2
;;
-f)
if [ -z "$2" ]; then
echo "Error: -f needs a ftp server address"
instruction
exit 1
fi
SERVER=$2
shift 2
;;
-p)
if [ -z "$2" ]; then
echo "Error: -p needs a ftp server password"
echo "or use FTP_STORAGE_PASS environment variable"
instruction
exit 1
fi
FTP_PASS=$2
shift 2
;;
*)
echo "Error: unexpected argument!"
instruction
exit 1
;;
esac
done
if [ -z "$INPUT_DIR" ] || [ -z "$OUT_FILE" ] || [ -z "$USERNAME" ] || [ -z "$SERVER" ]; then
echo "Error: insufficient arguments!"
instruction
exit 1
fi
# check disk space for creating the tar.gz file
INPUT_SIZE=$(du -s $INPUT_DIR | awk '{print $1}')
FREE_SPACE=$(df / | sed -n '2 p' | awk '{print $4}')
if [[ "$INPUT_SIZE" -ge "$FREE_SPACE" ]]; then
echo "Error: insufficient free disk space!"
exit 1
fi
echo "Free disk space [OK]"
# check connection to ftp server
echo 'exit' | ftp ftp://$USERNAME:$FTP_PASS@$SERVER/ > /dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to connect to ftp server"
exit 1
fi
echo "FTP server connection [OK]"
# create tar.gz file
tar -zcf $OUT_FILE $INPUT_DIR
echo "tar.gz file creation [OK]"
# upload to ftp server
OUT_FILE_PATH=$(dirname $OUT_FILE)
OUT_FILE_NAME=$(basename $OUT_FILE)
cd $OUT_FILE_PATH
echo "Uploading to ftp server..."
ftp -n $SERVER << EOL
quote USER $USERNAME
quote PASS $FTP_PASS
binary
put $OUT_FILE_NAME
quit
EOL
echo "Uploading to ftp server [OK]"
# delete tar.gz file after a successful upload
if [[ $REMOVE_OUTPUT -eq 1 ]]; then
rm -f $OUT_FILE
echo "Removing local tar.gz file [OK]"
fi
echo "Done!"