失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Linux日常运维管理技巧(四)文件同步工具-rsync Linux系统日志 dmesg命令 lastb命

Linux日常运维管理技巧(四)文件同步工具-rsync Linux系统日志 dmesg命令 lastb命

时间:2018-08-01 20:08:19

相关推荐

Linux日常运维管理技巧(四)文件同步工具-rsync Linux系统日志 dmesg命令 lastb命

目录

Linux文件同步工具-rsync

Linux系统日志

dmesg命令

lastb命令

screen工具

Linux文件同步工具-rsync

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。 rsync是一个功能非常强大的工具,其命令也有很多功能特色选项,我们下面就对它的选项一一进行分析说明。

eg.

rsync -av /etc/passwd /tmp/1.txt ##本机同步:A目录同步到B目录,并改名为1.txt。##

rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt ##远程同步:本机的A同步到远程的B,或相反##

rsync常用选项

-a , --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD-r , --recursive 对子目录以递归模式处理,同步目录时要加上,类似cp时的-r选项-v , --verbose 详细模式输出,同步时显示一些信息,让我们知道同步的过程-l , --links 保留软链结-L , --copy-links 想对待常规文件一样处理软链结,加上该选项后,同步软链接时会把源文件给同步-p , --perms 保持文件权限,保持文件的权限属性-o , --owner 保持文件属主信息,保持文件的属主-g , --group 保持文件属组信息-D , --devices 保持设备文件信息-t , --times 保持文件时间属性--delete 删除DEST中SRC没有的文件--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步-P 等同于 --partial,显示同步过程,比如速率,比-v更加详细-u , --update加上该选项后,如果DEST中的文件比SRC新(mtime要新),则不同步-z , --compress 对备份的文件在传输时进行压缩处理

实例

第一次用这个rsync命令前,先安装包

[root@zyshanlinux-001 ~]# yum install -y rsync

同步目录(目录后记得加/),并改名

[root@zyshanlinux-001 ~]# rsync -av /root/009/ /tmp/009_dest/sending incremental file list created directory /tmp/009_dest ./ 004.txt 011.txt.bak test01 008/ 008/005.txt 010/​sent 382 bytes received 143 bytes 1,050.00 bytes/sec total size is 0 speedup is 0.00

软链接也同步

选项-L可以把软链接指向的源文件同步过去,并且覆盖了选项-l

选项--delete删除与源目录文件不一样的目标目录文件

[root@zyshanlinux-001 ~]# touch /tmp/009_dest/new.txt [root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 004.txt 008 010 011.txt.bak new.txt test01[root@zyshanlinux-001 ~]# rsync -av --delete /root/009/ /tmp/009_dest/ sending incremental file list deleting new.txt ./​sent 216 bytes received 32 bytes 496.00 bytes/sec total size is 0 speedup is 0.00 [root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 004.txt 008 010 011.txt.bak test01

选项--exclude过滤以txt结尾的文件,再同步

[root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 004.txt 008 010 011.txt.bak test01[root@zyshanlinux-001 ~]# rm -rf /tmp/009_dest/* [root@zyshanlinux-001 ~]#[root@zyshanlinux-001 ~]# rsync -avL --exclude "*.txt" /root/009/ /tmp/009_dest/ sending incremental file list ./ 011.txt.bak test01 008/ 010/​sent 253 bytes received 65 bytes 636.00 bytes/sec total size is 0 speedup is 0.00 [root@zyshanlinux-001 ~]# ls /tmp/009_dest/ 008 010 011.txt.bak test01​[root@zyshanlinux-001 ~]# rm -rf /tmp/009_dest/* [root@zyshanlinux-001 ~]# rsync -avL --exclude "*.txt" --exclude "test*" /root/009/ /tmp/009_dest/ sending incremental file list ./ 011.txt.bak 008/ 010/​sent 189 bytes received 46 bytes 470.00 bytes/sec total size is 0 speedup is 0.00

选项-P 显示同步过程,比如速率,比-v更加详细,大文件非常有用可以看到传输过程

[root@zyshanlinux-001 ~]# !rm rm -rf /tmp/009_dest/* [root@zyshanlinux-001 ~]# rsync -avP /root/009/ /tmp/009_dest/ sending incremental file list ./ 004.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=5/7) 011.txt.bak 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=4/7) test01 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=3/7) 008/ 008/005.txt 0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=0/7) 010/​sent 382 bytes received 107 bytes 978.00 bytes/sec total size is 0 speedup is 0.00

选项-u 加上该选项后,如果DEST中的文件比SRC新,则不同步

[root@zyshanlinux-001 009_dest]# vim test01 [root@zyshanlinux-001 009_dest]# cat !$ cat test01 123 456 789 [root@zyshanlinux-001 009_dest]# rsync -avPu /root/009/ /tmp/009_dest/ sending incremental file list ./​sent 216 bytes received 21 bytes 474.00 bytes/sec total size is 0 speedup is 0.00 [root@zyshanlinux-001 009_dest]# cat test01 123 456 789

推文件:把本地的文件同步到远程目录

[root@zyshanlinux-001 ~]# rsync -av /etc/passwd 192.168.106.130:/tmp/zyshan.txt root@192.168.106.130's password: sending incremental file list passwd​sent 1,404 bytes received 35 bytes 261.64 bytes/sec total size is 1,312 speedup is 0.91 ######################################################### [root@zyshanlinux-02 ~]# cat /tmp/zyshan.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ...

拉文件:把远程目录的文件同步到本地

[root@zyshanlinux-001 ~]# rsync -avP 192.168.106.130:/tmp/zyshan.txt /tmp/123.txt root@192.168.106.130's password: receiving incremental file list zyshan.txt1,312 100% 1.25MB/s 0:00:00 (xfr#1, to-chk=0/1)​sent 43 bytes received 1,408 bytes 263.82 bytes/sec total size is 1,312 speedup is 0.90

如果不是22端口,则选用-e选项,指定端口

[root@zyshanlinux-001 ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.106.130:/tmp/zyshan.txt root@192.168.106.130's password: sending incremental file list​sent 45 bytes received 12 bytes 1.48 bytes/sec total size is 1,312 speedup is 23.02

直接连接远程主机

[root@zyshanlinux-001 ~]# ssh -p 22 192.168.106.130 root@192.168.106.130's password: Last login: Mon Jun 18 10:17:02 from 192.168.106.1 [root@zyshanlinux-02 ~]# exit 登出 Connection to 192.168.106.130 closed.

rsync 通过服务的方式同步

要编辑配置文件/etc/rsyncd.conf

port=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pidaddress=192.168.106.128

[test]path=/tmp/rsyncuse chroot=truemax connections=4read only=no

list=trueuid=rootgid=rootauth users=testsecrets file=/etc/rsyncd.passwdhosts allow=192.168.106.130

rsyncd.conf配置文件详解

​port:指定在哪个端口启动rsyncd服务,默认是873端口。

log file:指定日志文件。

pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。

address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。

[]:指定模块名,里面内容自定义。

path:指定数据存放的路径。

use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。

max connections:指定最大的连接数,默认是0,即没有限制。

read only ture|false:如果为true,则不能上传到该模块指定的路径下。

list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。

uid/gid:指定传输文件时以哪个用户/组的身份传输。 auth users:指定传输时要使用的用户名。

secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码

hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。 当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件 rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass 其中/etc/pass内容就是一个密码,权限要改为600

启动服务rsync --daemon

格式:rsync -av test1/ 192.168.133.130::module/dir/

Linux系统日志

/var/log/messages 是linux系统的总日志

[root@zyshanlinux-001 ~]# ls /var/log/messages /var/log/messages [root@zyshanlinux-001 ~]# less !$ less /var/log/messages [root@zyshanlinux-001 ~]# du -sh !$ du -sh /var/log/messages 2.0M /var/log/messages

/etc/logrotate.conf 日志切割配置文件

[root@zyshanlinux-001 ~]# ls /var/log/messages* /var/log/messages/var/log/messages-0527 /var/log/messages-0610 /var/log/messages-0521 /var/log/messages-0603[root@zyshanlinux-001 ~]# logrotate^C [root@zyshanlinux-001 ~]# cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly​# keep 4 weeks worth of backlogs rotate 4​# create new (empty) log files after rotating old ones create​# use date as a suffix of the rotated file dateext​# uncomment this if you want your log files compressed #compress​# RPM packages drop log rotation information into this directory include /etc/logrotate.d​# no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1 }​/var/log/btmp { missingok monthly create 0600 root utmp rotate 1 }​# system-specific logs may be also be configured here.

指定切割日志,脚本定义切割日志后让系统在新的日志上正常写入

[root@zyshanlinux-001 ~]# cat !$ cat /etc/logrotate.d/syslog /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler { missingok sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript }

参考:/u/2000675/blog/908189

dmesg命令

把系统硬件相关日志列出来,保存在内存中并不是一个文件

[root@zyshanlinux-001 ~]# dmesg ... [root@zyshanlinux-001 ~]# dmesg -c ##清除内存中的硬件日志,当重启系统或出现问题硬件日志又会生成##

/var/log/dmesg 系统启动的日志,记录的一些信息,和dmesg命令没有关系的

[root@zyshanlinux-001 ~]# ls /var/log/dmesg /var/log/dmesg

last命令 查看正确的登录历史,调用的文件/var/log/wtmp(二进制文件,不能用cat看)

[root@zyshanlinux-001 ~]# last root pts/0 192.168.106.1 Mon Jun 18 08:19 still logged in reboot system boot 3.10.0-693.el7.x Mon Jun 18 08:18 - 11:46 (03:28) root tty1 Fri Jun 15 22:04 - 22:04 (00:00) root pts/0 192.168.106.1 Fri Jun 15 20:12 - 22:03 (01:51) reboot system boot 3.10.0-693.el7.x Fri Jun 15 20:11 - 22:04 (01:52)[root@zyshanlinux-001 ~]# last root pts/0 192.168.106.1 Mon Jun 18 08:19 still logged in root tty1 Mon May 7 04:37 - crash (-6:-24) reboot system boot 3.10.0-693.el7.x Mon May 7 04:36 - 19:57 (2+15:21) ​wtmp begins Mon May 7 04:36:08

lastb命令

查看登录失败的用户,对应的文件时/var/log/btmp

[root@zyshanlinux-001 ~]# lastb root tty1 Sun Jun 10 11:47 - 11:47 (00:00) ​btmp begins Sun Jun 10 11:47:04 [root@zyshanlinux-001 ~]# ls /var/log/btmp /var/log/btmp

/var/log/secure 安全日志

[root@zyshanlinux-001 ~]# ls /var/log/secure /var/log/secure [root@zyshanlinux-001 ~]# cat !$ cat /var/log/secure Jun 10 23:05:17 zyshanlinux-01 sshd[1250]: error: Received disconnect from 192.168.106.1 port 14605:0: Jun 10 23:05:17 zyshanlinux-01 sshd[1250]: Disconnected from 192.168.106.1 port 14605 Jun 10 23:05:17 zyshanlinux-01 sshd[1250]: pam_unix(sshd:session): session closed for user rootJun 10 23:05:32 zyshanlinux-01 polkitd[551]: Registered Authentication Agent for unix-process:2138:4212583 (system bus name :1.117 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8)Jun 18 08:19:20 zyshanlinux-001 sshd[1369]: pam_unix(sshd:session): session opened for user root by (uid=0)

screen工具

虚拟屏幕(虚拟终端)

远程运行脚本输入命令,断网了就会令命令失败;

解决办法有2种。

1、后台运行,输出日志,缺点:不能实时看到运行的情况

为了不让一个任务意外中断 命令:nohup command &

2、screen是一个虚拟终端

[root@zyshanlinux-001 ~]# yum install -y screen

screen的操作

[root@zyshanlinux-001 ~]# screen [detached from 2763.pts-0.zyshanlinux-001] ##Ctrl+a+d把虚拟终端放到后台## [root@zyshanlinux-001 ~]# screen -ls ##看看在后台运行的所有终端## There is a screen on: 2763.pts-0.zyshanlinux-001 (Detached) 1 Socket in /var/run/screen/S-root.​[root@zyshanlinux-001 ~]# screen -r 2763 ##打开指定id的虚拟终端## [screen is terminating] [root@zyshanlinux-001 ~]# screen -ls ##结束虚拟终端,exit就登出虚拟终端## No Sockets found in /var/run/screen/S-root.​[root@zyshanlinux-001 ~]#

给screen定义名字,方便数量多了知道哪个screen运行什么东西

[root@zyshanlinux-001 ~]# screen -ls There are screens on: 2862.pts-0.zyshanlinux-001 (Detached) 2847.pts-0.zyshanlinux-001 (Detached) 2 Sockets in /var/run/screen/S-root.​[root@zyshanlinux-001 ~]# screen -S "test_screen" ##选项-S启动一个自定义名字的screen## [detached from 2903.test_screen] [root@zyshanlinux-001 ~]# screen -ls There are screens on: 2903.test_screen (Detached) 2862.pts-0.zyshanlinux-001 (Detached) 2847.pts-0.zyshanlinux-001 (Detached) 3 Sockets in /var/run/screen/S-root.​[root@zyshanlinux-001 ~]# screen -r test_screen ##可以用id或者自定义名字启动screen##

扩展

Linux日志文件总管logrotate /article-4126-1.html

xargs用法详解 /zhangfn/article/details/6776925

Linux日常运维管理技巧(四)文件同步工具-rsync Linux系统日志 dmesg命令 lastb命令查看登录失败的用户 screen工具虚拟屏幕

如果觉得《Linux日常运维管理技巧(四)文件同步工具-rsync Linux系统日志 dmesg命令 lastb命》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。