特定のファイルを含むすべてのディレクトリを再同期する

特定のファイルを含むすべてのディレクトリを再同期する

私は公開鍵とパスワードが必要なクラスタで作業していますが、このクラスタで作業を構成するための複雑なファイル構造があり、ローカルシステムからいくつかのディレクトリだけをバックアップしたいと思います。

rsyncが "backup"というファイルを含むディレクトリだけをインポートしたいと思います。例:

data/sub1/sub1_1/backup < back up this directory
data/sub1/sub1_2/ < don't back up

このパスワードの問題のために、sshを何度も呼び出すスクリプトを避けたいと思います。 rsyncを使用してこれを実行できる高度なフィルタはありますか?

ベストアンサー1

バックアップするディレクトリが多すぎない場合は、bashシェルスクリプトを使用してそのディレクトリをコマンドラインに配置できます。次のシェルスクリプトはデモです。

ディレクトリ構造

$ tree data
data
└── sub1
    ├── sub1_1
    │   ├── a
    │   ├── b
    │   └── backup
    ├── sub1_2
    │   └── c
    └── sub1_3
        ├── backup
        └── d

4 directories, 6 files

シェルスクリプトrsyncer

#!/bin/bash

echo -n 'rsync -avn ' > command
find . -name 'backup' -type f | sed -e 's%/backup%%' -e 's%.*%"&"%' | tr '\n' ' ' >> command
echo ' target/' >> command

bash command

試運転

$ ./rsyncer 
sending incremental file list
created directory target
sub1_1/
sub1_1/a
sub1_1/b
sub1_1/backup
sub1_3/
sub1_3/backup
sub1_3/d

sent 199 bytes  received 64 bytes  526.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)

sub1/sub1_に注意してください。2とファイルリストにありません。

サポート

nシェルスクリプトのrsyncからオプションを削除して実行するか、ファイルからオプションを削除してcommand実行します。

sed 's/-avn/-av/' command > buper

$ bash buper
sending incremental file list
created directory target
sub1_1/
sub1_1/a
sub1_1/b
sub1_1/backup
sub1_3/
sub1_3/backup
sub1_3/d

sent 383 bytes  received 148 bytes  1,062.00 bytes/sec
total size is 0  speedup is 0.00

$ tree target
target
├── sub1_1
│   ├── a
│   ├── b
│   └── backup
└── sub1_3
    ├── backup
    └── d

2 directories, 5 files

sub1/sub1_に注意してください。2とファイルリストにありません。

おすすめ記事