-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull
executable file
·142 lines (128 loc) · 4.15 KB
/
pull
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
139
140
141
142
#!/usr/bin/env bash
# pull <options> <user@host> <source> <file/directory>
#
# example: bash pull -e "./.ignore" [email protected] ~/Workspace /www
# example: bash pull -WS -i "file-1.txt","file-2.txt" [email protected] / /
if [ "$1" == "-h" ];
then
echo "
pull <options> <user@host> <source> <file/directory>
<options>
-i exclude list: \"file-1.txt\",\"file-2.txt\"
-e exclude file: \"./.ignore\" (default: ./.ignore)
-W upload directory starts with: /Workspace
-S source directory starts with: /www
<user@host>
example SSH user name and IPv4 address: [email protected]
<source>
source host source directory: /path/to/directory
<file/directory>
file file path: /path/to/file.ext
directory directory path: /path/to/directory
"
exit 1
fi
user_host=${@: -3:1}
source=${@: -2:1}
file_directory=${@: -1}
exclude_file=( --exclude-from="./.ignore" )
exclude_list=""
if [[ ! $source =~ ^\/ ]];
then
echo "The host source directory should start with -> / <- ($source)."
exit 1
fi
while getopts "e:i:WS" opt;
do
case $opt in
e )
[[ -f $OPTARG ]] && exclude_file=( --exclude-from=$OPTARG )
;;
i )
arr=()
IFS="," read -ra OPTS <<< "$OPTARG"
for i in "${OPTS[@]}"; do
arr+=( --exclude="$i" )
done
exclude_list=( ${arr[@]} );;
W )
file_directory="$HOME/Workspace$file_directory";;
S )
source="/www$source";;
* )
break;;
esac
done
if [[ $user_host =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];
then
echo "$user_host is valid."
else
if [[ ! $user_host =~ \@ ]]
then
echo "$user_host is not valid! @ character is missing!"
exit 1
fi
if [[ $user_host =~ ^[a-zA-Z\_\.\-]+\@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];
then
echo "$user_host is valid."
else
echo "$user_host is not valid! Allowed characters for user names [a-zA-Z\_\.\-]+. If something is missing, add your special character to the list."
exit 1
fi
fi
if [[ -d $file_directory ]];
then
echo "$file_directory is valid."
elif [[ -f $file_directory ]];
then
echo "$file_directory is valid."
else
echo "$file_directory is not valid! Seems that $file_directory does not exists."
exit 1
fi
[[ ${exclude_list[@]} == "null" ]] && exclude=${exclude_file[@]} || exclude=${exclude_list[@]}
if [[ ! $file_directory =~ \/$ ]];
then
while true; do
read -p "Are you sure to synchronize $(tput bold)$(tput setaf 1)$file_directory$(tput sgr0) with $(tput bold)$(tput setaf 1)$source$(tput sgr0) (yes/no)? " answer
case "$answer" in
yes )
exec rsync -vr ${exclude[@]} --delete $user_host:$source $file_directory
break;;
no )
echo "Ok, maybe later."
break;;
*)
echo "Please, try again (yes/no).";;
esac
done
elif [[ $file_directory =~ \/$ ]];
then
while true; do
read -p "Are you sure to synchronize $(tput bold)$(tput setaf 1)all files $files_directory$(tput sgr0) with $(tput bold)$(tput setaf 1)the directory $source$(tput sgr0) (yes/no)? " answer
case "$answer" in
yes )
exec rsync -vr ${exclude[@]} --delete $user_host:$source $file_directory
break;;
no )
echo "Ok, maybe later."
break;;
*)
echo "Please, try again (yes/no).";;
esac
done
else
while true; do
read -p "Are you sure to synchronize $(tput bold)$(tput setaf 1)the file $files_directory$(tput sgr0) with $(tput bold)$(tput setaf 1)the directory $source$(tput sgr0) (yes/no)? " answer
case "$answer" in
yes )
exec rsync -vr ${exclude[@]} --delete $user_host:$source $file_directory
break;;
no )
echo "Ok, maybe later."
break;;
*)
echo "Please, try again (yes/no).";;
esac
done
fi