Linux:rsync
基本的な使い方
例1)
rsync -avz foo:/home/hoge/src/bar /data/tmp
foo というマシンにある /home/hoge/src/bar ディレクトリまるごと、ローカルマシンの /data/tmp ディレクトリへ転送。
例2)
rsync -avz /home/hoge/src/bar/ /data/tmp
/home/hoge/src/bar/ ディレクトリ配下のファイルを、ローカルマシンの /data/tmp/ ディレクトリへ 転送。
例3)
rsync -rv -e ssh ./index.html aho@baka.com:/home/aho/a
リモートマシン baka.com に ssh 接続し、ユーザ aho でログインし、ディレクトリ home/aho/a に、ローカルマシンの ./index.html を転送。
rオプションで指定ディレクトリ配下をすべてとしているが、ファイル名指定なので無視される
rsyncのオプション
オプション | 別名 | 意味 |
-a | --archive | シンボリックリンク、デバイス、属性、 パーミッション、所有を維持。指定ディレクトリ以下を再帰的に転送。-rlptgoD と同じ |
-r | --recursive | 指定ディレクトリ配下をすべて対象とする |
-v | --verbose | コピーしたファイル名やバイト数などの転送情報を出力 |
-u | --update | 転送先に既にファイルが存在し、転送先のタイムスタンプの方が新しい場合は転送しない |
-n | --dry-run | コピーや転送を実際には行わず転送内容のみ出力 |
-z | --compress | データ転送時に圧縮 |
--delete | コピー元で削除されたファイルをコピー先でも削除 | |
--progress | ファイル単位で転送状況をリアルタイム表示 |
ssh のポートを 22222 で接続する
rsync --rsh="ssh -p 22222"
または
rsync -e "ssh -p 22222"
exclude-from=FILEはちょっとややこしい
--exclude-from=FILE # FILEに記載したパターンに一致したファイルを除外する --include-from=FILE # ファイルに記述されたパターンと一致するファイルを除外しない
beginner-to-beginner-rsync-exclude-from The file can list both includes and excludes. You indicate which is which by prefacing each item with a + or a -. The list assumes that the root directory is whichever one you specified in the rsync command line. So, if your command line said that you want to back up “/Users/me/”, then you would tell it to exclude “/Users/me/junk” by putting the following line in your exclude-from file: e.g exclude-files.txt
- junk/
Then use liki this:
rsync -avz /me/ /Users/me/ --exclude-from=exclude-files.txt
送信先として/User/me/ を指定している場合、--exclude-from内に記述するパスはそれ以降。この場合、/User/me/からの相対パスjunk/を指定する
Likewise, to include /Users/me/importantstuff/ you’d put in the line:
+ importantstuff/
But, at least in my experiments, that line will not include any subdirectories of importantstuff. After failing to understand the instructions I found on the Net, and after a lot of trial and error, I’ve found that it works if I also include the line:
+ importantstuff/**
The double stars tell it to backup all the subdirectories and all their subdirectories, ad infinitum. I’ve found I have to put in both the line without the stars and then the line with the stars. You’d think the line with the stars would be enough, but in my tries and my errors, it wasn’t.
The list of inclusions and exclusions is sensitive to the order of the list. If you have particular subdirectories you want to exclude (e.g., importantstuff/junk/), put them first:
- importantstuff/junk/**
If you want rsync to backup only designated directories, list your excludes first, then your includes, and end with
- *
which tells it to exclude anything you didn’t already tell it to include. I have the feeling that that may be an ugly hack with unintended consequences. Remember, I don’t know what I’m doing.
So, my exclude-from file looks roughly like this:
- *Azureus*/ - *Azureus*/** - Documents/TiVo* - Documents/Aptana* + Sites/ + Sites/** + Pictures/ + Pictures/** + Music/ + Music/** + Documents + Documents/** - *
Two important notes: 1. The -n parameter on the command line will run rsync in “what if” mode, showing you what it would do without actually doing it. 2. As I’ve likely made some embarrassing and awful mistakes, please read the comments in hopes that some knowledgeable and kind soul will correct me.
リモートサーバへ認証なしで転送する
local $ ssh-keygen -t rsa
「Enter …. : 」の箇所では、常に何も入力せずにリターンでOKです。 id_rsa.pub : 公開キーファイル id_rsa : 秘密キーファイル となります。
remote $ mkdir ~/.ssh
local $ scp id_rsa.pub user@host:~/.ssh/authorized_keys