10.28 rsync工具介绍
- 可以把文件从A复制到B
- 也可以把文件从a目录拷贝到b目录
本机文件目录复制并改名
-v 可视化
[root@localhost ~]# rsync -av /etc/passwd /root/1.txt
sending incremental file list
passwd
sent 938 bytes received 35 bytes 1,946.00 bytes/sec
total size is 846 speedup is 0.87
拷贝文件目录到远程服务器
[root@localhost ~]# rsync -av /etc/passwd root@192.168.1.105:/root/1.txt
root@192.168.1.105's password:
sending incremental file list
passwd
sent 938 bytes received 35 bytes 278.00 bytes/sec
total size is 846 speedup is 0.87
不写用户表示执行命令的当前用户
[root@localhost ~]# rsync -av /etc/passwd 192.168.1.105:/root/1.txt
root@192.168.1.105's password:
sending incremental file list
sent 45 bytes received 12 bytes 8.77 bytes/sec
total size is 846 speedup is 14.84
远程服务器拷贝文件目录到本机
[root@localhost ~]# rsync -av root@192.168.1.105:/etc/passwd /tmp/1.txt
root@192.168.1.105's password:
receiving incremental file list
passwd
sent 43 bytes received 937 bytes 217.78 bytes/sec
total size is 846 speedup is 0.86
10.29/10.30 rsync常用选项
option | 含义 |
---|---|
-a | 包含-rtplgoD |
-r | 同步目录时要加上,类似cp时的-r选项 |
-v | 同步时显示一些信息,让我们知道同步的过程 |
-l | 保留软连接 |
-L | 加上该选项后,同步软链接时会把源文件给同步 |
-p | 保持文件的权限属性 |
-o | 保持文件的属主 |
-g | 保持文件的属组 |
-D | 保持设备文件信息 |
-t | 保持文件的时间属性 |
--delete | 删除DEST中SRC没有的文件 |
--exclude | 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步 |
-P | 显示同步过程,比如速率,比-v更加详细 |
-u | 加上该选项后,如果DEST中的文件比SRC新,则不同步 |
-z | 传输时压缩 |
-l 软连接
[root@localhost 111]# ls -l
lrwxrwxrwx. 1 root root 11 8月 31 19:55 passwd.l -> /etc/passwd
[root@localhost 111]# rsync -av /root/111/ /tmp/111_dest
sending incremental file list
created directory /tmp/111_dest
./
passwd.l -> /etc/passwd
sent 388 bytes received 153 bytes 1,082.00 bytes/sec
total size is 11 speedup is 0.02
[root@localhost 111]# ls -l /tmp/111_dest/
lrwxrwxrwx. 1 root root 11 8月 31 19:55 passwd.l -> /etc/passwd
-L 加上该选项后,同步软链接时会把源文件给同步
[root@localhost 111]# ls -l
lrwxrwxrwx. 1 root root 11 8月 31 19:55 passwd.l -> /etc/passwd
[root@localhost 111]# rsync -avL /root/111/ /tmp/111_dest
sending incremental file list
passwd.l
sent 1,060 bytes received 35 bytes 2,190.00 bytes/sec
total size is 846 speedup is 0.77
[root@localhost 111]# ls -l /tmp/111_dest/
-rw-r--r--. 1 root root 846 7月 27 05:17 passwd.l
--delete 删除DEST中SRC没有的文件
[root@localhost 111]# ls
1.txt 2.txt 3.txt aaa.txt a.txt passwd.l
[root@localhost 111]# ls /tmp/111_dest/
1.txt 2.txt 3.txt aaa.txt a.txt passwd.l
[root@localhost 111]# touch /tmp/111_dest/cde.txt
[root@localhost 111]# rsync -av --delete /root/111/ /tmp/111_dest
sending incremental file list
deleting cde.txt
sent 193 bytes received 33 bytes 452.00 bytes/sec
total size is 11 speedup is 0.05
--exclude 过滤指定文件
[root@localhost 111]# rm -rf /tmp/111_dest/*
[root@localhost 111]# rsync -av --exclude "*.txt" /root/111/ /tmp/111_dest
sending incremental file list
./
passwd.l -> /etc/passwd
sent 85 bytes received 22 bytes 214.00 bytes/sec
total size is 11 speedup is 0.10
[root@localhost 111]# ls /tmp/111_dest/
passwd.l
-P 显示同步过程,比如速率,比-v更加详细
[root@localhost 111]# rsync -avP /root/111/ /tmp/111_dest
sending incremental file list
./
1.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=5/7)
2.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=4/7)
3.txt
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/7)
a.txt
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=2/7)
aaa.txt
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=1/7)
passwd.l -> /etc/passwd
sent 388 bytes received 117 bytes 1,010.00 bytes/sec
total size is 11 speedup is 0.02
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
[root@localhost 111]# vim /tmp/111_dest/1.txt
jfajsdhafkjdashfkj
[root@localhost 111]# rsync -avPu /root/111/ /tmp/111_dest
sending incremental file list
./
sent 190 bytes received 19 bytes 418.00 bytes/sec
total size is 11 speedup is 0.05
[root@localhost 111]# cat 1.txt
555666
[root@localhost 111]# cat /tmp/111_dest/1.txt
jfajsdhafkjdashfkj
10.31 rsync通过ssh同步
拷贝文件目录到远程服务器
[root@localhost ~]# rsync -av /etc/passwd root@192.168.1.105:/root/1.txt
root@192.168.1.105's password:
sending incremental file list
passwd
sent 938 bytes received 35 bytes 278.00 bytes/sec
total size is 846 speedup is 0.87
远程服务器拷贝文件目录到本机
[root@localhost ~]# rsync -av root@192.168.1.105:/etc/passwd /tmp/1.txt
root@192.168.1.105's password:
receiving incremental file list
passwd
sent 43 bytes received 937 bytes 217.78 bytes/sec
total size is 846 speedup is 0.86
如果对方不是22端口,可指定端口
[root@localhost ~]# rsync -av -e "ssh -p 22" test1/ 192.168.133.132:/tmp/test2/
10.32/10.33 rsync通过服务同步
要编辑配置文件/etc/rsyncd.conf
启动服务rsync --daemon
格式rsync -av test1/ 192.168.133.130::module/dir/
- rsyncd.conf样例
port=873 //指定端口
log file=/var/log/rsync.log //指定日志文件
pid file=/var/run/rsyncd.pid //指定pid文件
address=192.168.133.130 //指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动
[test] //模块名
path=/root/rsync //指定数据存放的路径
use chroot=true //表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false
max connections=4 //指定最大的连接数,默认是0,即没有限制
read only=no //如果为true,则不能上传到该模块指定的路径下
list=true //表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏
uid=root //指定传输文件时以哪个用户/组的身份传输,nobody 权限很小的用户
gid=root
auth users=test //指定传输时要使用的用户名
secrets file=/etc/rsyncd.passwd //指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
hosts allow=192.168.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24 //示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开
注意
当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件
rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass
其中/etc/pass内容就是一个密码,权限要改为600
用法
[root@localhost 111]# vim /etc/rsyncd.conf
[root@localhost 111]# rsync --daemon
[root@localhost 111]# ps aux |grep rsync
root 11775 0.0 0.0 114740 556 ? Ss 20:24 0:00 rsync --daemon
root 11777 0.0 0.0 112720 972 pts/0 R+ 20:24 0:00 grep --color=auto rsync
[root@localhost2 ~]# rsync -av /etc/passwd 192.168.1.104::test/aming-02.txt
指定端口
[root@localhost ~]# rsync -av --port 8730 /etc/passwd 192.168.1.104::test/aming-02.txt
列出所有模块
[root@localhost ~]# rsync 192.168.1.104::
10.34 linux系统日志
/var/log/messages
/etc/logrotate.conf 日志切割配置文件
weekly //一周切割一次
rotate 4 //保留4个文件
create //创建新的日志文件
dateext //日期后缀
#compress //压缩
include /etc/logrotate.d
var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}
参考https://my.oschina.net/u/2000675/blog/908189
dmesg命令 查看硬件信息 在内存中
dmesg -c 清除日志
/var/log/dmesg 日志 与dmesg命令无关
last命令,调用的文件/var/log/wtmp,用户登录信息
lastb命令查看登录失败的用户,对应的文件时/var/log/btmp
/var/log/secure 安全日志
10.35 screen工具
为了不让一个任务意外中断
nohup command & 丢到后台执行脚本
screen是一个虚拟终端
安装工具yum install -y screen
screen直接回车就进入了虚拟终端
ctrl a组合键再按d退出虚拟终端,但不是结束
[detached from 11987.pts-0.localhost]
screen -ls 查看虚拟终端列表
oot@localhost 111]# screen -ls
There is a screen on:
11987.pts-0.localhost (Detached)
1 Socket in /var/run/screen/S-root.
screen -r id 进入指定的终端
screen -r aming
screen -S aming 自定义终端名
screen -r aming
exit 退出