1、rsync:remove sync(远程同步),数据备份工具,可以实现本地同步和远程同步,同步时可以增量同步,只同步不相同的数据,也就是说只同步两个文件不不同的部分,因此速度非常快:
rsync的五种命令格式:注:src表示源文件 dest表示目标文件:
rsync options src dest #本地到本地
rsync options src user@ip地址:/目录 #从本地到远程
rsync options user@ip地址:/目录 dest #从远程主机到本地
rsync options src user@ip地址::dest #从本地到远端主机(两个冒号,验证方式不同)
rsync options user@ip地址::src dest #从远端主机到本地(两个冒号,验证方式不同)
注释:@前面的user可以省略,省略后则是以目标主机的当前用户验证,也可以写成普通用户: rsync -av src xiaoxiao@IP地址:/目录
options:
-a:表示以递归方式传输文件,包含了-rlptgoD选项: -a--no-p
-r:同步目录时需要加此选项,类似于cp的-r选项:
-v:verbose,传输时可视化:
-l:保留软连接,(因为没同步软连接的源文件,所以保留后也无法使用):
-L:同步软连接时,会把软连接所指的源文件也同步过来:
-p:同步时保留源文件的权限:
-o:同步时保留源文g件的属主信息(如www同步后还是www,若不存在,则显示UID):
-g:同步时保留源文件的属组信息(GID):
-D:同步时保留文件的设备信息:
-t:同步时保留文件时间信息:
--delte:同步时会删除DEST(目标目录)里STC(源目录)没有的文件:
--exclude:过滤指定文件,可使用通配(--exclude "*.txt")
-P:显示同步过程(更详细,会显示速率,传输百分比等):
-u:update,如果目标文件比源文件新,则不同步源文件过来(新旧根据mtime来判断的):
-z:传输时压缩文件:
示例1:同步本地文件或者目录: rsync -av test.txt /tmp/123.txt
[root@localhost_001 ~]# rsync -av test.txt /tmp/123.txt #同步test.txt到tmp目录下的123.txt
sending incremental file list
test.txt
sent 89 bytes received 35 bytes 248.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost_001 ~]# ls -ld /tmp/123.txt
-rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/123.txt
[root@localhost_001 ~]# rsync -av test /tmp/test1 #同步test目录到/tmp/目录下test1
sending incremental file list
created directory /tmp/test1
test/
test/123
test/2112 -> 123
test/bac.txt -> /tmp/ipt.txt
sent 409 bytes received 143 bytes 1,104.00 bytes/sec
total size is 15 speedup is 0.03
[root@localhost_001 ~]# ls -ld /tmp/test1/
drwxr-xr-x 3 root root 18 8月 14 12:34 /tmp/test1/
示例2:同步本地文件到远程主机: rsync -av test.txt 192.168.149.130:/tmp/123.txt
rsync -av test.txt root@192.168.149.130:/tmp/123.txt
同步本机的test.txt文件到远端主机192.168.149.130的tmp目录下:
[root@localhost_001 ~]# rsync -av test.txt root@192.168.149.130:/tmp/1231.txt
root@192.168.149.130's password:
sending incremental file list
test.txt
sent 89 bytes received 35 bytes 27.56 bytes/sec
total size is 0 speedup is 0.00
在远端主机130下查看:
[root@localhost_002 ~]# ls -ld /tmp/1231.txt
-rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/1231.txt
注释:执行远程同步时,两端主机均需要安装rsync: yum install -y rsync
示例3:同步远端主机文件到本地: rsync -av root@192.168.149.130:/tmp/1121.txt /tmp/test.txt
[root@localhost_001 ~]# rsync -av 192.168.149.130:/tmp/1231.txt /tmp/test.txt
root@192.168.149.130's password:
receiving incremental file list
1231.txt
sent 43 bytes received 89 bytes 52.80 bytes/sec
total size is 0 speedup is 0.00
[root@localhost_001 ~]# ls -ld /tmp/test.txt
-rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/test.txt
rsync同步时采用的是ssh协议,需要借助其端口,有时如果端口发生改变,则需要加 -e “ssh -p 56588”
同步本机文件/tmp/test.txt到远端主机130的tmp目录下,通过其ssh端口“52588”
[root@localhost_001 ~]# rsync -av -e "ssh -p 52588" /tmp/test.txt 192.168.149.130:/tmp/test1.txt
root@192.168.149.130's password:
sending incremental file list
test.txt
sent 89 bytes received 35 bytes 35.43 bytes/sec
total size is 0 speedup is 0.00
在远端主机130上查看其文件:
[root@localhost_002 ~]# ls -ld /tmp/test1.txt
-rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/test1.txt
示例4:--delte:删除目标目录中 源文件没有的文件:
[root@localhost_001 test]# ls /root/test #查看其源文件
123 2112 234 4356 567 bac.txt dir1 dir2
[root@localhost_001 test]# ls /tmp/test/
123 2112 222 234 4356 567 bac.txt dir1 dir2 #查看其目标文件
[root@localhost_001 test]# rsync -av --delete /root/test/ /tmp/test/ #同步操作
sending incremental file list
deleting 222
./
sent 225 bytes received 28 bytes 506.00 bytes/sec
total size is 15 speedup is 0.06
示例5:--exclude :
[root@localhost_001 ~]# ls test #查看文件,发现有.txt的文件存在:
123 2112 234 4356 567 bac.txt dir1 dir2
[root@localhost_001 ~]# rsync -av --exclude "*.txt" /root/test/ /tmp/test/ #过滤不包含*.txt
sending incremental file list
created directory /tmp/test
./
123
2112 -> 123
234
4356
567
dir1/
dir2/
sent 355 bytes received 138 bytes 986.00 bytes/sec
total size is 3 speedup is 0.01
[root@localhost_001 ~]# ls /tmp/test/ #再次查看文件,发现没有txt的文件:
123 2112 234 4356 567 dir1 dir2
支持过滤多个选项,需要加上多个--exclude:
[root@localhost_001 ~]# rsync -av --exclude "*.txt" --exclude "1*" /root/test/ /tmp/test/
示例4:同步软连接:需要加大L同步其源文件,不然无法使用:
在001机器上rsync同步到002机器上:
[root@localhost_001 ~]# rsync -avL -e "ssh -p 52588" /root/test/ root@192.168.149.130:/tmp/test1/
root@192.168.149.130's password:
bac.txt -> /tmp/ipt.txt
dir1/
dir2/
sent 393 bytes received 146 bytes 154.00 bytes/sec
total size is 15 speedup is 0.03
示例5:同步是如果目标文件比源文件新则不同步: -u update
001主机操作:
[root@localhost_001 ~]# rsync -av -e "ssh -p 52588" /root/test.txt 192.168.149.130:/root/test1.txt
root@192.168.149.130's password:
sending incremental file list
test.txt
sent 101 bytes received 35 bytes 54.40 bytes/sec
total size is 7 speedup is 0.05
002主机:
[root@localhost_002 ~]# cat test1.txt
dsjdfl;jsafkla;sjf;klsafjk;lsdfjkals;f
sdfjksdfjs'ad
fsaljflfasdf
示例6:显示详细的过程,如速率、百分比等: -p
[root@localhost_001 ~]# rsync -avP -e "ssh -p 52588" /root/test/ root@192.168.149.130:/tmp/test1/
root@192.168.149.130's password:
sending incremental file list
created directory /tmp/test1
./
123
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=7/9)
2112 -> 123
234
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=5/9)
4356
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/9)
567
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=3/9)
bac.txt -> /tmp/ipt.txt
dir1/
dir2/
sent 393 bytes received 146 bytes 119.78 bytes/sec
total size is 15 speedup is 0.03
2、客户端连接服务端需要输入密码,若想在命令行中带上密码,可以设定一个密码文件:/etc/rsyncd.passwd(客户端)
首先在服务端配置/etc/rsyncd.conf下制定验证的用户名和密码所在的配置文件:
[root@localhost_001 ~]# cat /etc/rsyncd.conf |grep -v ^#
port=8730 #指定端口
log file=/var/log/rsync.log #指定日记所在文件
pid file=/var/run/rsyncd.pid #PID所在目录
address=192.168.149.129 #指定启动了rsync服务的IP地址,默认是当前系统的所有IP地址:
[test] #模块名称:
path=/tmp/rsync #所在的路径:
use chroot=false #是否限制在此目录:
max connections=4 #最大连接数:
read only=no #是否需要客户端上传文件:
list=true #是否会在客户端列出模块名称:
uid=root #连接我的时候的用户:
gid=root #连接我的时候的组:
auth users=test #制定登录的用户名
secrets file=/etc/rsyncd.passwd #制定密码所在的配置文件:
hosts allow=192.168.149.130 #允许那些IP地址或者是网段连接我:
如图:在服务端新建文件:/etc/rsyncd.passwd:格式如下:
[root@localhost_001 test1]# cat /etc/rsyncd.passwd
test:12345
用户 :密码
然后在客户端操作推文件和拉文件均需输入密码:
[root@localhost_002 ~]# rsync -avL /tmp/test8/ --port=8730 test@192.168.149.129::test/test1/
Password:
sending incremental file list
./
1.txt
sent 252 bytes received 40 bytes 34.35 bytes/sec
total size is 539 speedup is 1.85
[root@localhost_002 ~]# rsync -avL --port=8730 test@192.168.149.129::test/test1/ /tmp/test8/
Password:
receiving incremental file list
sent 22 bytes received 202 bytes 29.87 bytes/sec
total size is 539 speedup is 2.41
3:有时候我们需要些在shell脚本里,需要自动化操作:在如上的基础上,在客户端新建一个配置文件:里面只写入密码即可:
客户端:
[root@localhost_002 ~]# cat /etc/rsyncd_pass.txt
12345
然后在服务端操作:
[root@localhost_002 ~]# rsync -avL /tmp/test8/ --port=8730 test@192.168.149.129::test/test1/ --password-file=/etc/rsyncd_pass.txt
sending incremental file list
./
55.txt
sent 273 bytes received 40 bytes 56.91 bytes/sec
total size is 539 speedup is 1.72
2、系统日记:配置文件如下:
/var/log/message:系统总日记:(各种各样的都有)
/etc/loogrotate.conf:日记配置切割文件:
dmesg:硬件错误: -c : 清空内存的日记,并重新生成日记:
/var/log/dmesg:系统启动的日记:
last:用于显示用户最近登录信息,单独执行last命令,他会读取/var/log/wtmp(二进制的)的文件:并把该文件打印出来:
[root@localhost_002 ~]# last
root pts/1 192.168.149.135 Tue Aug 21 13:03 still logged in
root pts/1 192.168.149.135 Tue Aug 21 12:41 - 13:02 (00:21)
root pts/0 192.168.149.135 Thu Aug 16 00:37 still logged in
root pts/0 192.168.149.135 Wed Aug 15 23:48 - 00:37 (00:49)
root pts/0 192.168.149.135 Wed Aug 15 22:27 - 23:48 (01:20)
reboot system boot 3.10.0-693.el7.x Wed Aug 15 22:26 - 13:35 (5+15:08)
lastb:显示用户登录错误的信息,可发现异常的登录,单独执行lastb命令,会读取/var/log/btmp(二进制)的文件:列出登录失败的用户名单:
[root@localhost_002 ~]# last
root pts/1 192.168.149.135 Tue Aug 21 13:03 still logged in
root pts/1 192.168.149.135 Tue Aug 21 12:41 - 13:02 (00:21)
root pts/0 192.168.149.135 Thu Aug 16 00:37 still logged in
root pts/0 192.168.149.135 Wed Aug 15 23:48 - 00:37 (00:49)
root pts/0 192.168.149.135 Wed Aug 15 22:27 - 23:48 (01:20)
reboot system boot 3.10.0-693.el7.x Wed Aug 15 22:26 - 13:35 (5+15:08)
logrotate:用于对日记的操作,对系统日记进行切割、压缩和删除,也可以将日记发送到指定邮箱,可以设定每日、每周删除等,只是必须手动编辑配置文件:/etc/logrotate.conf
安装logrotate工具:yum install -y 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)
4、screen:虚拟终端,用户可以通过此命令同时运行多个终端或者命令行会话,并在其间自由切换,提供统一的会话管理:
不让某个任务中断:
安装screen工具:yum install -y screen
语法:sreen 直接进入虚拟终端:
-ls:列出当前在运行的虚拟终端:
-r:后面加id,进入某个虚拟终端:
-S:后面跟名称,虚拟终端名称自定义:
按alt+a,再按d则退出需要screen:
[root@localhost_002 ~]# screen #进入第一个虚拟终端
[detached from 1949.pts-1.localhost_002]
[root@localhost_002 ~]# screen #进入第二个虚拟终端
[detached from 1964.pts-1.localhost_002]
[root@localhost_002 ~]# screen -S "testyou" #进入第三个自定义的虚拟终端
[detached from 1978.testyou]
[root@localhost_002 ~]#
[root@localhost_002 ~]#
[root@localhost_002 ~]# screen -ls #查看当前的虚拟终端
There are screens on:
1978.testyou (Detached)
1964.pts-1.localhost_002 (Detached)
1949.pts-1.localhost_002 (Detached)
3 Sockets in /var/run/screen/S-root.
[root@localhost_002 ~]# screen -r 1978 #进入id为1978的虚拟终端
[detached from 1978.testyou]
[root@localhost_002 ~]# screen -r testyou #也可根据名称进入某一虚拟终端
[detached from 1978.testyou]
进入后可以exit,退出虚拟终端
[root@localhost_002 ~]# screen -ls #查看还剩两个虚拟终端
There are screens on:
1964.pts-1.localhost_002 (Detached)
1949.pts-1.localhost_002 (Detached)
2 Sockets in /var/run/screen/S-root.
扩展:xargs:用作替换工具,用于读取输入数据重新格式化后输出:
[root@localhost_002 ~]# cat test
1 2
3 4
a b
c d
[root@localhost_002 ~]# cat test|xargs
1 2 3 4 a b c d
[root@localhost_002 ~]# cat test|xargs -n3
1 2 3
4 a b
c d
[root@localhost_002 ~]# cat test|xargs -n2
1 2
3 4
a b
c d