ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Sysadmin工具:使用rsync管理备份,还原和文件同步

2020-08-13 09:02:34  阅读:288  来源: 互联网

标签:rsync rw 15 -- 备份 Jun Sysadmin skipworthy root


作为一个系统管理员,我把大部分精力都花在两件事上,一件是担心是否有备份,另一件是找出最简单最好的方法。我最喜欢的解决这两个问题的工具之一叫做rsync。
Rsync是由发明Samba的Andrew Tridgell创建的。它是一个非常有用和灵活的工具,它被包含在每一个版本的Linux中,并被移植到其他操作系统中。最简单的说,rsync是一个复制文件的工具。然而,它的功能远不止这些。
它可以让两组文件保持更新和同步。
它可以以命令的形式运行,也可以编写脚本。
它对数据流进行压缩和加密。
它使用多种类型的远程访问客户端(例如SSH和RSH)。
与mvand cp命令一样,最基本的形式rsync只需要一个源和一个目标:

[root@milo enable]# rsync ./foo/testfoo ./bar/

[root@milo enable]# ls -ilR
.:
total 8
5079202 drwxrwxr-x 2 skipworthy skipworthy 4096 Jun 11 15:15 bar
5079201 drwxrwxr-x 2 skipworthy skipworthy 4096 Jun 11 15:08 foo

./bar:
total 8
5001398 -rw-rw-r-- 1 skipworthy skipworthy 8 Jun 11 15:08 testbar
4982446 -rw-rw-r-- 1 root       root       8 Jun 11 15:15 testfoo

./foo:
total 4
5001268 -rw-rw-r-- 1 skipworthy skipworthy 8 Jun 11 15:08 testfoo

我们复制testfoo到bar目录。
现在,让我们在./foo中添加一个文件并重新同步:

[root@milo enable]# touch ./foo/bat.txt

[root@milo enable]# rsync ./foo/* ./bar/
[root@milo enable]# ls -ilR
.:
total 8
5079202 drwxrwxr-x 2 skipworthy skipworthy 4096 Jun 11 15:45 bar
5079201 drwxrwxr-x 2 skipworthy skipworthy 4096 Jun 11 15:25 foo

./bar:
total 8
4992599 -rw-r--r-- 1 root       root       0 Jun 11 15:45 bat.txt
5001398 -rw-rw-r-- 1 skipworthy skipworthy 8 Jun 11 15:08 testbar
4992604 -rw-rw-r-- 1 root       root       8 Jun 11 15:45 testfoo

./foo:
total 4
5002591 -rw-r--r-- 1 root       root       0 Jun 11 15:25 bat.txt
5001268 -rw-rw-r-- 1 skipworthy skipworthy 8 Jun 11 15:08 testfoo

在这一点上,我们要注意几个问题。首先,当我们重新运行rsync时,它重新复制了testfoo并更新了atime。另外,每次复制一个文件时,它都会给文件一个新的inode号。因此,就文件系统而言,它是一个完全不同的文件(因为它是--它每次都复制了所有的信息)。最后,请注意,当我们对文件进行rsync时,它会将文件的所有权改为执行命令的用户(本例中为root)。

如果我们要进行备份,这一切都很重要。这个行为和cp命令是一样的。我们也可以使用 cp 命令来递归复制目录,以及保留属性和所有权。最大的区别是rsync可以对文件进行校验,并比较源文件和目标文件,而cp只是看时间值。Rsync的附加功能对于保存备份的完整性非常有用(我们将在本系列的后面介绍完整性)。

所以让我们只更新其中一个文件,看看rsync的作用。

[root@milo enable]# echo 'this is new text'>>./foo/testfoo
 
[root@milo enable]# ls -al ./foo
 
-rw-rw-r-- 1 skipworthy skipworthy   25 Jun 11 16:13 testfoo

[root@milo enable]# rsync -aruv ./foo/* ./bar/
sending incremental file list
testfoo

sent 194 bytes  received 35 bytes  458.00 bytes/sec
total size is 25  speedup is 0.11

[root@milo enable]# ls -ilR
.:
total 8
5079202 drwxrwxr-x 2 skipworthy skipworthy 4096 Jun 11 16:16 bar
5079201 drwxrwxr-x 2 skipworthy skipworthy 4096 Jun 11 15:56 foo

./bar:
total 8
4992599 -rw-r--r-- 1 root       root        0 Jun 11 15:45 bat.txt
4998080 -rw-r--r-- 1 root       root        0 Jun 11 15:56 footoo.txt
5001398 -rw-rw-r-- 1 skipworthy skipworthy  8 Jun 11 15:08 testbar
4983541 -rw-rw-r-- 1 skipworthy skipworthy 25 Jun 11 16:13 testfoo

./foo:
total 4
5002591 -rw-r--r-- 1 root       root        0 Jun 11 15:25 bat.txt
4997949 -rw-rw-r-- 1 skipworthy skipworthy  0 Jun 11 15:56 footoo.txt
5001268 -rw-rw-r-- 1 skipworthy skipworthy 25 Jun 11 16:13 testfoo

注意,这次我们使用了一些快捷键。

-a 存档模式,保留mtime,权限和符号链接。
-r 递归模式,深入到任何目录并同步这些目录(应该是多余的-a开关,但我总是指定它)。
-u 只有在源文件的mtime较新时才会更新文件。
-v Verbose 模式,告诉你它在做什么(能够监控正在发生的事情总是好的。另一个有用的技巧是把输出结果用管道传送到一个文件,然后再检查)。)

使用rsync还原文件
因此,让我们假装几周后。CFO打电话说出了问题—他的/ foo目录中缺少许多文件。

./foo:
total 8
5002591 -rw-r--r-- 1 root       root        0 Jun 11 15:25 bat.txt
4997949 -rw-rw-r-- 1 skipworthy skipworthy 33 Jul 24 15:32 footoo.txt
5001268 -rw-rw-r-- 1 skipworthy skipworthy 25 Jun 11 16:13 testfoo

我们来看一下备份,并查看丢失的文件:

./bar:
total 12
4992599 -rw-r--r-- 1 root       root        0 Jun 11 15:45 bat.txt
4994298 -rw-rw-r-- 1 skipworthy skipworthy 33 Jul 24 15:32 footoo.txt
4994359 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles1.txt
4994367 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles2.txt
4994374 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles3.txt
4994413 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles4.txt
5001398 -rw-rw-r-- 1 skipworthy skipworthy  8 Jun 11 15:08 testbar
4983541 -rw-rw-r-- 1 skipworthy skipworthy 25 Jun 11 16:13 testfoo

快速rsync还原:

[root@milo enable]# rsync -aruv ./bar/* ./foo
sending incremental file list
bat.txt
laterfiles1.txt
laterfiles2.txt
laterfiles3.txt
laterfiles4.txt
testbar

和:

./foo:
total 12
4994387 -rw-r--r-- 1 root       root        0 Jun 11 15:45 bat.txt
4997949 -rw-rw-r-- 1 skipworthy skipworthy 33 Jul 24 15:32 footoo.txt
4994562 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles1.txt
4994564 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles2.txt
4994565 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles3.txt
4994567 -rw-r--r-- 1 root       root        0 Jul 24 15:31 laterfiles4.txt
4994579 -rw-rw-r-- 1 skipworthy skipworthy  8 Jun 11 15:08 testbar
5001268 -rw-rw-r-- 1 skipworthy skipworthy 25 Jun 11 16:13 testfoo

缺失的文件会从最近的备份中恢复或更新,但现有的文件--没有改变--则被保留下来。另外,请注意footoo.txt的所有权被保留了下来。

我鼓励您像往常一样浏览的手册页rsync,并尝试使用此有用的命令。

这里还有一些要考虑的快捷键:

-r (递归)
-b (备份)
-R (相对)
-u (更新-仅复制更改的文件)
-P (进展)
-c (压缩)
-p (保留权限)
在本系列的下一篇文章中,我们将进一步rsync介绍该命令的远程功能和其他一些高级功能。

A5互联https://www.a5idc.net/

标签:rsync,rw,15,--,备份,Jun,Sysadmin,skipworthy,root
来源: https://www.cnblogs.com/a5idc/p/13494377.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有