一、Linux的rsync命令
1.rsync与scp的区别
scp是复制和rsync是同步,rsync和scp在文件夹均不存在时,执行时间相差不大,但是文件夹存在的情况下差异很大。因为scp是复制:若目的地文件不存在则新建,若存在则暴力覆盖(改变属性)。rsync则是同步,比较两边文件是否相同,相同则保留源文件,若存在差异就直接更新,而且rsync远程拷贝可以附带软链接/硬链接。(参数-l 保留软链接,-H 保留硬链接)。
视情况来选择rsync或scp,若是同步作用则rsync会快一些,若是复制作用时两者均可(目的地无文件)。
rsync [参数] filename username@ip:/dir 具体参数含义: -r ##同步目录 -l ##不忽略链接 -p ##不忽略文件权限 -t ##不忽文件时间戳 -g ##不忽文件所有组 -o ##不忽文件所有人 -D ##不忽略设备文件
2.建立实验环境
[root@Desktop ~]# hostnamectl set-hostname node1 ##虚拟机Desktop更名 [root@server ~]# hostnamectl set-hostname node2 ##虚拟机Sercer更名
[root@node1 ~]cd /mnt ##搭建实验环境移动目录至/mnt [root@node1 mnt]# touch westos{1..5} [root@node1 mnt]# ll total 0 -rw-r--r--. 1 root root 0 Oct 15 11:07 westos1 -rw-r--r--. 1 root root 0 Oct 15 11:07 westos2 -rw-r--r--. 1 root root 0 Oct 15 11:07 westos3 -rw-r--r--. 1 root root 0 Oct 15 11:07 westos4 -rw-r--r--. 1 root root 0 Oct 15 11:07 westos5 [root@node1 mnt]# chown student.student * [root@node1 mnt]# ll total 0 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos1 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos2 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos3 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos4 -rw-r--r--. 1 student student 0 Oct 15 11:07 westos5
[root@node1 mnt]# chmod 777 westos* [root@node1 mnt]# ll total 0 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5 [root@node1 mnt]# ln -s westos1 file1 [root@node1 mnt]# ll total 0 lrwxrwxrwx. 1 root root 7 Oct 15 11:12 file1 -> westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5
2.练习实验开始
注意 :时刻注意rysnc传输过去后在node2中的属性变化,分辨参数的含义
[root@node1 mnt]# rsync -r /mnt/* [email protected]:/mnt/ skipping non-regular file "file1" ##跳过非普通文件fiel快捷方式
[root@node2 ~]# cd /mnt ##位移到目录/mnt下方便查询 [root@node2 mnt]# ll total 0 -rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos1 -rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos2 -rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos3 -rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos4 -rwxr-xr-x. 1 root root 0 Oct 15 11:25 westos5
[root@node1 ~]# rsync -rp /mnt/* [email protected]:/mnt/ skipping non-regular file "file"
[root@node2 mnt]# ll total 0 -rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos1 -rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos2 -rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos3 -rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos4 -rwxrwxrwx. 1 root root 0 Oct 15 11:27 westos5
[root@node1 ~]#rsync -rpo /mnt/* [email protected]:/mnt/ skipping non-regular file "file"
[root@node2 mnt]# ll total 0 -rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos1 -rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos2 -rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos3 -rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos4 -rwxrwxrwx. 1 student root 0 Oct 15 11:29 westos5
[root@node1 ~]# rsync -rpog /mnt/* [email protected]:/mnt/ skipping non-regular file "file"
[root@node2 mnt]# ll total 0 -rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos2 -rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos3 -rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos4 -rwxrwxrwx. 1 student student 0 Oct 15 11:30 westos5
[root@node1 ~]#rsync -rpogt /mnt/* [email protected]:/mnt/ skipping non-regular file "file"
[root@node2 mnt]# ll total 0 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5
[root@node1 ~]# rsync -rpogtl /mnt/* [email protected]:/mnt/
[root@node2 mnt]# ll total 0 lrwxrwxrwx. 1 root root 7 Oct 15 11:12 file1 -> westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos1 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos2 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos3 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos4 -rwxrwxrwx. 1 student student 0 Oct 15 11:07 westos5
rysnc的练习测试到此结束
二、文件的归档及压缩
1.文件归档的定义及作用
文件归档,就是把多个文件/目录变成一个归档文件
归档的作用:加快文件传输速度
2.tar命令
tar + 参数 ##tar命令的主旨是为了加快文件的传输 参数含义: c ##创建 f ##指定归档文件名称 t ##显示归档文件中的内容 r ##向归档文件中添加文件 --get ##取出单个文件 --delete ##删除单个文件 x ##取出归档文件中的所有内容 -C ##指定解档目录 -z ##gz格式压缩 -j ##bz2格式压缩 -J ##xz格式压缩
3.tar命令的练习
[root@node1 Desktop]# tar cf etc.tar /etc/ /mnt ##tar归档 c创建一个归档 f指定归档文件名称 tar: Removing leading `/' from member names [root@node1 Desktop]# tar tf etc.tar ##t查看归档文件内容
[root@node1 Desktop]# tar -rf etc.tar file ##r向归档文件中添加文件
[root@node1 Desktop]# tar -xf etc.tar ##取出归档中所有内容
[root@node1 Desktop]# tar -f etc.tar --get file ##get 取出归档文件中单个文件 [root@node1 Desktop]# tar -f etc.tar --get mnt
[root@node1 Desktop]# tar -f etc.tar --delete mnt ##delete删除单个文件
[root@node1 Desktop]# tar -xf etc.tar -C /mnt ##指定解档目录
[root@node1 Desktop]# tar zcf etc.tar.gz . ##归档文件压缩为gz格式 tar: .: file changed as we read it [root@node1 Desktop]# du -sh etc.tar.gz ##查看压缩文件大小 8.4M etc.tar.gz [root@node1 Desktop]# ls ##查看目录为解压处理环境 etc.tar etc.tar.gz [root@node1 Desktop]# rm -fr etc.tar [root@node1 Desktop]# ls etc.tar.gz [root@node1 Desktop]# tar zxf etc.tar.gz . ##格式解压 [root@node1 Desktop]# ls etc.tar etc.tar.gz
[root@node1 Desktop]# tar jcf etc.tar.bz2 . ##将归档文件压缩成bz2格式 tar: .: file changed as we read it [root@node1 Desktop]# du -sh etc.tar.bz2 ##查看文件bz2压缩格式大小 7.0M etc.tar.bz2 [root@node1 Desktop]# ls ##查看环境为解压清理环境 etc.tar etc.tar.bz2 [root@node1 Desktop]# rm -fr etc.tar [root@node1 Desktop]# ls etc.tar.bz2 [root@node1 Desktop]# tar jxf etc.tar.bz2 . ##解压bz2格式 [root@node1 Desktop]# ls etc.tar etc.tar.bz2
[root@node1 Desktop]# tar Jcf etc.tar.xz . tar: .: file changed as we read it [root@node1 Desktop]# du -sh etc.tar.xz 7.5M etc.tar.xz [root@node1 Desktop]# ls etc.tar etc.tar.xz [root@node1 Desktop]# rm -fr etc.tar [root@node1 Desktop]# ls etc.tar.xz [root@node1 Desktop]# tar Jxf etc.tar.xz . [root@node1 Desktop]# ls etc.tar etc.tar.xz
4.文件压缩
注意:同样大小的文件,不同的格式压缩大小不同且压缩的越小,需要的时间越长。
gz 格式:
[root@node1 Desktop]# gzip etc.tar ##压缩成gz格式 [root@node1 Desktop]# du -sh etc.tar.gz ##查看压缩大小 8.4M etc.tar.gz [root@node1 Desktop]# gunzip etc.tar ##解压gz格式压缩包
bz2 格式:
[root@node1 Desktop]# bzip2 etc.tar ##压缩成bz2格式 [root@node1 Desktop]# du -sh etc.tar.bz2 ##查看bz2压缩大小 7.0M etc.tar.bz2 [root@node1 Desktop]# bunzip2 etc.tar.bz2 ##解压bz2格式压缩包
xz 格式:
[root@node1 Desktop]# xz etc.tar ##压缩成xz格式 [root@node1 Desktop]# du -sh etc.tar.xz ##查看xz压缩文件大小 7.6M etc.tar.xz [root@node1 Desktop]# unxz etc.tar.xz ##解压并解档xz格式压缩包