LoginSignup
10
5

More than 5 years have passed since last update.

rsync ファイルリストを指定してコピー

Posted at

rsync コマンドにファイルのリストを渡してバックアップ等行う方法

--files-from オプションを使う。

rsync -a --files-from=<your_file_name> <source_dir> <target_dir>

  • <your_file_name>:ファイルのリストを格納したファイル
  • <target_dir>:バックアップ先のディレクトリ
  • <source_dir>:コピー元(ファイルリストに記載のファイル)のディレクトリ(絶対パスでファイルリストを記載した場合には,/(ルート)から始まるのでここは/を指定する。相対パスの場合は然るべきディレクトリを指定。)

使用例(bashスクリプト)

do_rsync.bash
#!/bin/bash -eu

###
# Please give the appropriate file and disk paths
# of each file list and the mounted disk
# to the variables, file_name=@@@ and disk_name=@@@, respectively.
###

# USAGE:
#      nohup bash do_rsync.bash > log.txt &

###  SETTING  ###

file_name=/home/hnishi/bak3_20180716/disk1/filelist_201806_disk1.txt
disk_name=/media/HDD1_4TB

###  HARD CODE  ####################
# You do not need to change the codes below
echo  "USAGE:    nohup bash do_rsync.bash > log.txt &"
echo You selected
echo list: $file_name
echo disk: $disk_name

file=$(rev <<< $file_name |cut -d"/" -f 1|rev)
disk=$(rev <<< $disk_name |cut -d"/" -f 1|rev)
log_name=log_do_rsync_${file}_${disk}.txt

echo
echo The log appears as a file
echo $log_name

date > $log_name
echo list file: $file_name   >> $log_name
echo disk path: $disk_name   >> $log_name

rsync --partial --itemize-changes -auR --files-from=${file_name} / $disk_name >> $log_name

#cat $file_name |xargs -P 2 -I {} rsync --partial --itemize-changes -auR {}  $disk_name >> $log_name ## catで渡す場合

date >> $log_name

echo
echo
echo successfully finished

補足

  • catコマンドで開いてパイプで渡す方法(cat <your_file_name>|rsync <target_dir>)でもできるが,--files-from オプションを使った方が早いことを確認した。
10
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5