rsync, 系统日志, screen工具

linux文件同步工具 - rsync

同步工具,比CP命令不同的地方是可以实现增量同步,减少很多无必要的网络流量和降低工作难度。

 rsync -av /etc/passwd /tmp/1.txt   #源文件passwd,同步成1.txt,-av可同步文件和目录,并看到同步的文件;
rsync -av /tmp/rsyn1 172.16.92.142:/tmp/rsyn2/		#同步目录,这个命令会把rsyn1目录和内容放在rsyn2目录下;
rsync -av /tmp/rsyn1/ 172.16.92.142:/tmp/rsyn2/	
#这个命令会把目录rsyn1下文件放在rsyn2目录下,感觉这个方式前后路径完全一致,
#容易理解,还可以修改同步后的目录名;
rsync 172.16.92.142:/tmp/rs2/ /tmp/rs1/			#从远端拉文件;

rsync其他参数

rsync -a  
 #等于rsync -rtplgoD, -r用于同步目录,-l保留软链接 ,-p保持文件的权限属性,-o保持文件的属主,
 #-g保持文件的属组,-D 保持设备文件信息,-t保持文件的时间属性;
rsync -avL	#-L把软链接的源文件同步到目的地;
rsync -avP	#-P显示同步过程,增加速率的显示;
rsync -avu	#-u目的地的文件版本比源版本新,则不更新;
rsync -avz 	#-z传输时压缩;


[root@tanyvlinux rsyn1]# rsync -av --delete /tmp/rsyn1/ 172.16.92.142:/tmp/rsyn2/	
#--delete删除目的地 中 源 没有的文件,原来是增量同步;
root@172.16.92.142's password: 
sending incremental file list
deleting rsyn1/3.txt
deleting rsyn1/2.txt
deleting rsyn1/1.txt
deleting rsyn1/

sent 104 bytes  received 67 bytes  26.31 bytes/sec
total size is 0  speedup is 0.00

[root@tanyvlinux rsyn1]# touch 1.log 2.log 4.txt
[root@tanyvlinux rsyn1]# rsync -av --exclude "*.log" /tmp/rsyn1/ 172.16.92.142:/tmp/rsyn2/	
#--exclude排除某些文件不同步;
root@172.16.92.142's password: 
sending incremental file list
./
4.txt

sent 166 bytes  received 38 bytes  45.33 bytes/sec
total size is 0  speedup is 0.00

[root@tanyvlinux rsyn1]# ll /tmp/bigfile
-rw-r--r-- 1 root root 720749 9月  25 01:22 /tmp/bigfile
[root@tanyvlinux rsyn1]# rsync -avPz /tmp/bigfile 172.16.92.142:/tmp/rsyn2/	#-z传输时压缩;
root@172.16.92.142's password: 
sending incremental file list
bigfile
        720,749 100%   13.96MB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 190,450 bytes  received 35 bytes  42,330.00 bytes/sec		#传输量比文件小;
total size is 720,749  speedup is 3.78
[root@tanyvlinux rsyn1]# gzip /tmp/bigfile && ls -lh /tmp/bigfile.gz
-rw-r--r-- 1 root root 186K 9月  25 01:22 /tmp/bigfile.gz			#跟压缩后大小差不多;

ssh方式同步

rsync -av -e "ssh -p 1122" test1/ 192.168.133.132:/tmp/test2/  	
#可以结合iptable nat表的应用,外网机器ssh内外网机器:1122 <-> 内网机器:22,可通过一台机器从外部访问只具内网连接的机器;
#结合命令同步内容,试验可行;

开启rsync服务

  • 修改配置文件,添加内容如下:
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.31.128
[test]
path=/tmp/rsync1
use chroot=true
max connections=4
read only=no
list=false
uid=root
gid=root
#auth users=test
#secrets file=/etc/rsyncd.passwd
hosts allow=192.168.31.130
  • 要修改/tmp/rsync1权限为777, 目录可写进读出;
  • 测试:
[root@tanyvlinuxn2 rsync2]# rsync -avP bigfile 192.168.31.128::test/bigfile	
	#模块test代表了路径/tmp/rsync1,直接在后面添加内容即可;
sending incremental file list
bigfile
        720,749 100%   32.81MB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 721,006 bytes  received 35 bytes  1,442,082.00 bytes/sec
total size is 720,749  speedup is 1.00


  • 配置文件2 - 修改端口后要重启服务
port=8730				#可指定,默认873;
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.31.128
[test]						#模块名;可增加模块,要增加目录path;
path=/tmp/rsync1		#读写的路径;
use chroot=false		#设置rsync的活动目录可以超出/tmp/rsync1;
max connections=4	# 最大连接数;
read only=no				#如果为yes, 不能上传文件到服务端目录里,只能从服务端下载;
list=true						#是否提供列出模块名的服务;
uid=root						#指定传输时以哪个用户的身份传输;
gid=root						#指定传输时以哪个组的身份传输;
auth users=test			#连接时使用的帐号;
secrets file=/etc/rsyncd.passwd		#设置帐号的文件,文件内容为test:PW,文件权限为600;
hosts allow=192.168.31.130 1.1.1.1			#允许连接到该模块的主机,多个IP空格分开;
  • 测试2:
    rsync服务端
[root@tanyvlinux rsync1]# ll
总用量 0
lrwxrwxrwx 1 root root 12 9月  25 10:36 bflink -> /tmp/bigfile	#软链接文件;

rsync客户端

[root@tanyvlinuxn2 rsync2]# rsync -avP --port 8730 test@192.168.31.128::		
#改变了默认连接端口;配置list=true可以列出模块名;为false时返回空;
test           	#模块名;
[root@tanyvlinuxn2 rsync2]# rsync -avPL --port 8730 192.168.31.128::test/bflink /tmp/rsync2/bflink		
#-L复制链接原文件,由于use chroot=false,所以成功复制;如果为true就会失败,rsync活动不能超出/tmp/rsync1;
Password: 								#由于指定密码文件,需要输入密码;
receiving incremental file list
./
bflink
        720,749 100%   32.73MB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 46 bytes  received 721,028 bytes  160,238.67 bytes/sec
total size is 720,749  speedup is 1.00
[root@tanyvlinuxn2 rsync2]# ll
总用量 2112
-rw-r--r--. 1 root root 720749 9月  25 01:22 bflink			#把服务端软链接文件的源文件拉过来了;
-rw-r--r--. 1 root root 720749 9月  25 01:22 bigfile

[root@tanyvlinuxn2 rsync2]# vim /tmp/rsync.passwd		#生成密码文件,用于登陆服务器,内容为PW;
[root@tanyvlinuxn2 rsync2]# chmod 600 /tmp/rsync.passwd	#权限600
[root@tanyvlinuxn2 rsync2]# rsync -avP bigfile --port 8730 test@192.168.31.128::test/ --password-file=/tmp/rsync.passwd		
#上传一个文件,--password-file指定密码文件;
sending incremental file list
bigfile
        720,749 100%   27.34MB/s    0:00:00 (xfr#1, to-chk=0/1)

sent 721,006 bytes  received 35 bytes  1,442,082.00 bytes/sec
total size is 720,749  speedup is 1.00

关于rsyncd配置文件uid,gid试验总结:

  • 1
    配置文件:
    uid=%RSYNC_USER_NAME%
    gid=*
    同步目录权限777
    同步成功,写入的文件都显示nobody,即以nobody写入,理论上更安全。

    目录权限775都不能同步成功,nobody权限很低。

需要上传的文件如果属主是root:root那这个同步会失败,但是改成属组是普通用户后会成功,即文件改成root:user1; 可能是因为rsyncd是nobody, 只能取得普通用户权限,同步文件,无法取得root取限;可能跟chroot也有关系。

  • 2
    配置文件:
    uid=root
    gid=root
    同步目录权限000

    同步都可成功,风险应该比较高。

参考信息:
https://www.samba.org//ftp/rsync/rsyncd.conf.html

系统日志

  • /var/log/messages
    总的日志,系统的日志
  • 日志切割
    logrotate命令;
    配置文件:/etc/logrotate.conf
    /etc/logrotate.d (目录)

更多信息:
https://my.oschina.net/u/2000675/blog/908189

  • dmesg
    硬件相关日志,保存在内存
    dimes -c清空

  • /var/log/dmesg
    系统启动记录的日志

  • last
    登陆系统的记录
    调用/var/log/wtmp文件,不能直接cat

  • lastb
    登陆系统失败的记录
    调用/var/log/btmp,不能直接cat

  • /var/log/secure
    登陆系统的相关信息

screen工具

  • 后台不中断运行,并把输出写在文档里
nohup command &		#command后台运行;
tail -f nohup.out			#查看运行信息;
  • screen
yum install screen		#安装;

screen				#直接回车就进入了虚拟终端,在终端里的命令一直在运行,随时回到界面里查看;
#退出ssh后也可再进入screen;
#ctrl a组合键再按d退出虚拟终端,但不是结束;   
screen -ls 			#查看虚拟终端列表;  
screen -r id 		#进入指定的终端;
screen -S aming   #命名一个虚拟终端;
screen -r aming		#用名字恢复一个终端;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值