1、 scp远程传输(完全备份)

scp命令:
scp 本地文件 远程主机用户@远程主机ip:远程主机目录的绝对路径
scp 远程主机用户@远程主机ip:远程主机文件的绝对路径 本地文件

实验环境
node1:172.25.254.100
node2:172.25.254.200

实验步骤:
在node2里建立实验素材

[root@node2 Desktop]# touch westos 建立文件
[root@node2 Desktop]# mkdir westosdir 建立目录

测试

ssh-key
ssh-copy-id -i id_rsa.pub root@172.25.254.100  做免密
[root@node2 Desktop]# scp westos root@172.25.254.100:/root/Desktop/   将node2上的weostos文件远程复制到node1的桌面上
westos                                        100%    0     0.0KB/s   00:00  
[root@node2 Desktop]# scp -r westosdir/ root@172.25.254.100:/root/Desktop/  将node2上的weostosdir目录远程复制到node1的桌面上,传输目录需要加 -r
[root@node2 Desktop]# scp -r westos westosdir/ root@172.25.254.100:/root/Desktop/  文件和目录可以一起远程传输
westos                                        100%    0     0.0KB/s   00:00  

[root@node2 Desktop]# scp root@172.25.254.100:/home/westos/Desktop/westos  /mnt/   把远程文件westos复制到本地/mnt 目录
  /mnt/
westos                                                                                                100%    0     0.0KB/s   00:00 

[root@node2 Desktop]# scp -qr westos westosdir/ root@172.25.254.100:/root/Desktop/   传输时不显示进度
[root@node2 Desktop]# time scp -qr westos westosdir/ root@172.25.254.100:/root/Desktop/ 查询传输时间

real	0m0.314s
user	0m0.016s
sys	0m0.005s

2、rsync(可以做增量备份,需要先把服务认证/免密认证做好)

命令功能
rsync -r复制目录
rsync -l复制链接
rsync -p复制权限
rsync -t复制时间戳
rsync -o复制拥有者
rsync -g复制拥有组
rsync -D复制设备文件

实验步骤

[root@node2 Desktop]# cd /mnt/
[root@node2 mnt]# touch westosfile{1..3}
[root@node2 mnt]# mkdir westos
[root@node2 mnt]# touch westos/lee{1..3}
[root@node2 mnt]# ln -s /mnt/westosfile1 /mnt/westoslinux
[root@node2 mnt]# ls -Rl /mnt/
[root@node2 mnt]# chmod 777 * -R  
[root@node2 mnt]# chown westos.westos * -R
[root@node2 mnt]# ls -Rl /mnt/ 查看文件属性
/mnt/:
total 0
drwxrwxrwx. 2 westos westos 42 Sep  7 21:49 westos
-rwxrwxrwx. 1 westos westos  0 Sep  7 21:48 westosfile1
-rwxrwxrwx. 1 westos westos  0 Sep  7 21:48 westosfile2
-rwxrwxrwx. 1 westos westos  0 Sep  7 21:48 westosfile3
lrwxrwxrwx. 1 westos westos 16 Sep  7 21:49 westoslinux -> /mnt/westosfile1

/mnt/westos:
total 0
-rwxrwxrwx. 1 westos westos 0 Sep  7 21:49 lee1
-rwxrwxrwx. 1 westos westos 0 Sep  7 21:49 lee2
-rwxrwxrwx. 1 westos westos 0 Sep  7 21:49 lee3

[root@node2 mnt]# rsync -r /mnt/westos root@172.25.254.100:/mnt  同步 ,注意目录westos不加/表示同步目录本身以及里面的内容
Every 2.0s: ls -Rl /mnt/              node1.westos.org: Tue Sep  7 22:07:01 2021
/mnt/:
total 0
drwxr-xr-x. 2 root root 42 Sep  7 22:06 westos
/mnt/westos:
total 0
-rwxr-xr-x. 1 root root 0 Sep  7 22:06 lee1
-rwxr-xr-x. 1 root root 0 Sep  7 22:06 lee2
-rwxr-xr-x. 1 root root 0 Sep  7 22:06 lee3

[root@node2 mnt]# rsync -r /mnt/westos/ root@172.25.254.100:/mnt 目录westos后面不加/表示同步目录里面的内容
Every 2.0s: ls -Rl /mnt/              node1.westos.org: Tue Sep  7 22:04:51 2021
/mnt/:
total 0
-rwxr-xr-x. 1 root root 0 Sep  7 22:00 lee1
-rwxr-xr-x. 1 root root 0 Sep  7 22:00 lee2
-rwxr-xr-x. 1 root root 0 Sep  7 22:00 lee3
[root@node2 mnt]# rsync -r /mnt/ root@172.25.254.100:/mnt 同步整个mnt时里面包含的连接不能同不到mode1上
skipping non-regular file "westoslinux"
[root@node2 mnt]# rsync -lr /mnt/ root@172.25.254.100:/mnt  加上-l 就可以同步连接
Every 2.0s: ls -Rl /mnt/              node1.westos.org: Tue Sep  7 22:11:56 2021
/mnt/:
total 0
drwxr-xr-x. 2 root root 42 Sep  7 22:11 westos
-rwxr-xr-x. 1 root root  0 Sep  7 22:11 westosfile1
-rwxr-xr-x. 1 root root  0 Sep  7 22:11 westosfile2
-rwxr-xr-x. 1 root root  0 Sep  7 22:11 westosfile3
lrwxrwxrwx. 1 root root 16 Sep  7 22:11 westoslinux -> /mnt/westosfile1

/mnt/westos:
total 0
-rwxr-xr-x. 1 root root 0 Sep  7 22:11 lee1
-rwxr-xr-x. 1 root root 0 Sep  7 22:11 lee2
-rwxr-xr-x. 1 root root 0 Sep  7 22:11 lee3
/mnt/westos:
total 0
-rwxr-xr-x. 1 root root 0 Sep  7 22:11 lee1
-rwxr-xr-x. 1 root root 0 Sep  7 22:11 lee2
-rwxr-xr-x. 1 root root 0 Sep  7 22:11 lee3

root@node2 mnt]# rsync -lrp /mnt/ root@172.25.254.100:/mnt  -p表示同步权限
Every 2.0s: ls -Rl /mnt/              node1.westos.org: Tue Sep  7 22:14:43 2021
/mnt/:
total 0
drwxrwxrwx. 2 root root 42 Sep  7 22:13 westos     权限都被同步了
-rwxrwxrwx. 1 root root  0 Sep  7 22:13 westosfile1 权限都被同步了
-rwxrwxrwx. 1 root root  0 Sep  7 22:13 westosfile2 权限都被同步了
-rwxrwxrwx. 1 root root  0 Sep  7 22:13 westosfile3 权限都被同步了
lrwxrwxrwx. 1 root root 16 Sep  7 22:11 westoslinux -> /mnt/westosfile1

/mnt/westos:
total 0
-rwxrwxrwx. 1 root root 0 Sep  7 22:13 lee1 权限都被同步了
-rwxrwxrwx. 1 root root 0 Sep  7 22:13 lee2 权限都被同步了
-rwxrwxrwx. 1 root root 0 Sep  7 22:13 lee3 权限都被同步了

root@node2 mnt]# rsync -lrpog /mnt/ root@172.25.254.100:/mnt  同步用户和组,o表示所有人,g表示所有组
Every 2.0s: ls -Rl /mnt/              node1.westos.org: Tue Sep  7 22:20:23 2021

/mnt/:
total 0
drwxrwxrwx. 2 westos westos 42 Sep  7 22:17 westos   变为westos了
-rwxrwxrwx. 1 westos westos  0 Sep  7 22:17 westosfile1 变为westos了
-rwxrwxrwx. 1 westos westos  0 Sep  7 22:17 westosfile2 变为westos了
-rwxrwxrwx. 1 westos westos  0 Sep  7 22:17 westosfile3 变为westos了
lrwxrwxrwx. 1 westos westos 16 Sep  7 22:11 westoslinux -> /mnt/westosfile1

/mnt/westos:
total 0
-rwxrwxrwx. 1 westos westos 0 Sep  7 22:17 lee1 变为westos了
-rwxrwxrwx. 1 westos westos 0 Sep  7 22:17 lee2 变为westos了
-rwxrwxrwx. 1 westos westos 0 Sep  7 22:17 lee3 变为westos了

[root@node2 mnt]# rsync -lrpogt /mnt/ root@172.25.254.100:/mnt   t表示同步时间

Every 2.0s: ls -Rl /mnt/              node1.westos.org: Tue Sep  7 22:21:58 2021
/mnt/:
total 0
drwxrwxrwx. 2 westos westos 42 Sep  7 21:49 westos  时间被同步了
-rwxrwxrwx. 1 westos westos  0 Sep  7 21:48 westosfile1
-rwxrwxrwx. 1 westos westos  0 Sep  7 21:48 westosfile2
-rwxrwxrwx. 1 westos westos  0 Sep  7 21:48 westosfile3
lrwxrwxrwx. 1 westos westos 16 Sep  7 21:49 westoslinux -> /mnt/westosfile1

/mnt/westos:
total 0
-rwxrwxrwx. 1 westos westos 0 Sep  7 21:49 lee1
-rwxrwxrwx. 1 westos westos 0 Sep  7 21:49 lee2
-rwxrwxrwx. 1 westos westos 0 Sep  7 21:49 lee3

同步设备文件
[root@node2 mnt]# rsync -Dr /dev/pts root@172.25.254.100:/mnt -D表示同步设备文件
/mnt/pts:
total 0
crw-------. 1 root root 136, 0 Sep  7 22:24 0
c---------. 1 root root   5, 2 Sep  7 22:24 ptmx

3、文件的归档打包(提高传输效率)

tar

命令功能
c创建
f指定文件名称
x解档
v显示过程
t查看
r向归档文件中添加文件
–delete删除指定文件
–get解档指定文件
-C指定解档路径
-Pdon’t remove “/”
[root@node2 mnt]# tar cf /root/Desktop/etc.tar /etc 打包  c表示创建 f表示指定文件名 
[root@node2 Desktop]# ls
mnt.tar    打包成功
[root@node2 Desktop]# tar xf etc.tar  x表示解档
[root@node2 Desktop]# ls
etc  etc.tar
[root@node2 Desktop]# tar xf etc.tar -C /mnt/  -C表示指定解档路经
[root@node2 Desktop]# cd /mnt/
[root@node2 mnt]# ls
etc
[root@node2 Desktop]# tar tf etc.tar 查看压缩包里面的东西
etc/
etc/mtab
etc/fstab
etc/crypttab
etc/dnf/
etc/dnf/modules.d/
etc/dnf/modules.d/container-tools.module
etc/dnf/modules.d/llvm-toolset.module
[root@node2 Desktop]# touch file  新建文件
[root@node2 Desktop]# tar rf etc.tar file  向归档文件中添加文件
[root@node2 Desktop]# tar f etc.tar --delete file 将归档文件的每个文件删除
[root@node2 Desktop]# tar f etc.tar --get etc/resolv.conf  将归档文件里的某一个解出来用--get
[root@node2 etc]# cd /root/Desktop/etc/  
[root@node2 etc]# ls
resolv.conf
[root@node2 Desktop]# tar cvf etc.tar /etc/  v表示显示过程
/etc/qemu-ga/
/etc/qemu-ga/fsfreeze-hook
/etc/qemu-ga/fsfreeze-hook.d/
/etc/sudo-ldap.conf
[root@node2 Desktop]# ls 
etc.tar
[root@node2 Desktop]# tar xvf etc.tar  解包显示过程
etc/mime.types
etc/vconsole.conf
etc/locale.conf
[root@node2 Desktop]# ls
etc  etc.tar
[root@node2 Desktop]# tar cf etc.tar /etc/ 归档后文件/路径会删除
etc/pbm2ppa.conf
etc/pnm2ppa.conf
etc/mailcap
etc/mime.types
[root@node2 Desktop]# tar cfP etc.tar /etc/  p表示不去删除文件/路径
/etc/pbm2ppa.conf
/etc/pnm2ppa.conf
/etc/mailcap
/etc/mime.types

4、文件的压缩

格式有gz bzip2(bz2) zip xz rar(闭源,在linux 中没有)

zip

命令功能
zip -r mnt.tar.zip mnt.tar#zip格式压缩
unzip mnt.tar.zip#zip格式解压缩
[root@node2 mnt]# tar cf etc.tar /etc 打包
tar: Removing leading `/' from member names
[root@node2 mnt]# du -sh etc.tar 
28M	etc.tar 
[root@node2 mnt]# zip -r etc.tar.zip  etc.tar  压缩
  adding: etc.tar (deflated 75%)
[root@node2 mnt]# du -sh etc.tar.zip 
6.8M	etc.tar.zip   从28M变成6.8M
[root@node2 mnt]# unzip etc.tar.zip  解压缩
Archive:  etc.tar.zip
 inflating: etc.tar                 
[root@node2 mnt]# ls
etc.tar  etc.tar.zip

gzip

命令功能
gzip mnt.tar#gzip格式压缩
gunzip mnt.tar.gz#gzip格式解压缩
[root@node2 mnt]# gzip etc.tar  压缩
[root@node2 mnt]# du -sh etc.tar.gz  
6.8M	etc.tar.gz
[root@node2 mnt]# gunzip etc.tar.gz 解压缩
[root@node2 mnt]# ls
etc.tar

bzip

命令功能
bzip2 mnt.tar#bzip2格式压缩
bunzip2 etc.tar.bz2#bzip2格式解压缩
[root@node2 mnt]# bzip2 etc.tar  压缩
[root@node2 mnt]# du -sh etc.tar.bz2 
5.3M	etc.tar.bz2 
[root@node2 mnt]# bunzip2 etc.tar.bz2   解压缩
[root@node2 mnt]# ls
etc.tar

xz

命令功能
xz mnt.tar#xz格式压缩
unxz mnt.tar.xz#xz格式解压缩
[root@node2 mnt]# xz etc.tar 
[root@node2 mnt]# ls
etc.tar.xz
[root@node2 mnt]# du -sh etc.tar.xz 
4.5M	etc.tar.xz
[root@node2 mnt]# unxz etc.tar.xz 
[root@node2 mnt]# ls
etc.tar

[root@node2 mnt]# tar zcf etc.tar.gz /etc/
tar: Removing leading `/’ from member names

5、tar+压缩 (打包压缩一步完成)

gzip

tar zcf etc.tar.gz /etc
tar zxf etc.tar.gz

[root@node2 mnt]# tar zcf etc.tar.gz /etc/
tar: Removing leading `/' from member names
[root@node2 mnt]# tar zxf etc.tar.gz 解压

bzip2

tar jcf etc.tar.bz2 /etc
tar jxf etc.tar.bz2

[root@node2 mnt]# tar jcf etc.tar.bz2 /etc/ 打包压缩
tar: Removing leading `/' from member names
[root@node2 mnt]# tar jxf etc.tar.bz2  解压

xz

tar Jcf etc.tar.xz /etc
tar Jxf etc.tar.xz

[root@node2 mnt]# tar Jcf etc.tar.xz /etc/  打包压缩
tar: Removing leading `/' from member names
[root@node2 mnt]# tar Jxf etc.tar.xz  解压
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐