0%

批量替换文件脚本

任务要求

步骤一:【替换文件】log4j.properties文件拷贝覆盖到:/opt/flume/conf/log4j.properties
步骤二:【替换文件】run.sh文件拷贝覆盖到:/opt/flume/bin/run.sh
步骤三:【删除文件】进入/opt/plugins.d/vipkid/lib目录,如果存在就删除flume-ext-1.0-SNAPSHOT.jar
步骤四:【替换文件】flume-ext-1.1-SNAPSHOT.jar文件拷贝覆盖到:/opt/flume/plugins.d/vipkid/lib/flume-ext-1.1-SNAPSHOT.jar
其他:替换的文件所属用户为flume用户

ansible实现

ansible脚本的思路是先备份,然后执行替换删除操作。

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
---
- hosts: commonservers
tasks:
- name: get the time stamp
shell: date +%Y%m%d%H%M%S.%N
register: now_time
- name: backup log4j.properties
shell: cp /opt/flume/conf/log4j.properties{,.{{now_time.stdout}}}
- name: backup run.sh
shell: cp /opt/flume/bin/run.sh{,.{{now_time.stdout}}}
- name: backup flume-ext-1.0-SNAPSHOT.jar
shell: cp /opt/flume/plugins.d/vipkid/lib/flume-ext-1.0-SNAPSHOT.jar{,.{{now_time.stdout}}}
- name: backup flume-ext-1.1-SNAPSHOT.jar
shell: cp /opt/flume/plugins.d/vipkid/lib/flume-ext-1.1-SNAPSHOT.jar{,.{{now_time.stdout}}}
- name: replace log4j.properties
copy:
src: log4j.properties
dest: /opt/flume/conf/log4j.properties
owner: flume
force: yes
- name: replace run.sh
copy:
src: run.sh
dest: /opt/flume/bin/run.sh
owner: flume
force: yes
- name: remove flume-ext-1.0-SNAPSHOT.jar
shell: rm -f /opt/flume/plugins.d/vipkid/lib/flume-ext-1.0-SNAPSHOT.jar
- name: replace flume-ext-1.1-SNAPSHOT.jar
copy:
src: flume-ext-1.1-SNAPSHOT.jar
dest: /opt/flume/plugins.d/vipkid/lib/flume-ext-1.1-SNAPSHOT.jar
owner: flume
force: yes

shell脚本实现

shell脚本的思路是在A机器上执行batch-main.sh批量处理脚本,读取主机列表,把新文件和replace.sh脚本拷贝到所有B机器上。然后batch-main.sh调用执行所有B机器上的replace.sh脚本,replace.sh脚本负责备份和执行替换删除操作。

batch-main.sh内容为:

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
#!/bin/bash
# zip
mkdir newfile
cp /opt/flume/conf/log4j.properties newfile/
cp /opt/flume/bin/run.sh newfile/
cp /opt/flume/plugins.d/vipkid/lib/flume-ext-1.1-SNAPSHOT.jar newfile/
tar -czvf newfile.tar newfile &&

# scp
array_str=`cat hosts`
IPS=' '
array=($array_str)
user=`whoami`

echo 'scp start'
for data in ${array[@]}
do
echo 'host: '$data
scp newfile.tar $user@$data:~/
scp replace.sh $user@$data:~/
ssh $user@$data '/bin/tar -xvf ~/newfile.tar'
done &&
echo 'scp finished'

# replace
echo 'replace start'
for data in ${array[@]}
do
echo 'host: '$data
ssh $user@$data 'sudo /bin/chown flume -R ~/newfile'
ssh $user@$data '/bin/bash ~/replace.sh'
done
echo 'repalce finished'

replace.sh内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# backup
now_time=`date +%Y%m%d%H%M%S.%N`
sudo cp /opt/flume/conf/log4j.properties{,.$now_time}
sudo cp /opt/flume/bin/run.sh{,.$now_time}
if [ ! -f "/opt/flume/plugins.d/vipkid/lib/flume-ext-1.0-SNAPSHOT.jar" ];then
echo "flume-ext-1.0-SNAPSHOT.jar不存在"
else
sudo cp /opt/flume/plugins.d/vipkid/lib/flume-ext-1.0-SNAPSHOT.jar{,.$now_time}
fi
sudo cp /opt/flume/plugins.d/vipkid/lib/flume-ext-1.1-SNAPSHOT.jar{,.$now_time}

# operation
sudo cp -f ~/newfile/log4j.properties /opt/flume/conf/log4j.properties
sudo cp -r ~/newfile/run.sh /opt/flume/bin/run.sh
sudo rm -f /opt/plugins.d/vipkid/lib/flume-ext-1.0-SNAPSHOT.jar
sudo cp -f ~/newfile/flume-ext-1.1-SNAPSHOT.jar /opt/flume/plugins.d/vipkid/lib/flume-ext-1.1-SNAPSHOT.jar

hosts内容为:

1
2
3
4
l-newman-server1.ops.beta.ali.dm
l-newman-server2.ops.beta.ali.dm
l-newman-server3.ops.beta.ali.dm
l-newman-server4.ops.beta.ali.dm