行业动态

了解最新公司动态及行业资讯

当前位置:首页>新闻中心>行业动态
全部 4100 公司动态 964 行业动态 3136

【培训】如何使用一下把事情干完文件拷贝一个地

时间:2022-09-11   访问量:2005

背景

在运维或者日常工作生活中,我们经常将一个文件拷贝到其他服务器,或者同时分发到多台服务器,甚至要求目标机器把文件放在同一个路径下服务器运维,方便的程序可以进一步调用。

遇到这些问题,我们一般的做法是使用scp或者rsync命令将文件一个一个拷贝到多台服务器上,费力又费力;大师的方法是用它来完成工作服务器运维,前提是你有技能;快速的方法是使用明天的脚本

功效展示

目前有4台机器,分别是node1、node2和node3,可以完成与其他3台机器的ssh链接。/root/test 目录下有两个文件 a.txt 和 b.txt。

[root@client test]# ls /root/test/
a.txt  b.txt
[root@client test]# 

我将文件分发到 node1、node2 和 node3 的 /root/test 并执行以下命令:

# 在/root/test目录下执行, xrsync是我的脚本
[root@client test]# xrsync a.txt b.txt 

执行分发过程:

[root@client test]# xrsync a.txt b.txt 
============ node1 ============
sending incremental file list
a.txt
sent 93 bytes  received 35 bytes  256.00 bytes/sec
total size is 2  speedup is 0.02
sending incremental file list
b.txt
sent 93 bytes  received 35 bytes  85.33 bytes/sec
total size is 2  speedup is 0.02
============ node2 ============
sending incremental file list
a.txt
sent 93 bytes  received 35 bytes  256.00 bytes/sec
total size is 2  speedup is 0.02
sending incremental file list
b.txt
sent 93 bytes  received 35 bytes  256.00 bytes/sec
total size is 2  speedup is 0.02
============ node3 ============
sending incremental file list
a.txt
sent 93 bytes  received 35 bytes  85.33 bytes/sec
total size is 2  speedup is 0.02
sending incremental file list
b.txt
sent 93 bytes  received 35 bytes  256.00 bytes/sec
total size is 2  spee

去node2看看,文件确实存在。同样,node3 和 node4 也是同步的。

# node2上查看
[root@node2 ~]# ls /root/test/
a.txt  b.txt
[root@node2 ~]# 
# node3上查看
[root@node3 ~]# ls /root/test/
a.txt  b.txt
[root@node3 ~]# 
# node4上查看
[root@node4 ~]# ls /root/test/
a.txt  b.txt
[root@node4 ~]# 

脚本

整个脚本的代码,只需要将改成自己环境的或者ip地址即可。

#!/bin/bash
# 判断参数是否足够
if [ $# -lt 1 ]
then
 echo Not Enounh Arguement!
 exit;
fi
# 遍历所有的机器
for host in node1 node2 node3
do
 echo ============  $host ============
 for file in $@
 do
  # 判断文件是否存在
  if [ -e $file ]
  then
   # 获取父目录
   pdir=$(cd -P $(dirname $file); pwd)
   # 获取当前目录的名称
   fname=$(basename $file)
   ssh $host "mkdir -p $pdir"
   rsync -av $pdir/$fname $host:$pdir
  else
   echo $file does not exists!
  fi
 done
done

运行条件

为了更方便地运行脚本,建议使用以下优化。

1.修改/etc/hosts文件,添加IP地址和主机名的对应关系,这样我们就可以直接使用主机名进行操作了。例如我演示的机器配置。

vim  /etc/hosts
# 加入配置,自己的机器对应修改
……
192.168.31.47 client
192.168.31.48 node1
192.168.31.50 node2
192.168.31.51 node3

2.客户端机器和目标机器之间使用ssh密码验证登录,这样传输文件时不需要二次验证。

# 生成ssh私钥
ssh-keygen -f /root/.ssh/id_rsa -N '' 
# 循环把公钥传递到服务器上,免密登录
for i in node1 node2 node3 
do 
  ssh-copy-id $i
done
# 根据提示输入密码

3.为脚本添加可执行权限,并配置环境变量使用全局可用。

# 把文件存储为xrsync,加上x权限
[root@client shell]# chmod +x xrsync 
[root@client shell]# 
# 配置环境变量
# 我把脚本放在/opt/shell下的,自己情况类比修改
[root@client shell]# vim /etc/profile.d/my_env.sh 
export PATH=$PATH:/opt/shell
# 配置生效,就可以在全局生效了
[root@client opt]# source /etc/profile

至此,早点完成工作,开始愉快的玩耍吧~

上一篇:it技术员 【平安二号?百日攻坚】2016年10月21日·星期四

下一篇:美国著名的华尔街就有很多金融大鳄,年薪百万

发表评论:

评论记录:

未查询到任何数据!

在线咨询

点击这里给我发消息 售前咨询专员

点击这里给我发消息 售后服务专员

在线咨询

免费通话

24小时免费咨询

请输入您的联系电话,座机请加区号

免费通话

微信扫一扫

微信联系
返回顶部