日常运维【4】

  • rsync工具介绍
  • rsync常用选项
  • rsync通过ssh同步
  • rsync通过服务同步【上】
  • rsync通过服务同步【下】
  • linux系统日志
  • screen工具

一,rsync工具介绍(数据备份)

rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

  • [OPTION]=选项
  • SRC=源地址
  • DEST=目标地址
  • [user@]=用户名(如root@,可省略,省略时默认为当前用户)
  • HOST=ip地址

rsync的命令格式:
rsync [OPTION] SRC DEST      本地同步
rsync [OPTION] SRC [USER@]HOST: DEST     从本地数据同步到远程目录
rsync [OPTION] [USER@]HOST:SRC  DEST     从远程目录同步数据到本地
rsync [OPTION] [USER@]HOST::SRC  DEST    从远程目录同步数据到本地,验证方式不同
rsync [OPTION] SRC  [USER@]HOST::DEST    从本地数据同步到远程目录,验证方式不同

二、rsync常用选项

  • -a:包含-rtplgoD
  1. -r:同步目录时加上表示对子目录进行递归处理
  2. -t:保持文件的时间属性
  3. -p:保持文件的权限属性
  4. -l:保留软链接
  5. -g:保存文件数组
  6. -o:保持文件的属主
  7. -D:保存设备文件信息
  • -v:=visual,可视化
  • -L:同步软链接的同时同步其源文件
  • -P:显示同步过程,比v更详细
  • -u:=update,加上该选项,如果DEST中文件比SRC中的新,则不同步
  • -z:=zip,传输时压缩
  • --delete:删除DEST中SRC没有的文件
  • --exclude:过滤指定文件,不同步

相关使用:

  • 同步本地文件
[root@gaodehua33 ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
1.txt.gz
11.txt.zip
2.txt
22.log -> /tmp/22.txt
test.txt
test.zip
123/
123/2.txt
123/cham/
123/cham/test/
123/cham/test/11.txt
123/cham/test/chamlinux/

[root@gaodehua33 ~]# ls -l 111
总用量 2112
-rw-r--r-- 1 root root   71404 12月  5 21:11 11.txt.zip
drwxr-xr-x 3 root root      31 12月  5 21:11 123
-rw-r--r-- 1 root root   71266 12月  5 21:11 1.txt.gz
lrwxrwxrwx 1 root root      11 12月  5 21:23 22.log -> /tmp/22.txt
-rw-r--r-- 1 root root 1585284 12月  5 21:11 2.txt
-rw-r--r-- 1 root root       0 12月  5 21:20 test.txt
-rw-r--r-- 1 root root  423839 12月  5 21:11 test.zip
[root@gaodehua33 ~]# ls -l /tmp/111_dest/
总用量 2112
-rw-r--r-- 1 root root   71404 12月  5 21:11 11.txt.zip
drwxr-xr-x 3 root root      31 12月  5 21:11 123
-rw-r--r-- 1 root root   71266 12月  5 21:11 1.txt.gz
lrwxrwxrwx 1 root root      11 12月  5 21:23 22.log -> /tmp/22.txt
-rw-r--r-- 1 root root 1585284 12月  5 21:11 2.txt
-rw-r--r-- 1 root root       0 12月  5 21:20 test.txt
-rw-r--r-- 1 root root  423839 12月  5 21:11 test.zip
  • rsync -av --exclude 传输时过滤掉指定文件(同步的时候可以指定多个exclude)
[root@gaodehua33 ~]# rsync -avL --exclude "*.txt"  /root/111/ /tmp/111_dest/
sending incremental file list
./
1.txt.gz
11.txt.zip
22.log
test.zip
123/
123/cham/
123/cham/test/
123/cham/test/chamlinux/

sent 630022 bytes  received 107 bytes  1260258.00 bytes/sec
total size is 629601  speedup is 1.00
[root@gaodehua33 ~]# ls /tmp/111_
ls: 无法访问/tmp/111_: 没有那个文件或目录
[root@cham002 ~]# ls /tmp/111_dest/
11.txt.zip  123  1.txt.gz  22.log  test.zip


[root@gaodehua33 ~]# rsync -avL --exclude "*.txt" --exclude "test*"  /root/111/ /tmp/111_dest/
sending incremental file list
./
1.txt.gz
11.txt.zip
22.log
123/
123/cham/

sent 206038 bytes  received 80 bytes  412236.00 bytes/sec
total size is 205762  speedup is 1.00
[root@cham002 ~]# ls /tmp/111_dest/
11.txt.zip  123  1.txt.gz  22.log
[root@gaodehua33 ~]# 
  • rsync -avP 显示详尽的同步过程
[root@gaodehua33 ~]# rsync -avP  /root/111/ /tmp/111_dest/
sending incremental file list
./
1.txt.gz
       71266 100%   36.71MB/s    0:00:00 (xfer#1, to-check=11/13)
11.txt.zip
       71404 100%   68.10MB/s    0:00:00 (xfer#2, to-check=10/13)
2.txt
     1585284 100%  116.30MB/s    0:00:00 (xfer#3, to-check=9/13)
22.log -> /tmp/22.txt
test.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=7/13)
test.zip
      423839 100%   23.78MB/s    0:00:00 (xfer#5, to-check=6/13)
123/
123/2.txt
     1585284 100%   52.13MB/s    0:00:00 (xfer#6, to-check=4/13)
123/cham/
123/cham/test/
123/cham/test/11.txt
     1849498 100%   37.53MB/s    0:00:00 (xfer#7, to-check=1/13)
123/cham/test/chamlinux/

sent 5587814 bytes  received 167 bytes  11175962.00 bytes/sec
total size is 5586586  speedup is 1.00
  • rsync -avL 同步软链接的同时同步其源文件
[root@gaodehua33 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
22.log

sent 63375 bytes  received 35 bytes  42273.33 bytes/sec
total size is 5649667  speedup is 89.10

三、rsync通过ssh同步

  • 将本地机器的内容拷贝到远程机器
  •  进行远程同步的时候,远程机和本地机必须都安装有rsync工具才可以。
A
[root@gaodehua33 ~]# rsync -av /root/111/ [email protected]:/tmp/222_dest
sending incremental file list
created directory /tmp/222_dest
./
1.txt.gz
11.txt.zip
2.txt
22.log -> /tmp/22.txt
test.txt
test.zip
123/
123/2.txt
123/cham/
123/cham/test/
123/cham/test/11.txt
123/cham/test/chamlinux/

sent 5587814 bytes  received 167 bytes  11175962.00 bytes/sec
total size is 5586586  speedup is 1.00


B
[root@zhangeryan ~]# ls /tmp/
222_dest
systemd-private-07784cef2e0a4a67a17f2383678e171d-vmtoolsd.service-pCOnSd
systemd-private-12a98d0e96d7464bbf4ac0a4d21fc8e8-vmtoolsd.service-CWmbPc
systemd-private-85eb551e7da440e89503daf8ae845e90-vmtoolsd.service-bVO4ZE
systemd-private-a021b403901f4520af095d241173e03a-vmtoolsd.service-bLWHsq
yum_save_tx.2017-11-15.07-55.GVJ5F3.yumtx
[root@zhangeryan ~]# ls /tmp/222_dest/
11.txt.zip  123  1.txt.gz  22.log  2.txt  test.txt  test.zip
  • 将远程机器的内容拷贝到本地机器
[root@gaodehua33 ~]# rsync -av [email protected]:/tmp/test111 /tmp/test1205
receiving incremental file list
test111

sent 30 bytes  received 76 bytes  212.00 bytes/sec
total size is 0  speedup is 0.00
[root@gaodehua33 ~]# ls  /tmp/
11111.txt  22.txt  dabao.tar                                                                 test1205
111_dest   2.log   ipt.txt                                                                   test1.tar
1.cap      2.txt   systemd-private-3028068023e74f279fac858603530b71-vmtoolsd.service-X4vfc5
22.log     dabao   test1
 
rsync -av --delete 删除目标文件中和源文件中不一样的文件
[root@gaodehua33 ~]# touch /tmp/111_dest/new.txt
[root@gaodehua33 ~]# rsync -av --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt
22.log -> /tmp/22.txt

sent 258 bytes  received 22 bytes  560.00 bytes/sec
total size is 5586586  speedup is 19952.09
[root@gaodehua33 ~]# ls -l /tmp/111_dest/
总用量 2112
-rw-r--r-- 1 root root   71404 12月  5 21:11 11.txt.zip
drwxr-xr-x 3 root root      31 12月  5 21:11 123
-rw-r--r-- 1 root root   71266 12月  5 21:11 1.txt.gz
lrwxrwxrwx 1 root root      11 12月  5 21:23 22.log -> /tmp/22.txt
-rw-r--r-- 1 root root 1585284 12月  5 21:11 2.txt
-rw-r--r-- 1 root root       0 12月  5 21:20 test.txt
-rw-r--r-- 1 root root  423839 12月  5 21:11 test.zip
  • 远程同步时指定端口,如图:

四、rsync通过服务同步 【上】

  • 在使用该方法进行同步之前需要先在服务的开启相应服务同时要监听一个端口(可自定义),默认是监听873端口,开启服务之前编辑rsync配置文件“/etc/rsyncd.conf”(将服务添加进去)然后直接使用命令:“rsync --daemon(服务名称)”进行同步。或者将配置文件写到一个自定义文件中,使用命令“rsync --configfile”进行同步。
  • 配置完成后客户端可以通过指定端口与服务端进行通信。

配置服务

编辑配置文件:

  • 在配置文件中设定path时注意所给予的权限,安全起见不要直接使用root目录!!!
  • 配置文件中“[]”为模块,里面内容为模块名称,该模块代表的路径即为path指定的路径。
  • 使用服务同步前,要编辑配置文件/etc/rsyncd.conf(只有当编辑配置文件中的端口时才需要重启服务),内容如下:
[root@gaodehua33 ~]# vi !$
vi /etc/rsyncd.conf

port=873
#指定启动rsync服务的端口,如果不指定,默认为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
#use chroot=true/false:安全参数,表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,建议设置成false。

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

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

list=true
#表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出(此时功能类似于tab),false则隐藏。

uid=root
gid=root
#指定传输文件时以哪个uid和gid的身份进行。

auth users=test
#指定传输时要使用的用户名

secrets file=/etc/rsyncd.passwd
#指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。
#注意:该密码文件的权限一定要是600。格式:“用户名:密码”如:test:champin

hosts allow=192.168.133.132 1.1.1.1 2.2.2.2  192.168.133.0/24
#表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。 
  • 启动服务:rsync --daemon
  • 使用命令 ps aux | grep rsync 查看服务是否正常启动。(当进程未正常启动时,可以检查配置文件是否填写正确,如配置的ip地址是否正确。)
  • 使用命令 netstat -lnp  查看指定端口是否在监听装填。

使用 telnet 192.16229.128 873   查看指定ip端的端口是否能够连通。(退出方式:ctrl + ]  再输入 quit )

[root@gaodehua33sy ~]# telnet 192.168.230.135 873
Trying 192.168.230.135...
Connected to 192.168.230.135.
Escape character is '^]'.
@RSYNCD: 30.0
 #退出该命令时使用Ctrl+],然后输入quit即可!
@ERROR: protocol startup error
Connection closed by foreign host.
  • 若还是无法使用,可使用命令iptables -nvL查看firewalld设置,并尝试使用命令 systemctl stop firewalld 关闭两台服务同步的机器的firewalld防火墙。
  • 符号 :: 是服务同步的标志
在客户端进行同步:同步到服务端
[root@gaodehua33sy ~]# rsync -avp /tmp/123123 192.168.230.135::test/123123.txt
sending incremental file list
123123

sent 66 bytes  received 27 bytes  62.00 bytes/sec
total size is 0  speedup is 0.00
[root@gaodehua33 ~]# ls /tmp/rsync
123123.txt


将服务端数据同步到本机(客户端):
[root@gaodehua33sy ~]# rsync -avp 192.168.230.135::test/123123.txt  /tmp/321.txt
receiving incremental file list
123123.txt

sent 45 bytes  received 101 bytes  292.00 bytes/sec
total size is 0  speedup is 0.00
[root@gaodehua33sy ~]# ls /tmp
123123  222_dest  321.txt  systemd-private-85eb551e7da440e89503daf8ae845e90-vmtoolsd.service-bVO4ZE  test111

五、rsync通过服务同步【下】

编辑rsync配置文件,将端口改为port=8730

重启rsync服务

安装killall工具:“yum install -y psmisc”。

[root@gaodehua33 ~]# killall rsync
[root@gaodehua33 ~]# !ps
ps aux |grep rsync
root      3182  0.0  0.0 112664   968 pts/0    S+   21:22   0:00 grep --color=auto rsync
[root@gaodehua33 ~]# rsync --daemon

[root@gaodehua33 ~]# ps aux |grep rsyn
root      3185  0.0  0.0 114644   548 ?        Ss   21:22   0:00 rsync --daemon
root      3191  0.0  0.0 112664   972 pts/0    R+   21:22   0:00 grep --color=auto rsyn
[root@gaodehua33 ~]# !net
netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1703/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2419/master         
tcp        0      0 192.168.230.135:8730    0.0.0.0:*               LISTEN      3185/rsync          
tcp6       0      0 :::22                   :::*                    LISTEN      1703/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2419/master  
  • 说明: 因未开放8730端口,所以此时同步数据会报错!那么如何进行同步呢?方法如下: 使用--port选项指定端口号 
  • 此时使用命令需添加指定端口:rsync -avP --port 8730 192.168.229.128::test/aming.txt /tmp/test.txt
[root@gaodehua33sy ~]# rsync -avp 192.168.230.13::test/123123.txt  /tmp/321.txt
rsync: failed to connect to 192.168.230.13 (192.168.230.13): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(122) [Receiver=3.0.9]

[root@gaodehua33sy ~]# rsync -avp --port 8730 192.168.230.135::test/123123.txt  /tmp/321.txt
receiving incremental file list

sent 26 bytes  received 62 bytes  58.67 bytes/sec
total size is 0  speedup is 0.00
  • 列出服务端的模块名:rsync --port 8730 192.168.229.128::   (当配置文件中的list设置为false时则不显示模块名,该选项属于一个安全选项。)
  • 当uid/gid 设置为一些权限较小的用户时,可能会导致无法同步 (如nobody 权限较小的一个系统用户)
  • 设置auth userssercrets后,连接服务端时这需要使用用户名和密码,操作如下
  • 修改/etc/rsyncd.passwd 文件,格式为   用户名:密码
  • /etc/rsyncd.passwd文件权限更改为600 (未设置,会导致报错)
  • 连接服务端时添加用户名: rsync -avP --port 8730 [email protected]::test/aming.txt /tmp/test.txt    (@前即为设置的用户名)
  • 当每天同步文件需要输入密码过于繁杂操作不变时,可在客户端设置一个密码文件,文件内容只需填写密码即可,随后也更改权限为600。并使用一下命令 :rsync -avPL --port 8730 --password-file=/etc/rsync_passwd [email protected]::test /tmp/111/     其中 --password-file=  即表示指定你设置的那个密码文件的路径

六、Linux系统日志

  • 很多的错误只会保存在日志中,并不会直接显示在屏幕上。
  • /var/log/messages 系统总日志(没有定义单独目录的日志到会保存在这个日志中)
  • /etc/logrotate.conf 日志切割配置文件
  • /var/log/dmesg 系统启动日志
  • /var/log/scure 安全日志文件

dmesg命令

  • 查看系统硬件相关的日志。dmesg命令被用于检查和控制内核的环形缓冲区。kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。
  • ##开机信息保存在:/var/log/dmesg文件里 (与命令dmesg无关)。

在该日志中可以查看系统有哪些硬件故障。
语法: dmesg [option]
Options:
-c:清除日志(系统重启会重新生成)
-n:指定记录信息的层级

last命令

  • last命令用于显示用户最近登录信息。单独执行last命令,它会读取/var/log/wtmp的文件,并把该给文件的内容记录的登入系统的用户名单全部显示出来。

lastb命令

  • lastb命令用于显示用户错误的登录列表,此指令可以发现系统的登录异常。单独执行lastb命令,它会读取位于/var/log目录下,名称为btmp的文件,并把该文件内容记录的登入失败的用户名单,全部显示出来。(/var/log/btmp)

扩展:logrotate命令

  • logrotate命令用于对系统日志进行轮转、压缩和删除,也可以将日志发送到指定邮箱。使用logrotate指令,可让你轻松管理系统所产生的记录文件。每个记录文件都可被设置成每日,每周或每月处理,也能在文件太大时立即处理。您必须自行编辑,指定配置文件,预设的配置文件存放在/etc/logrotate.conf文件中。
  • 安装logrotate工具: yum install logrotate crontabs
  • Logrotate可配置参数,可使用man命令来查询:
compress                        通过gzip压缩转储以后的日志
nocompress                      不压缩
copytruncate                    用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate                  备份日志文件但是不截断
create mode owner group         转储文件,使用指定的文件模式创建新的日志文件
nocreate                        不建立新的日志文件
delaycompress 和 compress        一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress                 覆盖 delaycompress 选项,转储同时压缩。
errors address                   专储时的错误信息发送到指定的Email 地址
ifempty                         即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty                      如果是空文件的话,不转储
mail address                    把转储的日志文件发送到指定的E-mail 地址
nomail                          转储时不发送日志文件
olddir directory                转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir                        转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript             在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript            在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily                           指定转储周期为每天
weekly                          指定转储周期为每周
monthly                         指定转储周期为每月
rotate count                    指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
tabootext [+] list 让logrotate   不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ 
size size                       当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)

七,screen 工具 【虚拟终端】

Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

在一个脚本或进程长时间运行呢无法中断时使用的一个工具,可以防止因为突发事件导致的任务中断。

  • screen直接回车就进入了虚拟终端
  • ctral a组合键再按d退出虚拟终端,但不是结束
  • screen -ls 查看虚拟终端列表
  •  screen -r id 进入指定的终端(也可以使用名称)
  • screen -S aming{自定义名称  }
  • screen -r aming
  • 终止screen:在screen 执行如下命令exit即可
  • 将任务放置后台运行(无法实时查看): nohup sh /usr/local/sbin/sleep.sh &
  • 安装screen工具yum install -y screen

为screen指定命名

[root@gaodehua33 ~]# screen -S "test_screen"
[detached from 3577.test_screen]
[root@gaodehua33 ~]# screen -ls
There are screens on:
	3577.test_screen	(Detached)
	3555.pts-0.cham002	(Detached)
	3540.pts-0.cham002	(Detached)
	3525.pts-0.cham002	(Detached)
4 Sockets in /var/run/screen/S-root.

猜你喜欢

转载自blog.csdn.net/weixin_42604768/article/details/84889338