zoukankan      html  css  js  c++  java
  • Sysadmin工具:使用rsync管理备份,还原和文件同步

    作为一个系统管理员,我把大部分精力都花在两件事上,一件是担心是否有备份,另一件是找出最简单最好的方法。我最喜欢的解决这两个问题的工具之一叫做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/

  • 相关阅读:
    tiff遥感图像空间坐标转换(工作太忙,仅仅作为记录)
    get_beijing_roadnetwork(工作太忙,仅仅作为记录)
    xml_result_2_taos_db(工作太忙,仅仅作为记录)
    Hadoop HDFS原理详解(系统性回顾)
    基于Mapreduce数据排序
    Hadoop-Mapreduce-英文单词计数(Brief版本-超详细解读)
    Hadoop-Mapreduce-英文单词计数
    常见数据类型-HadoopDataType(仅仅作为记录)
    GeoServer 一键发布 Raster 数据服务(分片上传、GDAL)
    GeoServer 安装、跨域
  • 原文地址:https://www.cnblogs.com/a5idc/p/13494377.html
Copyright © 2011-2022 走看看