目录

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.txt
1,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 2018 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-20180527 /var/log/messages-20180610 /var/log/messages-20180521 /var/log/messages-20180603
[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 }

参考:https://my.oschina.net/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 2018

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 2018
[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 root
Jun 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##

扩展

  1. Linux日志文件总管logrotate http://linux.cn/article-4126-1.html

  2. xargs用法详解 http://blog.csdn.net/zhangfn2011/article/details/6776925

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

  1. Linux日常运维管理技巧

    Linux日常运维管理技巧 监控系统状态 1.w/uptime查看系统状态, 执行这个命令可得知目前登入系统的用户有那些人,以及他们正在执行的程序,以及当前负载的情况. 2.cat /proc/cpu ...

  2. Linux日常运维管理技巧(三)iptables规则备份和恢复、firewalld的9个zone、任务计划cron、chkconfig系统服务管理、添加服务命令、systemctl管理服务

    目录 Linux防火墙-netfilter iptables规则备份和恢复 Linux防火墙-firewalled firewalld的9个zone firewalld关于zone的操作 firewa ...

  3. Linux日常运维管理技巧(二)Linux防火墙:你可以ping别人,别人ping不了你、转发、代理

    目录 Linux防火墙 netfilter iptables filter表小案例 nat表应用 Linux防火墙 selinux临时关闭 setenforce 0 [root@zyshanlinux ...

  4. Linux日常运维管理技巧(二)

    Linux防火墙 selinux临时关闭 setenforce 0 [root@zyshanlinux-01 ~]# getenforce ##防火墙状态开启 Enforcing [root@zysh ...

  5. Linux日常运维管理技巧(一)监控系统状态、监控网卡流量、监控IO性能、查看系统进程、查看网络状态、Linux下抓包tcpdump、Linux网络相关、DNS配置

    目录 监控系统状态 监控网卡流量 监控IO性能 查看系统进程 查看网络状态 Linux下抓包 Linux网络相关 监控系统状态 w/uptime查看系统负载 [root@zyshanlinux-01 ...

  6. 日常运维管理技巧十七(Nethogs进程流量监控工具)(转载)

    有很多适用于Linux系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况.netstat用来查看接口统计报告,还有top监控系统当前运行进程.但是如果你想要找一个能够按进程实时统 ...

  7. 日常运维管理技巧十五(htop使用说明top增强版)(转载)

    htop 官网:http://htop.sourceforge.net/ Linux top命令的用法详细详解:https://www.cnblogs.com/zhoug2020/p/6336453. ...

  8. 日常运维管理技巧十六(iftop网卡流量监控工具)(转载)

    iftop是类似于top的实时流量监控工具. 官方网站:http://www.ex-parrot.com/~pdw/iftop/ 1.iftop的作用 iftop可以用来监控网卡的实时流量(可以指定网 ...

  9. Linux服务器运维管理 项目一 认识Linux操作系统

    任务一 查找Linux系统的基本概况 第一步 通过搜索引擎搜索"Linux" 第二步 收集整理搜索到的网页 一.Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torva ...

最新文章

  1. VC++读写INI文件示例
  2. git分支操作的成功案例
  3. php div边框,CSS自定义边框
  4. USACO-Section1.2 Friday the Thirteenth (简单日期处理)
  5. 安装visual studio code(VS Code)并修改为中文
  6. confluence添加用户_知识管理平台Confluence教程:如何添加,分配和查看任务
  7. WordPress ProfilePress插件多个严重漏洞
  8. JavaScript之浏览器大战
  9. ip pim spare 源树 和 共享树_如何实现IP组播,减少摄像头和网络的负载?
  10. 实时渲染技术和DLSS 2.0技术
  11. Deep Homography Estimation - Pytorch实现
  12. 硬盘MBR格式转换GPT
  13. CSS calc()函数的用法
  14. excel图表上x轴y轴怎么设置的教程介绍
  15. Photoshop学习(二十):画面的浮雕效果
  16. css玻璃雨滴效果,纯css实现窗户玻璃雨滴逼真效果
  17. windows 7不能安装谷歌浏览器怎么办?
  18. 米尔基于ARM架构核心板的国产化EtherCAT主站控制器解决方案
  19. 东南大学苏州联合研究生院计算机考研科目,东南大学苏州联合研究生院计算机技术调剂信息...
  20. C语言数据结构:什么是树?什么是二叉树?

热门文章

  1. 推荐一个数据可视化网站,提供绘图创意
  2. Nature Reviews Cancer综述:长非编码RNA在肿瘤转移中的作用
  3. 使用Lock and Load X 插件时导致Final Cat Pro意外退出的解决办法
  4. 【docker】第五节:docker常用命令总结
  5. java 调用jira_java中通过JIRA REST Java Client 使用jira
  6. 操作系统课设之Linux 进程间通信
  7. 【分享-快速仿站】无敌超强仿站小工具一键下载
  8. Qt工作笔记-QML中TextInput设置默认值,以及使用正则表达式只能输入整数
  9. Qt/C++工作笔记-对vector与QVector中erase操作的进一步认识(区别与联系)
  10. Java高级语法笔记-语法支持的异常