使用硬链接和组合视图创建日期文件夹版本备份

使用硬链接和组合视图创建日期文件夹版本备份

我正在使用这篇文章创建我的数据的增量版本备份。我基本上将数据同步到备份目标中的当前文件夹,然后创建一个带有指向当前文件夹的硬链接的日期文件夹。我最终得到以下结果:

$ ls
...
2019-01-01_10-00-01
2019-01-02_10-00-01
...
2019-02-15_10-00-01
...
current

效果很好。如果我需要从特定日期进行完整恢复,我可以从该日期的文件夹中恢复所有内容。

但是,如果您要查找特定文件的先前版本,则必须浏览每个日期文件夹才能找到所需内容。我想创建另一个文件夹,其中包含每次更改时所有文件的累计总数。如果您愿意,可以将其合并为一个视图。

我想到了这个,并且它有效,但我想知道是否有更优雅、更标准的方法来做到这一点。

#!/bin/bash

NOW=$(/bin/date +\%Y-\%m-\%d_\%H-\%M-\%S)

# the data that needs to be backed up
SOURCES=("/path/to/source 1" "/path/to/source 2")

# where it is going
DESTINATION="/path/to/backup"

# make sure the destination exists
mkdir -p "$DESTINATION"

# make sure there is a place to put the current data
mkdir -p "$DESTINATION/current"

# make sure there is a place to put the "combined" data
mkdir -p "$DESTINATION/combined"

# sync the data
rsync -v -a --delete "${SOURCES[@]}" "$DESTINATION/current"

# check if files were backed up
# any file with only one link is either new, and needs to have a hard link version
# or it wasn't fully backed up previously and needs a hard link version
if [[ $(find "$DESTINATION/current" -type f -links 1 | wc -l) -ne 0 ]] ; then
    # make a date folder backup using hard links
    cp -al "$DESTINATION/current" "$DESTINATION/$NOW"

    # make a combined view
    #  - find all files with 2 links
    #    - one link is to the file in the $DESTINATION/current
    #    - the other link is to the file in $DESTINATION/$NOW
    # - there should never be any files with only 1 hard link since the previous command
    #   is sure to have created a second link
    # - any files with more than 2 links were, hopefully, already covered during a previous iteration
    cd "$DESTINATION/current" && find * -type f -links 2 -print0 | while IFS= read -r -d $'\0' filePath
    do
        fileName="$(basename "$filePath")"
        fileFolder="$(dirname "$filePath")"

        # where the file will live in the combined folder
        # need to mirror the folder structure
        destinationFolder="$DESTINATION/combined/$fileFolder"
        mkdir -p "$destinationFolder"

        # make a hard link to it
        cp -al "$filePath" "$destinationFolder/$fileName.$NOW"
    done
fi

代码确实有效。经过几次迭代后,它创建了以下内容:

当前文件夹中的文件(这是源数据的“实时”副本):

backup/current/source 1/001
backup/current/source 1/002
backup/current/source 1/003
backup/current/source 1/file 100
backup/current/source 1/folder/004
backup/current/source 2/006

特定日期的文件夹中的文件(注意,第一次备份中的文件包含第二次备份中没有的文件,因为它们已被删除):

backup/2019-01-15_23-08-02/source 1/001
backup/2019-01-15_23-08-02/source 1/002
backup/2019-01-15_23-08-02/source 1/003
backup/2019-01-15_23-08-02/source 1/file 100
backup/2019-01-15_23-08-02/source 1/folder/004
backup/2019-01-15_23-08-02/source 1/folder/005
backup/2019-01-15_23-08-02/source 2/006
backup/2019-01-15_23-08-02/source 2/007

backup/2019-01-15_23-09-00/source 1/001
backup/2019-01-15_23-09-00/source 1/002
backup/2019-01-15_23-09-00/source 1/003
backup/2019-01-15_23-09-00/source 1/file 100
backup/2019-01-15_23-09-00/source 1/folder/004
backup/2019-01-15_23-09-00/source 2/006

这些是组合视图中的文件:

backup/combined/source 1/001.2019-01-15_23-08-02
backup/combined/source 1/002.2019-01-15_23-08-02
backup/combined/source 1/003.2019-01-15_23-08-02
backup/combined/source 1/003.2019-01-15_23-09-00
backup/combined/source 1/file 100.2019-01-15_23-08-02
backup/combined/source 1/folder/004.2019-01-15_23-08-02
backup/combined/source 1/folder/004.2019-01-15_23-09-00
backup/combined/source 1/folder/005.2019-01-15_23-08-02
backup/combined/source 2/006.2019-01-15_23-08-02
backup/combined/source 2/006.2019-01-15_23-09-00
backup/combined/source 2/007.2019-01-15_23-08-02

这样,如果我需要查找的先前版本source 1/folder/004,我只需要转到backup/combined/backup/combined/source 1/folder)中与其匹配的文件夹,所有004文件都在那里,并附加了日期/时间戳。

有没有更好、更优雅的方法来做到这一点?

相关内容