-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoem_ssdb_backup
More file actions
168 lines (138 loc) · 3.69 KB
/
oem_ssdb_backup
File metadata and controls
168 lines (138 loc) · 3.69 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
#获取当前脚本所在路径
function get_cur_script_location()
{
pwd_dir=`pwd`
location_dir=$(cd `dirname $0`; pwd)
cd $pwd_dir
echo "$location_dir"
}
#import module files---
. $(get_cur_script_location)/json_function.sh
. $(get_cur_script_location)/utils.sh
#-----------------------
#---global variables-----
YEAR=`expr $(date -d "today" +"%Y") + 0`
MONTH=`expr $(date -d "today" +"%m") + 0`
TODAY=`expr $(date -d "today" +"%e") + 0`
FILE=${YEAR}-${MONTH}-${TODAY}.tar.bz2
TARGET=/usr/local/backup/$FILE
DESTINATION=/usr/local/backup
#-----------------------
function main()
{
local json_file=$(get_cur_script_location)/oem.json
if [ ! -e $json_file ];then
echo "$json_file missing" >&2
exit
fi
local json_content=`cat $json_file`
local is_valid_json=`check_json "$json_content"`
if [ $is_valid_json != "true" ];then
echo "Invalid json file[$json_file]!" >&2
exit;
fi
#检查json的成员
local member="oem"
local b_find=`has_member "$json_content" $member`
if [ $b_find != "true" ];then
echo "Can't find $member in $json_file" >&2
exit;
fi
local oem_configs=`get_value "$json_content" $member`
local oem_size=`get_length "$oem_configs"`
local oem_keys=`get_keys "$oem_configs"`
for ((i=0;i<$oem_size;i++));do
local oem_name=`get_value "$oem_keys" [$i]`
local oem_config=`get_value "$oem_configs" $oem_name`
#检查每个oem配置的成员
local members=(host port user_name password)
local check_success="true"
for tkey in ${members[*]};do
local b_check=`has_member "$oem_config" $tkey`
if [ $b_check != "true" ];then
echo "Param[$tkey] missing in oem_config[$oem_name]" >&2
check_success="false"
break
fi
done
#如果检查成员失败了 则进行下一个检查
if [ $check_success != "true" ];then
continue
fi
#获取配置的参数,检查合法性
local ssh_user=`get_value "$oem_config" user_name`
param_type=`get_value_type "$oem_config" user_name`
ssh_user=`trim "$ssh_user"`
if [ -z "$ssh_user" -o $param_type != "string" ];then
continue
fi
local ssh_password=`get_value "$oem_config" password`
param_type=`get_value_type "$oem_config" password`
ssh_password=`trim "$ssh_password"`
if [ -z "$ssh_password" -o $param_type != "string" ];then
continue
fi
local ssh_host=`get_value "$oem_config" host`
param_type=`get_value_type "$oem_config" host`
ssh_host=`trim "$ssh_host"`
if [ -z "$ssh_host" -o $param_type != "string" ];then
continue
fi
local ssh_port=`get_value "$oem_config" port`
param_type=`get_value_type "$oem_config" port`
ssh_port=`trim "$ssh_port"`
if [ -z "$ssh_port" -o $param_type != "number" ];then
continue
fi
oem_name=`echo $oem_name|sed 's/\"//g'`
ssh_user=`echo $ssh_user|sed 's/\"//g'`
ssh_password=`echo $ssh_password|sed 's/\"//g'`
ssh_host=`echo $ssh_host|sed 's/\"//g'`
ssh_port=`echo $ssh_port|sed 's/\"//g'`
#进行scp 操作
do_scp $oem_name $ssh_user $ssh_password $ssh_host $ssh_port
#echo "$oem_name $ssh_user $ssh_password $ssh_host"
done
}
#通过scp命令从 oem的服务器将数据库备份文件拷贝到本地的备份目录
#参数:
# oem_name
# ssh_user
# ssh_password
# ssh_host
# ssh_port
#返回值 void
function do_scp()
{
local oem_name=$1
local ssh_user=$2
local ssh_password=$3
local ssh_host=$4
local ssh_port=$5
local oem_dest=$DESTINATION/$oem_name
mkdir -p $oem_dest
/usr/bin/expect << EOF
set timeout -1
spawn scp -P $ssh_port $ssh_user@$ssh_host:$TARGET $oem_dest
expect {
"password:"
{
send "$ssh_password\r"
}
"密码:"
{
send "$ssh_password\r"
}
"yes/no"
{
send "yes\r"
}
}
expect "100%"
send "exit\n"
expect eof
EOF
}
#开始任务
main