10.28 rsync工具介绍
1. 安装 rsyns命令:
[root@hostname ~]# yum -y install rsync
[root@hostname02 ~]# yum -y install rsync
2. 拷贝 原文件(passwd),到指定目录(tmp)下,并重命名为1.txt :
[root@hostname ~]# rsync -av /etc/passwd /tmp/1.txt
3. 拷贝 A机器原文件(passwd),到B机器远程用户(/tmp)下,并重命名为1.txt :
准备两个机器:A机器panyu 192.168.224.128 B机器panyu01 192.168.224.129
rsync -av 原文件目录/原文件 远程用户@远程ip:目标目录/自定义目标文件名
[root@hostname ~]#rsync -av /etc/passwd root@192.168.224.128:/tmp/1.txt #当前A目录文件传送给B192.168.224.128:/tmp/1.txt
[root@hostname ~]#rsync -av [email protected]:/tmp/1.txt /tmp/passwd
10.29 rsync常用选项(上)
rsync常用选项:
-a 包含这些选项: -r t p l g o D
-v 同步过程可视化
-L 同步软链接文件时,会把软链接文件指向的源文件给同步到目标目录
-r 同步目录时要加上,类似cp时的-r选项
-t 保持文件的时间属性
-p 保持文件的权限属性
-l 保留软连接(-a中包含-l)
-g 保持文件的属组
-o 保持文件的属主
-D 保持设备文件信息扫描二维码关注公众号,回复: 3194785 查看本文章
--delte 删除目标目录(DEST)中,原目录(SRC)没有的文件
--exclude 过滤掉指定文件(如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步)|
-P 显示同步过程,比如速率/进程/百分比传输速度(比-v更加详细)
-u 加上该选项后,如果目标目录(DEST)中的文件,比原目录(SRC)新,则不同步
-z 传输时压缩(同步到目标目录自动解压,这个参数用于减少带宽的!)
10.30 rsync常用选项(下)
1. -av 参数 同步目录,并重命名 :-a 包含这些选项: -r t p l g o D
[root@hostname ~]# rsync -av 原目录 目标目录 #复制
[root@hostname ~]# rsync -av /root/mulu/ /tmp/mulu_dest/
2. -L 参数 同步目录,并重命名;同时还要把原目录下软链接文件指向的源文件拷贝到目标目录 :-L 同步软链接文件时,会把软链接文件指向的源文件给同步到目标目录下
[root@hostname ~]# rsync -avL 原目录 目标目录
[root@hostname ~]# rsync -avL /root/mulu/ /tmp/mulu_dest/
3. --delte 参数 删除目标目录中,原目录没有的文件 :
[root@hostname ~]# rsync -avL --delete /root/mulu/ /tmp/mulu_dest/ #目标文件有的文件才更新
4. --exclude 参数 同步目录,过滤掉不同步指定文件类型(过滤掉.txt结尾的文件 不同步):
--exclude 过滤掉指定文件(如--exclude “txt”会把文件名包含logs的文件或者目录过滤掉,不同步的意思)
[root@hostname ~]# rsync -avL --exclude "*.txt" /root/mulu/ /tmp/mulu_dest/
5. --exclude 参数 过滤多个指定的文件类型(过滤以.txt结尾和过滤掉logs开头的文件 不同步):
[root@hostname ~]# rsync -avL --exclude "*.txt" --exclude "*logs" /root/mulu/ /tmp/mulu_dest/
6. -P 参数 同步目录,同时可视化打印出速率进程百分比 :
-P 显示同步过程,比如速率进程百分比传输速度(比-v更加详细)
[root@hostname ~]# rsync -avP /root/mulu/ /tmp/mulu_dest/
7. -u 参数 如果目标目录中的文件比原目录新(更改的日期新),则不同步 确定下系统时间同步:
[root@hostname ~]# rsync -avPu /root/mulu/ /tmp/mulu_dest/
8. -z 参数 同步传输时压缩 :
[root@hostname ~]# rsync -avPz /root/mulu/ /tmp/mulu_dest/
10.31 rsync通过ssh同步
1. 拷贝 原文件(passwd),到指定目录(tmp)下,并重命名为1.txt :
[root@hostname ~]# rsync -av /etc/passwd /tmp/1.txt
2. 拷贝 原文件(passwd),到指定远程用户目录(tmp)下,并重命名为1.txt :
rsync -av 本地目录/本地原文件 远程用户@远程ip:远程目录/远程重命名目标文件
[root@hostname ~]# rsync -av /etc/passwd root@192.168.223.129:/tmp/1.txt
3. 拷贝 远程用户原文件(passwd),到本地目录(tmp)下,并重命名为1.txt :
rsync -av 指定用户@远程ip:远程目录/远程原文件 本地目录/本地重命名目标文件
[root@hostname ~]# rsync -av root@192.168.223.129:/etc/passwd /tmp/1.txt
4. 指定远程用户端口(-e "ssh -p 22" 指定远程用户ip端口) :
rsync -av -e "ssh -p 指定端口" 指定用户@远程ip:远程目录/远程原文件 /本地目录/本地重命名目标文件
[root@hostname ~]# rsync -av -e "ssh -p 22" root@192.168.223.128:/etc/passwd /tmp/1.txt
远程 指定 ip端口:
[root@hostname ~]# ssh -p 端口 ip
[root@hostname ~]# ssh -p 22 192.168.224.129
[root@hostname ~]# ssh -p 22 root@192.164.223.129
10.32 rsync通过服务同步(上)
• rsync 通过服务的方式同步:
在 A机器上操作:
[root@hostname ~]# mkdir /tmp/rsync #1. 创建 rsync目录
[root@hostname ~]# chmod 777 /tmp/rsync #2.设置 rsync目录权限 为777
[root@hostname ~]# vim /etc/rsyncd.conf #3. 创建 rsync配置文件
最下面另起行——插入rsyncd.conf配置文件详解:
port=873 #port=873 自定义的是A(panyu01)的端口(默认是873端口)
log file=/var/log/rsync.log #自定义(指定)日志文件(自定义绝对路径)
pid file=/var/run/rsyncd.pid #自定义(指定)pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作
address=192.168.224.129 #address=192.168.224.129 定义的是A机器的ip;自定义(指定)启动rsyncd服务的IP
#(假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动)
[panyu01-a] #[panyu01-a] 模块名字,指向的是/tmp/rsync目录
path=/tmp/rsync #path=/tmp/rsync 定义的是数据存放传输文件保存目录(panyu01-a模块指向的路径)
use chroot=true #use chroot true如果同步软链接,则会被拦截;use chroot false如果同步软链接,不会被拦截!!
#表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。(默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。)
max connections=4 #指定最大同时连接数,(默认是0,即没有限制)
read only=no #如果为yes,则不能上传到该模块指定的路径下(read only=no可以上传)
list=true #表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定: true列出,false隐藏。
uid=root #uid/gid指定传输文件时,以哪个用户或组的身份传输
gid=root
#auth users=panyu # 定义用户名,这里暂时注释掉了 指定传输时要使用的用户名。
#secrets file=/etc/rsyncd.passwd #secrets file=/etc/rsyncd.passwd 定义密码文件的路径(需要创建密码文件),该参数连同上面的参数如果不指定,则不使用密码验证,设置auth users和secrets file后,客户端连服务端也需要用户名密码了(若想在命令行中带上密码,可以设定一个密码文件)
hosts allow=192.168.224.128 #定义的是B(panyu)机器的ip,表示被允许连接该模块的主机,可以是IP或者ip网段(如果是多个IP,中间用空格隔开)
[root@hostname ~]# rsync --daemon #4. 启动 rsync服务
[root@hostname ~]# ps aux |grep rsync #5. 搜素 rsync服务,是否启动?
在 B机器上操作:
[root@hostname1 ~]# ping 192.168.223.129 #6. 检查是否ping通hao1网卡ip :
[root@hostname1 ~]# yum install -y telnet #7. 安装 telnet命令 A机器也安装下
[root@hostname1 ~]# telnet 192.168.223.129 873 #8. 检查 A机器 ip端口 是否 ping通?
[root@hostname ~]# systemctl stop firewalld #9. 如果不通,A机器,和B机器,把firewalld服务关掉 、iptables -F也清除
[root@hostname1 ~]# systemctl stop firewalld
10. 推送 本地文件到远程 :
(把hao2的文件,推送到远程hao1模块对应的目录下:把B机器上的文件,传送给远程A机器—保存在模块对应的目录(/tmp/rsync)下)
rsync -avP 本地推送文件 远程ip::模块名/重命名文件
[root@hostname1 ~]# rsync -avP /tmp/1.txt 192.168.224.129::panyu01-a/222.txt
11. 拉 远程文件到本地 :
(把hostname模块对应目录下文件,拉到本地panyu01-a的指定目录下,并重命名)
rsync -avP 远程ip::模块名/远程拉的原文件 本地存放目录/重命名文件
[root@hostname1 ~]# rsync -avP 192.168.224.129::panyu01-a/222.txt /tmp/123.txt
12. A机器上,查看rsync操作日志 :
[root@hostname ~]# cat /var/log/rsync.log
10.33 rsync通过服务同步(下)
1. 更改 rsync配置文件 :
[root@hostname ~]# vim /etc/rsyncd.conf #(把默认端口号873更改为8730)更改内容:port=8730
2. 重启 killall服务 :关闭rsync服务:
[root@hostname ~]# killall rsync #yum install -y psmisc
[root@hostname ~]# rsync --daemon #开启rsync服务:
3. 在B机器上,同步A机器文件,需要指定端口号了:
rsync -avP 本地推送文件 -port hostname端口号 远程ip::模块名/重命名文件
[root@hostname ~]# rsync -avP /tmp/123.txt -port 8730 192.168.224.129::panyu01-a/123.txt.bak
4. 查看 可用模块 :
( list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定: true列出,false隐藏。列出list=true 或隐藏 list=false )
[root@hostname ~]# rsync -port 8730 192.168.224.129::
5. 修改 rsyncd.conf配置文件 :
[root@hostname ~]# vim /etc/rsyncd.conf
编辑:消除设定密码的两行行首 #(注释符号) auth users=自定义用户名 secrets file=自定义密码文件路径
6. 创建 密码文件 :
[root@hostname ~]# /etc/rsyncd.passwd #添加格式: 自定义用户名:自定义密码
7. 设置 密码文件权限为600 :
[root@hostname ~]# chmod 600 /etc/rsyncd.passwd
8. 客户端B机器上操作,推送本地文件到远程下:
rsync -avP 本地推送文件 --port=端口号 密码对应的用户名@远程ip::模块名/重命名文件
[root@hao-02 ~]# rsync -avP /tmp/1.txt --port=8730 panyu001@192.168.223.128::panyu01-a/444.txt
9. 客户端B机器上操作,创建 密码文件 :
[root@hao-02 ~]# vi /etc/rsync_pass.txt
添加内容:A机器rsync_pass.txt密码文件设定的密码!
10. 设置 密码文件权限为600 :
[root@hao-02 ~]# chmod 600 /etc/rsync_pass.txt
11. B机器本地文件推送到远程A机器模块对应的目录下,并重命名文件 :
密码验证,免输入密码,就可以同步文件到远程
• secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。
rsync -avP 本地推送文件 --port=端口号 --password-file=客户端密码文件路径 密码对应的用户名@远程ip::模块名/重命名文件
[root@hostname ~]# rsync -avP /tmp/1.txt --port=8730 --password-file=/etc/rsync_pass.txt haolinux@192.168.223.128::hao1/555.txt
10.34 Linux系统日志
logrotate工具 使用参考 :
https://my.oschina.net/u/2000675/blog/908189
1. 列出 linux系统日志文件 :
(一周切割一次,保留四个日志文件带日期)
[root@hao-01 ~]# ls /var/log/messages*
2. 查看 系统日志切割配置文件内容 :
[root@hao-01 ~]# cat /etc/logrotate.conf
3. 内存中系统日志 :
[root@hao-01 ~]# dmesg
4. 清除 内存中系统日志 :
[root@hao-01 ~]# dmesg -c
5. 列出 记录系统启动的日志 :
[root@hao-01 ~]# ls /var/log/dmesg
6. 查看 正确的用户登陆历史日志 :
•last命令调用的文件/var/log/wtmp(用last命令来查看的)
[root@hao-01 ~]# last
7. 查看 失败的用户登陆历史日志 :
•lastb命令调用的文件/var/log/btmp(用lastb命令来查看的)
[root@hao-01 ~]# lastb
8. 查看 安全日志 :
[root@hao-01 ~]# less /var/log/secure
10.35 screen工具
•screen是一个虚拟终端
1. 安装 screen :
[root@hao-01 ~]# yum install -y screen
2. 进入 虚拟终端:
[root@hao-01 ~]# screen
3. 退出 screen虚拟终端,但不结束:
键盘快捷键: ctral a组合键 再按d
4. 查看 虚拟终端列表:
[root@hao-01 ~]# screen -ls
5. 进入 指定的终端 :screen -r 终端id号
[root@hao-01 ~]# screen -r 2693
6. 退出(杀死) 运行的终端 :
[root@hao-01 ~]# exit
7. 自定义 screen :screen -S "自定义screen"
[root@hao-01 ~]# screen -S "hao_screen"
8. 进入 自定义的screen:
[root@hao-01 ~]# screen -r hao_screen
9. 查看 虚拟终端列表:
[root@hao-01 ~]# screen -ls