-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirrorsync
executable file
·161 lines (141 loc) · 3.57 KB
/
mirrorsync
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
# mirror-sync: a script make you sync file mirrors easily
# ==============================
# Document For Config File:
# ==============================
# The config file uses same syntax as shell script,
# declare -a MIRRORSYNC_SERVERS[]=("<names of your servers>")
# MIRRORSYNC_SERVER_<name>_CMDLINE=""
#
# Available variables:
# QUIET (int) -q is set
# VERBOSE (int) -v is set (more -v makes $VERBOSE bigger)
# DIR (string) -d is set or using default value
set -e
SCRIPT_NAME="mirrorsync"
AURTHOR="Neo_Chen <chenkolei@gmail.com>"
LICENSE="Public Domain"
# usage() is important
usage()
{
echo "Usage: $SCRIPT_NAME [-v] [-f <configure file>] [-d] [-q] [-o <variable>=<value>] [-h]"
echo " -v Be verbose, one more -v gives more details"
echo " -f Use alternative configfile instead of ~/.mirrorsync"
echo " -d Sync with different directory from .mirrorsync"
echo " -q Make this script really quiet"
echo " -o Add your own variable"
echo " -h Displays this message"
echo " Made by ${AURTHOR}, licensed under ${LICENSE}"
}
# Set default variables
declare -i VERBOSE=0
declare -i QUIET=0
declare CONFIG="${HOME}/.neoscriptrc"
declare DIR="${HOME}/HTML/"
declare -i COUNT=0
# Set $IFS for reading -o argument
IFS='='
# Get command line aruguments
while getopts "f:d:o:qvh" OPTION ; do
case $OPTION in
h) usage && exit 1 ;;
f) CONFIG="$OPTARG" ;;
v) ((++VERBOSE)) ;;
d) DIR="$OPTARG" ;;
q) VERBOSE=0
QUIET=1 ;;
o) read -r name value <<<"$OPTARG"
declare "$name"="$value";;
esac
done
IFS=$'\n'
# ===============Colors=============== #
ESC="\033"
RESET="${ESC}[0m" #Reset all attributes
BRIGHT="${ESC}[1m" #Bright
DIM="${ESC}[2m" #Dim
BLINK="${ESC}[5m" #Blink
# Foreground Colours #
FBLACK="${ESC}[30m" #Black
FRED="${ESC}[31m" #Red
FGREEN="${ESC}[32m" #Green
FYELLOW="${ESC}[33m" #Yellow
FBLUE="${ESC}[34m" #Blue
FMAGENTA="${ESC}[35m" #Magenta
FCYAN="${ESC}[36m" #Cyan
FWHITE="${ESC}[37m" #White
# Background Colours #
BBLACK="${ESC}[40m" #Black
BRED="${ESC}[41m" #Red
BGREEN="${ESC}[42m" #Green
BYELLOW="${ESC}[43m" #Yellow
BBLUE="${ESC}[44m" #Blue
BMAGENTA="${ESC}[45m" #Magenta
BCYAN="${ESC}[46m" #Cyan
BWHITE="${ESC}[47m" #White
# Read the config file
source "$CONFIG"
# Normal Message
msg_echo()
{
if [ "$QUIET" -lt 1 ] ;then
echo -e "${BBLUE}>>${RESET} ${FCYAN}${1}${RESET}"
fi
}
# Debug Level Verbose
verbose2_echo()
{
if [ "$VERBOSE" -ge 2 ] ;then
echo -e "${BRIGHT}${BBLUE}>>${RESET} ${BRIGHT}${FGREEN}${1}${RESET}"
fi
}
# Verbose Message
verbose_echo()
{
if [ "$VERBOSE" -ge 1 ] ;then
echo -e "${BRIGHT}${BBLUE}>>${RESET} ${BRIGHT}${FCYAN}${1}${RESET}"
fi
}
is_set()
{
local name
local value
name="$1"
value="$2"
if test -z "${value}" ;then
msg_echo "${name} is not set"
return 2
fi
}
expand_variables()
{
verbose2_echo "expand_variables: $*"
declare -g exp_name
declare -g exp_cmdline
local VAR
count="$1"
verbose2_echo "$count"
exp_name="${MIRRORSYNC_SERVERS[${count}]}"
verbose2_echo "exp_name=$exp_name"
VAR="MIRRORSYNC_SERVER_${exp_name}_CMDLINE"
verbose2_echo "VAR=$VAR"
exp_cmdline="${!VAR}"
verbose2_echo "exp_cmdline=$exp_cmdline"
is_set "\$MIRRORSYNC_SERVERS[${count}]" "$exp_name"
is_set "\$MIRRORSYNC_SERVER_${exp_name}_CMDLINE" "$exp_cmdline"
}
run_server()
{
verbose2_echo "run_server: $*"
local -i count
eval "$1"
}
# Check files are exist
test -f "$CONFIG"
while [ "$(( ${#MIRRORSYNC_SERVERS[@]} - 1 ))" -ge "$COUNT" ] ; do
expand_variables "$COUNT"
msg_echo "#[${count}]\t\"${exp_name}\", with commandline \"$exp_cmdline\""
run_server "$exp_cmdline"
(( ++COUNT ))
done
# vim: set tabstop=8:softtabstop=8:shiftwidth=8