第十六课 日常运维下

10.28 rsync工具介绍

rsync同步工具(可以实现把一个文件拷贝到另一台机器,或者另一个目录,类似于cp命令但是又不同,当需要同步的源新增加了东西,可以只同步新增加的文件,而不需要整个覆盖。)这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

命令格式:

Rsync 选项  SRC DEST \\拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup

Rsync 选项 SRC [USER@]HOST:DEST  \\使用一个远程shell程序(rshssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src

Rsync 选项 [USER@]HOST:SRC DEST\\使用一个远程shell程序(rshssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avzfoo:src/bar /data

Rsync 选项 [USER@]HOST::SRC DEST\\从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync [email protected]::www /databack

Rsync 选项 SRC [USER@]HOST::DEST\\从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av/databack [email protected]::www


10.29/10.30 rsync常用选项


-a选项:

[root@localhostrsync]# rsync -a test1/ test2/

[root@localhostrsync]# ls -l test2/

总用量 0

-rw-r--r-- 1 rootroot  0 6月  17 07:50 1

lrwxrwxrwx 1 rootroot 13 6月  17 07:53 123.txt ->/root/123.txt

-rw-r--r-- 1 rootroot  0 6月  17 07:50 2

-rw-r--r-- 1 rootroot  0 6月  17 07:50 3

在使用rsync备份目录时,要养成加斜杠的习惯。

-l选项:

[root@localhostrsync]# rm -rf test2

[root@localhostrsync]# rsync -av --no-l test1/ test2/

sendingincremental file list

created directorytest2

./

1

skippingnon-regular file "123.txt"

2

3

 

sent 192bytes  received 72 bytes  528.00 bytes/sec

total size is13  speedup is 0.05

[root@localhostrsync]# cd test2

[root@localhosttest2]# ls

1   2  3

上例跳过了非普通文件123.txt,这个文件是一个软链接文件。

-L选项:

虽然加-l选项能复制软链接文件,但软链接的目标文件却没有复制。有时需要复制软链接文件所指向的目标文件:

[root@localhostrsync]# rm -rf test2

[root@localhostrsync]# rsync -avL test1/ test2/

sendingincremental file list

created directorytest2

./

1

123.txt

2

3

 

sent 223bytes  received 91 bytes  628.00 bytes/sec

total size is0  speedup is 0.00

[root@localhostrsync]# ls -l test2/

总用量 0

-rw-r--r-- 1 rootroot 0 6月  17 07:50 1

-rw-r--r-- 1 rootroot 0 6月  17 07:50 123.txt

-rw-r--r-- 1 rootroot 0 6月  17 07:50 2

-rw-r--r-- 1 rootroot 0 6月  17 07:50 3

上例加上-L选项就可以把SRC中软链接的目标文件复制到DEST

-u选项:

查看test1/1,test2/1的创建时间是一样的

[root@localhostrsync]# ll test1/1 test2/1

-rw-r--r-- 1 rootroot 0 6月  17 07:50 test1/1

-rw-r--r-- 1 rootroot 0 6月  17 07:50 test2/1

下面修改test2/1的创建时间,不加-u同步:

[root@localhostrsync]# echo "1111" > test2/1

[root@localhostrsync]# ll !$

ll test2/1

-rw-r--r-- 1 rootroot 5 6月  17 08:19 test2/1

[root@localhostrsync]# rsync -a test1/1 test2/

[root@localhostrsync]# ll test2/1

-rw-r--r-- 1 rootroot 0 6月  17 07:50 test2/1

这里发现test2/1的创建时间还是和test1/1一样。下面加上-u选项:

[root@localhost rsync]# echo "1111" > test2/1

[root@localhost rsync]# ll test2/1

-rw-r--r-- 1 root root 5 6月  17 08:22 test2/1

[root@localhost rsync]# rsync -avu test1/ test2/

sending incremental file list

./

123.txt -> /root/123.txt

 

sent 92 bytes  received 18bytes  220.00 bytes/sec

total size is 13  speedup is 0.12

[root@localhost rsync]# ll test1/1 test2/1

-rw-r--r-- 1 root root 0 6月  17 07:50 test1/1

-rw-r--r-- 1 root root 5 6月  17 08:22 test2/1

--delete选项:

[root@localhostrsync]# rm -f test1/123.txt

[root@localhostrsync]# ls test1/

1  2  3

[root@localhostrsync]# ll test2/

总用量 4

-rw-r--r--1 root root  5 6月  17 08:22 1

lrwxrwxrwx1 root root 13 6月  1707:53 123.txt -> /root/123.txt

-rw-r--r--1 root root  0 6月  17 07:50 2

-rw-r--r--1 root root  0 6月  17 07:50 3

[root@localhostrsync]# rsync -av --delete test1/ test2/

sendingincremental file list

./

deleting123.txt

1

 

sent94 bytes  received 34 bytes  256.00 bytes/sec

totalsize is 0  speedup is 0.00

[root@localhostrsync]# ls test2/

1   2 3

这里test2/目录下的123.txt也被删除了,--delete表示删除DEST中SRC没有的文件。

--exclude选项:

[root@localhostrsync]# touch test1/4

[root@localhostrsync]# rsync -a --exclude="4" test1/ test2/

[root@localhostrsync]# ls test1/

1  2 3  4

[root@localhostrsync]# ls test2/

1   2 3

同时该选项还可以与匹配字符*一起使用,如下所示:

[root@localhostrsync]# touch test1/1.txt test1/2.txt

[root@localhostrsync]# ls test1/

1  1.txt 2  2.txt  3  4

[root@localhostrsync]# rsync -a --progress --exclude="*.txt" test1/ test2/

sendingincremental file list

./

4

           0 100%    0.00kB/s   0:00:00 (xfer#1, to-check=0/5)

 

sent104 bytes  received 34 bytes  276.00 bytes/sec

totalsize is 0  speedup is 0.00

[root@localhostrsync]# ls test2/

1   2 3  4

10.31rsync通过ssh同步

[root@localhostrsync]# rsync -avL test1/ 192.168.19.32:/tmp/test2/

[email protected]'spassword:

sendingincremental file list

created directory/tmp/test2

./

1

1.txt

2

2.txt

3

4

 

sent 323bytes  received 129 bytes  53.18 bytes/sec

total size is0  speedup is 0.00

这种方式是前面第二种命令格式,是通过ssh复制的数据,需要输入192.168.19.32root账户的密码。

10.32/10.33rsync通过服务同步

这种方式可以理解为:在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后将本机作为rsync的一个客户端连接远程的rsync服务器。

首先在主机192.168.19.15上配置:vim /etc/rsyncd.conf\\配置文件

port=873\\端口

logfile=/var/log/rsync.log\\日志文件

pidfile=/var/run/rsyncd.pid\\进程pid

address=192.168.19.15\\

 

[test]\\模块名

path=/root/rsync\\模块对应本机目录

use chroot=true\\是否限定在当前目录下,例如软链接,不在当前目录

max connections=4\\最大连接数

read only=no\\在目录下只读

list=true\\

uid=root\\属主

gid=root\\属组

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

secretsfile=/etc/rsyncd.passwd\\指定密码文件

hostsallow=192.168.19.32


10.34 linux
系统日志

经常查看的日志文件/var/log/messages.它是核心系统日志文件,包含了系统启动时的引导消息,以及系统运行时的其他状态消息。

常用日志文件

系统日志是由一个名为syslog的服务管理的,如以下日志文件都是由syslog日志服务驱动的:

/var/log/boot.log:录了系统在引导过程中发生的事件,就是Linux系统开机自检过程显示的信息

/var/log/lastlog :记录最后一次用户成功登陆的时间、登陆IP等信息

/var/log/messages :记录Linux操作系统常见的系统和服务错误信息

/var/log/secure :Linux系统安全日志,记录用户和工作组变坏情况、用户登陆认证情况

/var/log/btmp :记录Linux登陆失败的用户、时间以及远程IP地址

/var/log/syslog:只记录警告信息,常常是系统出问题的信息,使用lastlog查看

/var/log/wtmp:该日志文件永久记录每个用户登录、注销及系统的启动、停机的事件,使用last命令查看

/var/run/utmp:该日志文件记录有关当前登录的每个用户的信息。如 who、w、users、finger等就需要访问这个文件

last用来查看登录linux的系统信息。

/var/log/secure记录的也是和登录有关的信息,如ssh登录失败的信息在这里都可以记录到。

很多错误信息都不会反应在屏幕上,而是写在了日志里,所以使用常用软件时也应该有查看日志的习惯。如apache,myslq,php等时。

 

除了关注常用的日志文件外,还应该多关注一下dmesg这个命令,它可以显示系统的启动信息。

 

 


10.35 screen
工具

统管理员经常需要SSH 或者telent 远程登录到Linux服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份、ftp 传输等等。通常情况下我们都是为每一个这样的任务开一个远程终端窗口,因为它们执行的时间太长了。必须等待它们执行完毕,在此期间不能关掉窗口或者断开连接,否则这个任务就会被杀掉,一切半途而废了。

Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

·        会话恢复

只要Screen本身没有终止,在其内部运行的会话都可以恢复。这一点对于远程登录的用户特别有用——即使网络连接中断,用户也不会失去对已经打开的命令行会话的控制。只要再次登录到主机上执行screen -r就可以恢复会话的运行。同样在暂时离开的时候,也可以执行分离命令detach,在保证里面的程序正常运行的情况下让Screen挂起(切换到后台)。这一点和图形界面下的VNC很相似。

·        多窗口

在Screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。Screen实现了基本的文本操作,如复制粘贴等;还提供了类似滚动条的功能,可以查看窗口状况的历史记录。窗口还可以被分区和命名,还可以监视后台窗口的活动。

·        会话共享

Screen可以让一个或多个用户从不同终端多次登录一个会话,并共享会话的所有特性(比如可以看到完全相同的输出)。它同时提供了窗口访问权限的机制,可以对窗口进行密码保护。

有时候一个任务要执行几个小时,或者几天,如果中途断网或者出现意外情况怎么办? 我们可以用两种办法来解决。

nohup :执行一个脚本

nohupsh /uer/local/sbin/sleep.sh & //虽然末尾直接加&也可以在后台运行,但是如果退出该终端,这个脚本也会退出。如果前面加上了nohup就不会有这个问题。他的作用就是在当前目录下生成一个nohup文件,放置进程意外中断,并把输出信息记录到nohup中。

Screen:

screen就是开启一个新的终端,利用这个工具可以确保任务不会因为退出某个终端而终止。

首先安装screen:yum install –y screen

打开一个会话,直接输入screen命令,输入命令screen会打开一个终端,ctrl+a然后ctrl+d会退出该终端。

screen -ls //列出有哪些screen终端以及他们的编号


扩展
1. Linux日志文件总管logrotate http://linux.cn/article-4126-1.html
2. xargs
用法详解 http://blog.csdn.net/zhangfn2011/article/details/6776925

猜你喜欢

转载自blog.csdn.net/zanghaos/article/details/80722622