Linux日常运维技巧-4
rsync工具
Rsync简介
rsync全称remote sync,是一种更高效、可以本地或远程同步的命令,
之所以高效是因为rsync会对需要同步的源和目的进度行对比,只同步有改变的部分,所以比scp命令更高效,
但是rsync本身是一种非加密的传输,可以借助-e选项来设置具备加密功能的承载工具进行加密传输
rsync的六种工作模式
1:rsync [OPTION] … SRC DEST
拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。
如:rsync -a /data /backup
2:rsync [OPTION] … SRC [user@]host:DEST
使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。
如:rsync -avz *.c foo:src
3:rsync [OPTION] … [user@]host:SRC DEST
使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。
如:rsync -avz foo:src/bar /data
4:rsync [OPTION] … SRC [user@]host::DEST
从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。
如:rsync -av root@192.168.78.192::www /databack
5:rsync [OPTION] … [user@]host::SRC DEST
从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。
如:rsync -av /databack root@192.168.78.192::www
6:rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。
如:rsync -v rsync://192.168.78.192/www
rsync常用选项
-a 包含-rtplgoD
-r 同步目录时要加上,类似cp时的-r选项
[root@long0000 ~]# rsync test /tmp/
skipping directory test
[root@long0000 ~]# rsync -r test /tmp/
[root@long0000 ~]# ls -dl /tmp/test
drwxr-xr-x. 2 root root 6 Jun 21 00:25 /tmp/test
-v 同步时显示一些信息,让我们知道同步的过程
[root@long0000 ~]# rsync /etc/passwd /tmp/test
[root@long0000 ~]# rsync -v /etc/shadow /tmp/test
shadow
sent 940 bytes received 35 bytes 1,950.00 bytes/sec
total size is 856 speedup is 0.88
-l 保留软连接
[root@long0000 ~]# rsync -r ./test /tmp
skipping non-regular file "test/passwd"
[root@long0000 ~]# ls -l /tmp/test
total 8
-rw-r--r--. 1 root root 1183 Jun 21 00:27 passwd
----------. 1 root root 856 Jun 21 00:27 shadow
[root@long0000 ~]# rsync -rl ./test /tmp
[root@long0000 ~]# ls -l /tmp/test
total 4
lrwxrwxrwx. 1 root root 11 Jun 21 00:30 passwd -> /etc/passwd
----------. 1 root root 856 Jun 21 00:27 shadow
-L 加上该选项后,同步软链接时会把源文件给同步
[root@long0000 ~]# rsync -rL ./test /tmp
[root@long0000 ~]# ls -l /tmp/test
total 8
-rw-r--r--. 1 root root 1183 Jun 21 00:31 passwd
----------. 1 root root 856 Jun 21 00:27 shadow
-p 保持文件的权限属性
[root@long0000 ~]# rsync ./test/testfile /tmp/
[root@long0000 ~]# ls -dl ./test/testfile /tmp/testfile
-rwxrwxrwx. 1 root root 0 Jun 21 00:35 ./test/testfile
-rwxr-xr-x. 1 root root 0 Jun 21 00:36 /tmp/testfile
[root@long0000 ~]# rm -f /tmp/testfile
[root@long0000 ~]# rsync -p ./test/testfile /tmp/
[root@long0000 ~]# ls -dl ./test/testfile /tmp/testfile
-rwxrwxrwx. 1 root root 0 Jun 21 00:35 ./test/testfile
-rwxrwxrwx. 1 root root 0 Jun 21 00:39 /tmp/testfile
-o 保持文件的属主
-g 保持文件的属组
[root@long0000 ~]# chown user1.user1 ./test/testfile
[root@long0000 ~]# ls -dl ./test/testfile
-rwxrwxrwx. 1 user1 user1 0 Jun 21 00:35 ./test/testfile
[root@long0000 ~]# rsync -p ./test/testfile /tmp/
[root@long0000 ~]# ls -dl ./test/testfile /tmp/testfile
-rwxrwxrwx. 1 user1 user1 0 Jun 21 00:35 ./test/testfile
-rwxrwxrwx. 1 root root 0 Jun 21 00:41 /tmp/testfile
[root@long0000 ~]# rm -f /tmp/testfile
[root@long0000 ~]# rsync -pog ./test/testfile /tmp/
[root@long0000 ~]# ls -dl ./test/testfile /tmp/testfile
-rwxrwxrwx. 1 user1 user1 0 Jun 21 00:35 ./test/testfile
-rwxrwxrwx. 1 user1 user1 0 Jun 21 00:41 /tmp/testfile
-D 保持设备文件信息
[root@long0000 ~]# rsync /dev/tty /tmp
skipping non-regular file "tty"
[root@long0000 ~]# rsync -D /dev/tty /tmp
[root@long0000 ~]# ls -dl /tmp/tty
crw-r--r--. 1 root root 5, 0 Jun 21 00:44 /tmp/tty
-t 保持文件的时间属性
[root@long0000 ~]# rsync /etc/passwd /tmp/
[root@long0000 ~]# stat /etc/passwd /tmp/passwd
File: ‘/etc/passwd’
Access: 2018-06-21 00:34:05.803700337 +0800
Modify: 2018-06-15 01:15:05.649650803 +0800
Change: 2018-06-21 00:34:04.678710004 +0800
File: ‘/tmp/passwd’
Access: 2018-06-21 00:46:44.939212587 +0800
Modify: 2018-06-21 00:46:44.939212587 +0800 #未加-t选项同步会修改文件的时间
Change: 2018-06-21 00:46:44.939212587 +0800
[root@long0000 ~]# rm -f /tmp/passwd
[root@long0000 ~]# rsync -t /etc/passwd /tmp/
[root@long0000 ~]# stat /etc/passwd /tmp/passwd
File: ‘/etc/passwd’
Context: system_u:object_r:passwd_file_t:s0
Access: 2018-06-21 00:34:05.803700337 +0800
Modify: 2018-06-15 01:15:05.649650803 +0800
Change: 2018-06-21 00:34:04.678710004 +0800
File: ‘/tmp/passwd’
Access: 2018-06-21 00:49:49.795635419 +0800
Modify: 2018-06-15 01:15:05.649650803 +0800 #使用-t选项会保持文件的Modify时间。
Change: 2018-06-21 00:49:49.795635419 +0800
--delete 删除DEST中SRC没有的文件
[root@long0000 ~]# touch ./test/{1..3}.txt
[root@long0000 ~]# rsync -r ./test /tmp/
[root@long0000 ~]# ls ./test/ /tmp/test/
./test/:
1.txt 2.txt 3.txt
/tmp/test/:
1.txt 2.txt 3.txt
[root@long0000 ~]# rm ./test/2.txt -f
[root@long0000 ~]# rsync -r --delete ./test /tmp/
[root@long0000 ~]# ls ./test/ /tmp/test/
./test/:
1.txt 3.txt
/tmp/test/:
1.txt 3.txt
--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
[root@long0000 ~]# rm -f /tmp/test/*
[root@long0000 ~]# rsync -r --exclude "3.txt" ./test /tmp/
[root@long0000 ~]# ls ./test/ /tmp/test/
./test/:
1.txt 3.txt
/tmp/test/:
1.txt
-P 显示同步过程,比如速率,比-v更加详细
[root@long0000 ~]# dd if=/dev/zero of=./test.file1 bs=1M count=10
[root@long0000 ~]# dd if=/dev/zero of=./test.file2 bs=1M count=10
[root@long0000 ~]# dd if=/dev/zero of=./test.file3 bs=1M count=10
[root@long0000 ~]# cp test.file* ./test/
[root@long0000 ~]# ls ./test
test.file1 test.file2 test.file3
[root@long0000 ~]# rsync -aP ./test /tmp
sending incremental file list
test/
test/test.file1
10,485,760 100% 82.39MB/s 0:00:00 (xfr#1, to-chk=2/4)
test/test.file2
10,485,760 100% 42.02MB/s 0:00:00 (xfr#2, to-chk=1/4)
test/test.file3
10,485,760 100% 28.65MB/s 0:00:00 (xfr#3, to-chk=0/4)
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
[root@long0000 ~]# rm -f /tmp/test/* ./test/*
[root@long0000 ~]# echo `date "+%F %T"` >> ./test/test.txt
[root@long0000 ~]# cat test/test.txt
2018-06-21 01:19:10
[root@long0000 ~]# echo `date "+%F %T"` >> /tmp/test.txt
[root@long0000 ~]# cat /tmp//test.txt
2018-06-21 01:19:50
[root@long0000 ~]# rsync -u ./test/test.txt /tmp/test.txt
[root@long0000 ~]# cat /tmp//test.txt
2018-06-21 01:19:50
-z 传输时压缩
[root@long0000 ~]# dd if=/dev/sda of=file1 bs=4M count=250
[root@long0000 ~]# ls -dlh file1
-rw-r--r--. 1 root root 1000M Jun 21 01:26 file1
[root@long0000 ~]# rsync -P file1 /tmp
file1
1,048,576,000 100% 174.48MB/s 0:00:05 (xfr#1, to-chk=0/1)
[root@long0000 ~]# rm -f /tmp/file1
[root@long0000 ~]# rsync -zP file1 /tmp
file1
1,048,576,000 100% 116.36MB/s 0:00:08 (xfr#1, to-chk=0/1)
rsync通过ssh同步
1:将文件推送到目标主机上
[root@long0000 ~]# rsync -av test/test.txt 10.1.1.26:/root/test/
The authenticity of host '10.1.1.26 (10.1.1.26)' can't be established.
ECDSA key fingerprint is SHA256:6fNPXTYzIwcyPUZTCOEXOMlWtpU057I6zjP9HjQ6ry0.
ECDSA key fingerprint is MD5:de:0e:69:53:99:80:c6:26:ac:5e:cb:50:ee:19:b3:d4.
Are you sure you want to continue connecting (yes/no)? yes
(注:第一次使用ssh方式同步需要接受密钥信息,输入yes即可)
Warning: Permanently added '10.1.1.26' (ECDSA) to the list of known hosts.
root@10.1.1.26's password: #这里输出目标主机的root用户密码
sending incremental file list
test.txt
sent 114 bytes received 35 bytes 19.87 bytes/sec
total size is 20 speedup is 0.13
[root@long0000 ~]# cat test/test.txt
2018-06-21 01:19:10 #这是源主机的文件
[root@10_1_1_26 ~]# cat test/test.txt
2018-06-21 01:19:10 #这是推送到目标主机里面的文件。
2:从目标主机拉文件到本机。
[root@long0000 ~]# date '+%F %T' >> /tmp/date.txt
[root@long0000 ~]# cat /tmp/date.txt
2018-06-21 01:57:56
[root@10_1_1_26 ~]# echo "10.1.1.25 long0000" >> /etc/hosts
[root@10_1_1_26 ~]# rsync -av long0000:/tmp/date.txt /tmp
root@long0000's password:
receiving incremental file list
date.txt
sent 43 bytes received 114 bytes 44.86 bytes/sec
total size is 20 speedup is 0.13
[root@10_1_1_26 ~]# cat /tmp/date.txt
2018-06-21 01:57:56
rsync通过服务同步
rsync服务端配置
1:rsync通过服务同步需要编辑配置文件/etc/rsyncd.conf
##文件内容解释
uid = rsync #用户uid
gid = root #用户gid
use chroot = no #安全相关
max connections = 200 #最大连接数
timout = 300 #超时参数(秒)
pid file = /var/run/rsyncd.pid #进程号对应的文件
lock file = /var/run/rsync.lock #锁文件,防止文件不一致
log file = /var/log/rsyncd.log #日志文件
[backup] #模块名称
path = /backup #服务器提供访问的目录
ignore errors #忽略错误
read only = false #只读模式 false关闭,意为可读写
list = false #不允许列表
hosts allow = 10.0.0.0/24 #允许连接的网段
host deny = 0.0.0.0/32 #拒绝连接的网段
auth users = rsync_backup #连接的虚拟用户,非系统用户
secrets file = /etc/rsync.password #虚拟用户的账号密码文件
[root@10_1_1_26 ~]# cat /etc/rsyncd.conf
uid = rsync
gid = root
use chroot = no
max connections = 200
timout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup
ignore errors
read only = false
list = false
hosts allow = 10.1.1.0/24
host deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
2:创建rsync用户
[root@long0000 ~]# useradd rsync -s /sbin/nologin -M
3:创建备份目录
[root@long0000 ~]# mkdir /backup
4:把备份目录用户和用户组改为配置文件中的用户和用户组。
[root@long0000 ~]# chown rsync.root /backup
5:编辑rsync虚拟用户和密码文件,格式:user:password
[root@long0000 ~]# vim /etc/rsync.password
[root@long0000 ~]# cat /etc/rsync.password
rsync_backup:123456
6:更改虚拟用户密码文件的权限为600
[root@long0000 ~]# chmod 600 /etc/rsync.password
7:以进程模式启动rsync服务
[root@long0000 ~]# rsync --daemon
8:加入开机自启动(注:非必要操作)
[root@long0000 ~]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local #
9:检查rsync服务端口873是否开启
[root@long0000 ~]# netstat -lnp | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 129173/rsync
tcp6 0 0 :::873 :::* LISTEN 129173/rsync
rsync客户端配置
1:配置客户端的密码文件, 只需要写上服务端设置的虚拟用户的密码
[root@10_1_1_26 ~]# vim /etc/rsync.password
[root@10_1_1_26 ~]# cat /etc/rsync.password
123456
2:改密码文件权限为600
[root@10_1_1_26 ~]# chmod 600 /etc/rsync.password
3:创建本地临时备份目录
[root@10_1_1_26 ~]# mkdir /backup
rsync以服务模式的备份操作
准备实验环境
[root@10_1_1_26 ~]# mkdir /backup
[root@10_1_1_26 ~]# cd /backup/
[root@10_1_1_26 backup]# touch {1..5}.txt
[root@10_1_1_26 backup]# ls .
1.txt 2.txt 3.txt 4.txt 5.txt
第一种推送备份的命令,–password-file指定rsync密码文件
[root@10_1_1_26 /]# rsync -avz /backup/ rsync_backup@10.1.1.25::backup/ --password-file=/etc/rsync.password
sending incremental file list
./
1.txt
2.txt
3.txt
4.txt
5.txt
sent 311 bytes received 114 bytes 850.00 bytes/sec
total size is 0 speedup is 0.00
检查rsync服务端的备份目录
[root@long0000 ~]# ls /backup/
1.txt 2.txt 3.txt 4.txt 5.txt
第二种推送备份的命令。 rsync_backup@172.16.1.41/backup/后可跟子目录
[root@long0000 ~]# rm -f /backup/* ##清空服务端/backup目录
##将本机/backup目录下的文件推送到服务器的/backup/26目录下
[root@10_1_1_26 /]# rsync -avz /backup/ rsync://rsync_backup@10.1.1.25/backup/26/ --password-file=/etc/rsync.password
sending incremental file list
created directory 26
./
1.txt
2.txt
3.txt
4.txt
5.txt
sent 311 bytes received 139 bytes 900.00 bytes/sec
total size is 0 speedup is 0.00
如果客户端指定服务端的子目录不存在,服务端会自动创建。
[root@long0000 ~]# ls /backup/26/
1.txt 2.txt 3.txt 4.txt 5.txt
第三种命令:列出rsync服务端的文件
[root@10_1_1_26 /]# rsync -v rsync://rsync_backup@10.1.1.25/backup/26/ --password-file=/etc/rsync.password
receiving file list ... done
drwxr-xr-x 71 2018/06/21 02:50:43 .
-rw-r--r-- 0 2018/06/21 02:50:43 1.txt
-rw-r--r-- 0 2018/06/21 02:50:43 2.txt
-rw-r--r-- 0 2018/06/21 02:50:43 3.txt
-rw-r--r-- 0 2018/06/21 02:50:43 4.txt
-rw-r--r-- 0 2018/06/21 02:50:43 5.txt
sent 20 bytes received 120 bytes 25.45 bytes/sec
total size is 0 speedup is 0.00
linux系统日志
系统汇总日志
如果一个服务没有定义自己的日志文件,那么该服务产生的日志都会存在这个文件里面。
/var/log/messages
[root@long0000 ~]# tail -3 /var/log/messages
Jun 21 03:34:55 long0000 rsyncd[71046]: connect from UNKNOWN (10.1.1.26)
Jun 21 03:40:01 long0000 systemd: Started Session 110 of user root.
Jun 21 03:40:01 long0000 systemd: Starting Session 110 of user root.
日志切割
日志文件占用的存储空间总是会越来越大。这时候就需要切割日志了
[root@long0000 ~]# ls /var/log/messages*
/var/log/messages /var/log/messages-20180610 /var/log/messages-20180620
##以上都是CentOS7系统自动配置的日志切割方案。
我们也可以自己配置日志切割方案
配置文件/etc/logrotate.conf
[root@long0000 ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly ##这个表示每周切割1次
# keep 4 weeks worth of backlogs
rotate 4 #保留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.
日志切割参考https://my.oschina.net/u/2000675/blog/908189
dmesg 命令:查看硬件相关的日志。
dmesg -c #清空硬件日志信息。重启之后又会生成。
/var/log/dmesg 日志文件,记录系统启动的信息。
last命令,查看正确的登录记录,调用的文件/var/log/wtmp
[root@long0000 ~]# last -2 #-2为查看最近两次的成功登录记录。
root pts/1 10.1.1.169 Wed Jun 20 20:28 still logged in
root pts/0 10.1.1.169 Wed Jun 20 20:28 still logged in
lastb命令,查看登录失败的用户,对应的文件时/var/log/btmp
[root@long0000 ~]# lastb -1
root tty1 Tue Jun 12 21:32 - 21:32 (00:00)
btmp begins Tue Jun 12 21:32:06 2018
/var/log/secure 系统安全日志:可以查看系统是否有被暴力破解等信息。
[root@long0000 ~]# tail -1 /var/log/secure
Jun 21 04:20:36 long0000 sshd[128255]: Failed password for root from 10.1.1.26 port 49596 ssh2
虚拟终端:Screen工具
简介:
Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。
用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。
GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。
会话恢复
只要Screen本身没有终止,在其内部运行的会话都可以恢复。
这一点对于远程登录的用户特别有用——即使网络连接中断,
用户也不会失去对已经打开的命令行会话的控制。
只要再次登录到主机上执行screen -r就可以恢复会话的运行。
同样在暂时离开的时候,也可以执行分离命令detach,
在保证里面的程序正常运行的情况下让Screen挂起(切换到后台)。
这一点和图形界面下的VNC很相似。
多窗口
在Screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。
用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。
Screen实现了基本的文本操作,如复制粘贴等;还提供了类似滚动条的功能,可以查看窗口状况的历史记录。
窗口还可以被分区和命名,还可以监视后台窗口的活动。
会话共享 Screen可以让一个或多个用户从不同终端多次登录一个会话,并共享会话的所有特性
它同时提供了窗口访问权限的机制,可以对窗口进行密码保护。
Screen常用操作
为了解决远程连接时,因为网络中断等原因导致任务中断的一个简单解决办法:
nohup command &
这个命令可以将command放到后台运行,不会因为网络断开而中断任务。
但是缺点是不能实时的显示任务的输出信息。只能将输出的信息保存到一个文件中。
安装Screen工具
yum install -y screen
直接执行screen 就可以创建一个虚拟终端
先按ctral+a组合键再按d退出虚拟终端,但不是结束
[root@long0000 ~]# screen
退出虚拟终端
[detached from 18022.pts-0.long0000]
[root@long0000 ~]# screen
退出虚拟终端
[detached from 21060.pts-0.long0000]
screen -ls 查看虚拟终端列表
[root@long0000 ~]# screen -ls
There are screens on:
21060.pts-0.long0000 (Detached)
18022.pts-0.long0000 (Detached)
2 Sockets in /var/run/screen/S-root.
screen -r id 进入指定的终端
[root@long0000 ~]# screen -r 18022
screen -S long01 创建一个以long01命名的虚拟终端
[root@long0000 ~]# screen -S long01
退出虚拟终端
[root@long0000 ~]# screen -ls
There are screens on:
27404.long01 (Detached)
21060.pts-0.long0000 (Detached)
18022.pts-0.long0000 (Detached)
3 Sockets in /var/run/screen/S-root.
screen -r long01 利用虚拟终端名进入挂起的虚拟终端
[root@long0000 ~]# screen -r long01
[root@long0000 ~]# screen -ls #在虚拟终端里面查看所有虚拟终端
There are screens on:
27404.long01 (Attached) #这个为当前所在的虚拟终端
21060.pts-0.long0000 (Detached) #这个为挂起的虚拟终端
18022.pts-0.long0000 (Detached)
3 Sockets in /var/run/screen/S-root.
扩展
Linux日志文件总管logrotate http://linux.cn/article-4126-1.html
xargs用法详解 http://blog.csdn.net/zhangfn2011/article/details/6776925