rsync:フィルタを使用して最上位ディレクトリを除外しますが、一部のサブディレクトリは含まれています。

rsync:フィルタを使用して最上位ディレクトリを除外しますが、一部のサブディレクトリは含まれています。

/home내 디렉토리를 백업하고 싶습니다동기화. 나는 rsync에 대해 읽었습니다.매뉴얼 페이지これを行うためにフィルタリングルールを使用することにしました。

私が達成したいもの: ディレクトリ内のすべてのファイルとディレクトリを除外しますReposが、すべてのpull_all.shファイルとディレクトリはoutputディレクトリに残ります。Repos---ディレクトリどこにいても構いません。

これまで、次のフィルタのリストが得られましたが、これはディレクトリではなくpull_all.shファイルのみをバックアップしますoutput

# Files prefixed with "+ " are included. Files prefixed with "- " are excluded.
#
# The order of included and excluded files matters! For instance, if a folder
# is excluded first, no subdirectory can be included anymore. Therefore,
# mention included files first. Then, mention excluded files.
#
# See section "FILTER RULES" of rsync manual for more details.


# Included Files

# TODO: This rules do not work properly!
+ output/***
+ pull_all.sh
- Repos/**

# Excluded Files

- .android
- .cache
...

私のスクリプトはフィルタリストを使用していますrun_rsync.sh

#!/bin/bash

date="$(date +%Y-%m-%d)"
hostname="$(hostname)"

# debug_mode="" # to disable debug mode
debug_mode="--list-only"

# Note: With trailing "/" at source directory, source directory is not created at destination.
rsync ${debug_mode} --archive --delete --human-readable --filter="merge ${hostname}.rsync.filters" --log-file=logfiles/$date-$hostname-home.log --verbose /home backup/

残念ながら、既存のStackExchangeスレッドは私の問題を解決しません。

ここで何の問題がありますか?

[更新] 以下は、ホームディレクトリの外観、保持するファイル、無視するファイルの例です。

user@hostname:~$ tree /home/ | head
/home/
└── user
    ├── Desktop                -> keep this
    │   ├── file1              -> keep this
    │   └── file2              -> keep this
    ├── Documents              -> keep this
    ├── Repos
    │   ├── pull_all.sh        -> keep this
        ├── subdir1
        │   ├── output         -> keep this
        ├── subdir2
            ├── another_subdir
                ├── output     -> keep this
        ├── subdir3            -> do not keep (because does not contain any "output")
        ├── file3              -> do not keep

ベストアンサー1

あなたのリクエストの私の説明をもう少し繰り返すと、

  • pull_all.shどこにいても、すべてのファイルが含まれます。
  • outputすべてのディレクトリを含むそしてその内容私たちがそれらを見つけるたびに
  • Reposすでに指定されているディレクトリ以外のディレクトリは除外してください。
  • その他すべてを含む

これは次のように指定できます。

rsync --dry-run --prune-empty-dirs -av

    --include 'pull_all.sh'
    --include 'Repos/**/output/***'

    --include '*/'

    --exclude 'Repos/***'

    /home backup/

いくつかのメモ

  • これは、ディレクトリツリーの下に下がって(ファイルを見つけるために)考慮する--include '*/'ために必要です。それ以外の場合は、最終ステートメントからファイルが除外されます。rsyncRepospull_all.sh--exclude
  • 3つの用途が*区別されます。
    • */文字以外のすべてと一致します。
    • **/文字を含む何でも一致します。
    • dir/***dir/とを指定するのと同じショートカットですdir/**
  • この--prune-empty-dirsフラグは空のrsyncディレクトリの作成を停止します。これは、エントリをRepos見つけるためにディレクトリツリーを処理する必要があるため、特に重要です。pull_all.shoutput
  • --dry-run結果が満足であれば削除してください。

おすすめ記事