
Linuxで差分バックアップを行うコマンドといえば、rsyncコマンドだ。 バックアップ元ディレクトリとバックアップ先ディレクトリに差分があればそれを考慮してバックアップを取得するため、利用している人も多いだろう。
今回は、そんなrsyncコマンドで覚えておきたい使い方についてを紹介する。
1.基本的な使い方
rsyncコマンドは、基本的に以下のようにコマンドを実行しバックアップを行う。
bashrsync [オプション] バックアップ元ディレクトリ バックアップ先ディレクトリ
オプションを指定する場合はディレクトリの指定前に行う。
2.権限、所有者等を 引き継いでバックアップする
ファイルの権限や所有者、グループ、タイムスタンプを引き継いで、ディレクトリ内を再帰的にバックアップを取得する場合は、「-a」オプションを付与する。
bashrsync -a バックアップ元ディレクトリ バックアップ先ディレクトリ
権限や所有者、グループ、タイムスタンプを別々に指定したい場合は、それぞれに以下のオプションを付与する。
- ファイル・ディレクトリの所有者 … -o
- ファイル・ディレクトリのグループ … -g
- ファイル・ディレクトリの権限 … -p
- ファイル・ディレクトリのタイムスタンプ … -t
- ディレクトリ内を再帰的にバックアップ … -r
- シンボリックリンク(リンク先) … -l
3.バックアップの詳細を表示させる
通常、rsyncを実行すると何も表示されないのだが、「-v」オプションを付与することでその詳細情報が取得出来る。 「-v」の数に応じて、その詳細な情報量が変動する。
shell[root@test-centos7 ~]# #「-v」なし [root@test-centos7 ~]# rsync -a /work /work2 [root@test-centos7 ~]# [root@test-centos7 ~]# #「-v」あり(1個) [root@test-centos7 ~]# rsync -av /work /work3 sending incremental file list created directory /work3 work/ work/aaaaa.txt work/bbbbb.txt work/ccccc.txt work/ddddd.txt work/ln_test.txt -> test.txt work/ln_test.txt2 -> /work/ln_test.txt work/test.txt work/test2.txt sent 807 bytes received 136 bytes 1886.00 bytes/sec total size is 363 speedup is 0.38 [root@test-centos7 ~]# [root@test-centos7 ~]# #「-v」あり(2個) [root@test-centos7 ~]# rsync -avv /work /work4 sending incremental file list created directory /work4 delta-transmission disabled for local transfer or --whole-file work/ work/aaaaa.txt work/bbbbb.txt work/ccccc.txt work/ddddd.txt work/ln_test.txt -> test.txt work/ln_test.txt2 -> /work/ln_test.txt work/test.txt work/test2.txt total: matches=0 hash_hits=0 false_alarms=0 data=338 sent 807 bytes received 136 bytes 1886.00 bytes/sec total size is 363 speedup is 0.38 [root@test-centos7 ~]# [root@test-centos7 ~]# #「-v」あり(3個) [root@test-centos7 ~]# rsync -avvv /work /work5 sending incremental file list [sender] make_file(work,*,0) send_file_list done [sender] make_file(work/test2.txt,*,2) [sender] make_file(work/test.txt,*,2) [sender] make_file(work/ln_test.txt,*,2) [sender] make_file(work/ln_test.txt2,*,2) [sender] make_file(work/aaaaa.txt,*,2) [sender] make_file(work/bbbbb.txt,*,2) [sender] make_file(work/ccccc.txt,*,2) [sender] make_file(work/ddddd.txt,*,2) send_files starting server_recv(2) starting pid=7635 received 1 names recv_file_list done received 8 names recv_file_list done get_local_name count=9 /work5 created directory /work5 generator starting pid=7635 delta-transmission disabled for local transfer or --whole-file recv_generator(work,1) recv_generator(work,2) recv_files(1) starting send_files(2, //work) work/ set modtime of work to (1433560464) Sat Jun 6 12:14:24 2015 recv_generator(work/aaaaa.txt,3) send_files(3, //work/aaaaa.txt) send_files mapped //work/aaaaa.txt of size 0 calling match_sums //work/aaaaa.txt work/aaaaa.txt sending file_sum false_alarms=0 hash_hits=0 matches=0 sender finished //work/aaaaa.txt recv_generator(work/bbbbb.txt,4) send_files(4, //work/bbbbb.txt) send_files mapped //work/bbbbb.txt of size 0 calling match_sums //work/bbbbb.txt work/bbbbb.txt sending file_sum false_alarms=0 hash_hits=0 matches=0 sender finished //work/bbbbb.txt recv_generator(work/ccccc.txt,5) send_files(5, //work/ccccc.txt) send_files mapped //work/ccccc.txt of size 0 calling match_sums //work/ccccc.txt work/ccccc.txt sending file_sum false_alarms=0 hash_hits=0 matches=0 sender finished //work/ccccc.txt recv_generator(work/ddddd.txt,6) send_files(6, //work/ddddd.txt) send_files mapped //work/ddddd.txt of size 0 calling match_sums //work/ddddd.txt work/ddddd.txt sending file_sum false_alarms=0 hash_hits=0 matches=0 sender finished //work/ddddd.txt recv_generator(work/ln_test.txt,7) set modtime of work/ln_test.txt to (1433500169) Fri Jun 5 19:29:29 2015 send_files(7, //work/ln_test.txt) work/ln_test.txt -> test.txt recv_generator(work/ln_test.txt2,8) set modtime of work/ln_test.txt2 to (1433500254) Fri Jun 5 19:30:54 2015 send_files(8, //work/ln_test.txt2) work/ln_test.txt2 -> /work/ln_test.txt recv_generator(work/test.txt,9) send_files(9, //work/test.txt) send_files mapped //work/test.txt of size 108 calling match_sums //work/test.txt work/test.txt sending file_sum false_alarms=0 hash_hits=0 matches=0 sender finished //work/test.txt recv_generator(work/test2.txt,10) send_files(10, //work/test2.txt) send_files mapped //work/test2.txt of size 230 calling match_sums //work/test2.txt work/test2.txt sending file_sum false_alarms=0 hash_hits=0 matches=0 sender finished //work/test2.txt generate_files phase=1 recv_files(work) recv_files(work/aaaaa.txt) got file_sum set modtime of work/.aaaaa.txt.kkA1QP to (1433560454) Sat Jun 6 12:14:14 2015 renaming work/.aaaaa.txt.kkA1QP to work/aaaaa.txt recv_files(work/bbbbb.txt) got file_sum set modtime of work/.bbbbb.txt.5P0MbC to (1433560457) Sat Jun 6 12:14:17 2015 renaming work/.bbbbb.txt.5P0MbC to work/bbbbb.txt recv_files(work/ccccc.txt) got file_sum set modtime of work/.ccccc.txt.CMoAwo to (1433560461) Sat Jun 6 12:14:21 2015 renaming work/.ccccc.txt.CMoAwo to work/ccccc.txt recv_files(work/ddddd.txt) got file_sum set modtime of work/.ddddd.txt.PheqRa to (1433560464) Sat Jun 6 12:14:24 2015 renaming work/.ddddd.txt.PheqRa to work/ddddd.txt recv_files(work/ln_test.txt) recv_files(work/ln_test.txt2) recv_files(work/test.txt) got file_sum set modtime of work/.test.txt.SUzocX to (1433424684) Thu Jun 4 22:31:24 2015 renaming work/.test.txt.SUzocX to work/test.txt recv_files(work/test2.txt) got file_sum set modtime of work/.test2.txt.ZZEpxJ to (1433369865) Thu Jun 4 07:17:45 2015 renaming work/.test2.txt.ZZEpxJ to work/test2.txt send_files phase=1 set modtime of work to (1433560464) Sat Jun 6 12:14:24 2015 recv_files phase=1 generate_files phase=2 send_files phase=2 send files finished total: matches=0 hash_hits=0 false_alarms=0 data=338 recv_files phase=2 generate_files phase=3 recv_files finished generate_files finished sent 807 bytes received 136 bytes 1886.00 bytes/sec total size is 363 speedup is 0.38 [sender] _exit_cleanup(code=0, file=main.c, line=1052): about to call exit(0) [root@test-centos7 ~]#
ただバックアップを実施したファイルをログに出力して確認するだけであれば1個で良いと思うけれど… 必要であれば、3個指定してもいいだろう(出力多すぎでウザイ気もするけど)。
その他、「--progress」オプションを付与することで、ファイルごとの進捗状況を確認することが出来る。
bashrsync バックアップ元ディレクトリ バックアップ先ディレクトリ --progress
この時、「-h」オプションを付与することで進捗状況の容量がわかりやすい単位に表示される。
4.ssh経由でバックアップを行う
おそらく、rsyncを使っている多くの人が利用している機能。 rsyncをssh経由で実行し、バックアップを取得させるというもの。
例)ssh経由でリモートサーバのディレクトリをバックアップする
bashrsync ユーザ名@ホスト名:バックアップ元ディレクトリ ローカルのバックアップ先ディレクトリ
例)ローカルのディレクトリをssh経由でリモートサーバにバックアップする
bashrsync バックアップ元ディレクトリ ユーザ名@ホスト名:リモートのバックアップ先ディレクトリ
なお、ssh接続方式がパスワード認証だとパスワードを聞かれてしまうので、cronなどで定期的に実行するのであれば鍵認証方式に切り替える事をおすすめする。 何かしらの事情があって鍵認証方式に切り替えられない場合は、sshpassコマンドを使うか、expectでパスワードの自動入力スクリプトを作成すると良いだろう。
なお、この時「-z」オプションを付与することで、通信を圧縮してファイルを転送することができる。
例)ローカルのディレクトリをssh経由でリモートサーバにバックアップする
bashrsync -z バックアップ元ディレクトリ ユーザ名@ホスト名:リモートのバックアップ先ディレクトリ
例)特定の秘密鍵で接続する
特定の鍵を使ってssh接続する場合は、以下のようにrsyncを実行する。 ポート番号なども同様のやり方で指定可能だ。
bash
rsync -e 'ssh -i 秘密鍵ファイルのPATH' バックアップ元ディレクトリ ユーザ名@ホスト名:リモートのバックアップ先ディレクトリ
5.差分のみにバックアップ処理を実行する
「-u」オプションを利用することで、バックアップ先にすでに存在しているファイルはバックアップ対象とみなさず、差分のあるファイルのみバックアップを行わせる事ができる。 rsyncを使っているならば、大体の利用者はこのオプションを使っているだろう。
bashrsync -u バックアップ元ディレクトリ バックアップ先ディレクトリ
shell[root@test-centos7 ~]# rsync -auv /work /work2 sending incremental file list created directory /work2 work/ work/aaaaa.txt work/bbbbb.txt work/ccccc.txt work/ddddd.txt work/ln_test.txt -> test.txt work/ln_test.txt2 -> /work/ln_test.txt work/test.txt work/test2.txt sent 807 bytes received 136 bytes 1886.00 bytes/sec total size is 363 speedup is 0.38 [root@test-centos7 ~]# rsync -auv /work /work2 sending incremental file list sent 218 bytes received 13 bytes 462.00 bytes/sec total size is 363 speedup is 1.57 [root@test-centos7 ~]#
例を見てもらえればわかると思うが、バックアップ終了直後に再度コマンドを実行しても差分が無いため、バックアップ処理は行われない。 さらに「--delete」オプションを付与することで、バックアップ元から削除されたファイルは、バックアップ先でも削除するようにできる。
6.コマンドの実行結果を予測する
「-n」オプションを付与することで、実際にコマンドの処理を実行せず、もし実行された場合のファイルのリストを表示させることができる。
bashrsync -n バックアップ元ディレクトリ バックアップ先ディレクトリ
shell[root@test-centos7 ~]# rsync -avn /work /work2 sending incremental file list created directory /work2 work/ work/aaaaa.txt work/bbbbb.txt work/ccccc.txt work/ddddd.txt work/ln_test.txt -> test.txt work/ln_test.txt2 -> /work/ln_test.txt work/sample work/test.txt work/test2.txt sent 265 bytes received 43 bytes 616.00 bytes/sec total size is 209715563 speedup is 680894.69 (DRY RUN)
7.ログを出力させる
「--log-file」オプションを付与することで、rsyncのログを出力させることが出来る。
bash
rsync --log-file=ログファイルのPATH バックアップ元ディレクトリ バックアップ先ディレクトリ
shell[root@test-centos7 ~]# rsync -auv /work /work2 --log-file=/home/test.log sending incremental file list created directory /work2 work/ work/aaaaa.txt work/bbbbb.txt work/ccccc.txt work/ddddd.txt work/ln_test.txt -> test.txt work/ln_test.txt2 -> /work/ln_test.txt work/test.txt work/test2.txt sent 807 bytes received 136 bytes 1886.00 bytes/sec total size is 363 speedup is 0.38 [root@test-centos7 ~]# cat /home/test.log 2015/06/08 22:57:02 [11307] building file list 2015/06/08 22:57:02 [11307] created directory /work2 2015/06/08 22:57:02 [11307] cd+++++++++ work/ 2015/06/08 22:57:02 [11307] >f+++++++++ work/aaaaa.txt 2015/06/08 22:57:02 [11307] >f+++++++++ work/bbbbb.txt 2015/06/08 22:57:02 [11307] >f+++++++++ work/ccccc.txt 2015/06/08 22:57:02 [11307] >f+++++++++ work/ddddd.txt 2015/06/08 22:57:02 [11307] cL+++++++++ work/ln_test.txt -> test.txt 2015/06/08 22:57:02 [11307] cL+++++++++ work/ln_test.txt2 -> /work/ln_test.txt 2015/06/08 22:57:02 [11307] >f+++++++++ work/test.txt 2015/06/08 22:57:02 [11307] >f+++++++++ work/test2.txt 2015/06/08 22:57:02 [11307] sent 807 bytes received 136 bytes 1886.00 bytes/sec 2015/06/08 22:57:02 [11307] total size is 363 speedup is 0.38
なお、このログファイルへの出力は追記で行われる。
8.バックアップの統計情報を出力させる
「--stats」を付与することで、バックアップに関する統計情報を出力させる。
bashrsync --stats バックアップ元ディレクトリ バックアップ先ディレクトリ
shell[root@test-centos7 ~]# rsync -avn /work /work2 --stats sending incremental file list created directory /work2 work/ work/aaaaa.txt work/bbbbb.txt work/ccccc.txt work/ddddd.txt work/ln_test.txt -> test.txt work/ln_test.txt2 -> /work/ln_test.txt work/sample work/test.txt work/test2.txt Number of files: 10 Number of files transferred: 7 Total file size: 209715563 bytes Total transferred file size: 209715538 bytes Literal data: 0 bytes Matched data: 0 bytes File list size: 221 File list generation time: 0.001 seconds File list transfer time: 0.000 seconds Total bytes sent: 265 Total bytes received: 43 sent 265 bytes received 43 bytes 616.00 bytes/sec total size is 209715563 speedup is 680894.69 (DRY RUN)
9.findにヒットしたファイルを同期させる
rsyncで、バックアップ対象ファイルとしてfindで見つかったファイルを指定することが出来る。
bash
rsync -avR '`find バックアップ元ディレクトリ 検索条件`' バックアップ先ディレクトリ
shell[root@test-centos7 ~]# rsync -avR root@192.168.0.226:'`find /{etc,home,work} -name "*.conf"`' /work2 root@192.168.0.226's password: receiving incremental file list created directory /work2 etc/ etc/aaa.conf etc/asound.conf etc/dnsmasq.conf etc/dracut.conf etc/e2fsck.conf etc/fuse.conf etc/host.conf etc/incron.conf etc/kdump.conf etc/krb5.conf etc/ld.so.conf etc/libaudit.conf etc/libuser.conf etc/locale.conf etc/logrotate.conf etc/man_db.conf etc/mke2fs.conf etc/nilfs_cleanerd.conf etc/nsswitch.conf etc/ntp.conf etc/resolv.conf etc/rsyncd.conf etc/rsyslog.conf etc/sestatus.conf etc/sudo-ldap.conf etc/sudo.conf etc/sysctl.conf etc/tcsd.conf etc/vconsole.conf etc/yum.conf etc/NetworkManager/ etc/NetworkManager/NetworkManager.conf etc/X11/ etc/X11/xorg.conf.d/ etc/X11/xorg.conf.d/00-keyboard.conf etc/audisp/ etc/audisp/audispd.conf etc/audisp/plugins.d/ etc/audisp/plugins.d/af_unix.conf etc/audisp/plugins.d/sedispatch.conf etc/audisp/plugins.d/syslog.conf etc/audit/ etc/audit/auditd.conf etc/avahi/ etc/avahi/avahi-daemon.conf etc/dbus-1/ etc/dbus-1/session.conf etc/dbus-1/system.conf etc/dbus-1/system.d/ etc/dbus-1/system.d/FirewallD.conf etc/dbus-1/system.d/avahi-dbus.conf etc/dbus-1/system.d/com.redhat.tuned.conf etc/dbus-1/system.d/dnsmasq.conf etc/dbus-1/system.d/nm-avahi-autoipd.conf etc/dbus-1/system.d/nm-dispatcher.conf etc/dbus-1/system.d/nm-ifcfg-rh.conf etc/dbus-1/system.d/org.fedoraproject.SetroubleshootFixit.conf etc/dbus-1/system.d/org.fedoraproject.Setroubleshootd.conf etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf etc/dbus-1/system.d/org.freedesktop.PolicyKit1.conf etc/dbus-1/system.d/org.freedesktop.hostname1.conf etc/dbus-1/system.d/org.freedesktop.locale1.conf etc/dbus-1/system.d/org.freedesktop.login1.conf etc/dbus-1/system.d/org.freedesktop.machine1.conf etc/dbus-1/system.d/org.freedesktop.systemd1.conf etc/dbus-1/system.d/org.freedesktop.timedate1.conf etc/dbus-1/system.d/org.selinux.conf etc/dbus-1/system.d/teamd.conf etc/dbus-1/system.d/wpa_supplicant.conf etc/depmod.d/ etc/depmod.d/dist.conf etc/depmod.d/kmod-nilfs2.conf etc/etckeeper/ etc/etckeeper/etckeeper.conf etc/firewalld/ etc/firewalld/firewalld.conf etc/fonts/ etc/fonts/fonts.conf etc/fonts/conf.d/ etc/fonts/conf.d/10-scale-bitmap-fonts.conf -> /usr/share/fontconfig/conf.avail/10-scale-bitmap-fonts.conf etc/fonts/conf.d/20-unhint-small-vera.conf -> /usr/share/fontconfig/conf.avail/20-unhint-small-vera.conf etc/fonts/conf.d/25-no-bitmap-fedora.conf etc/fonts/conf.d/25-unhint-nonlatin.conf -> /usr/share/fontconfig/conf.avail/25-unhint-nonlatin.conf etc/fonts/conf.d/30-metric-aliases.conf -> /usr/share/fontconfig/conf.avail/30-metric-aliases.conf etc/fonts/conf.d/30-urw-aliases.conf -> /usr/share/fontconfig/conf.avail/30-urw-aliases.conf etc/fonts/conf.d/40-nonlatin.conf -> /usr/share/fontconfig/conf.avail/40-nonlatin.conf etc/fonts/conf.d/45-latin.conf -> /usr/share/fontconfig/conf.avail/45-latin.conf etc/fonts/conf.d/49-sansserif.conf -> /usr/share/fontconfig/conf.avail/49-sansserif.conf etc/fonts/conf.d/50-user.conf -> /usr/share/fontconfig/conf.avail/50-user.conf etc/fonts/conf.d/51-local.conf -> /usr/share/fontconfig/conf.avail/51-local.conf etc/fonts/conf.d/60-latin.conf -> /usr/share/fontconfig/conf.avail/60-latin.conf etc/fonts/conf.d/65-fonts-persian.conf -> /usr/share/fontconfig/conf.avail/65-fonts-persian.conf etc/fonts/conf.d/65-nonlatin.conf -> /usr/share/fontconfig/conf.avail/65-nonlatin.conf etc/fonts/conf.d/69-unifont.conf -> /usr/share/fontconfig/conf.avail/69-unifont.conf etc/fonts/conf.d/80-delicious.conf -> /usr/share/fontconfig/conf.avail/80-delicious.conf etc/fonts/conf.d/90-synthetic.conf -> /usr/share/fontconfig/conf.avail/90-synthetic.conf etc/httpd/ etc/httpd/conf.d/ etc/httpd/conf.d/autoindex.conf etc/httpd/conf.d/userdir.conf etc/httpd/conf.d/welcome.conf etc/httpd/conf.modules.d/ etc/httpd/conf.modules.d/00-base.conf etc/httpd/conf.modules.d/00-dav.conf etc/httpd/conf.modules.d/00-lua.conf etc/httpd/conf.modules.d/00-mpm.conf etc/httpd/conf.modules.d/00-proxy.conf etc/httpd/conf.modules.d/00-systemd.conf etc/httpd/conf.modules.d/01-cgi.conf etc/httpd/conf/ etc/httpd/conf/httpd.conf etc/java/ etc/java/java.conf etc/ld.so.conf.d/ etc/ld.so.conf.d/kernel-3.10.0-123.13.2.el7.x86_64.conf etc/ld.so.conf.d/kernel-3.10.0-123.20.1.el7.x86_64.conf etc/ld.so.conf.d/kernel-3.10.0-123.el7.x86_64.conf etc/ld.so.conf.d/mariadb-x86_64.conf etc/lvm/ etc/lvm/lvm.conf etc/modprobe.d/ etc/modprobe.d/OpenIPMI.conf etc/openldap/ etc/openldap/ldap.conf etc/plymouth/ etc/plymouth/plymouthd.conf etc/prelink.conf.d/ etc/prelink.conf.d/fipscheck.conf etc/prelink.conf.d/golang.conf etc/prelink.conf.d/grub2.conf etc/pulse/ etc/pulse/client.conf etc/puppet/ etc/puppet/auth.conf etc/puppet/puppet.conf etc/rsyslog.d/ etc/rsyslog.d/listen.conf etc/rsyslog.d/shell.conf etc/samba/ etc/samba/smb.conf etc/sasl2/ etc/sasl2/smtpd.conf etc/security/ etc/security/access.conf etc/security/chroot.conf etc/security/group.conf etc/security/limits.conf etc/security/namespace.conf etc/security/pam_env.conf etc/security/pwquality.conf etc/security/sepermit.conf etc/security/time.conf etc/security/limits.d/ etc/security/limits.d/20-nproc.conf etc/selinux/ etc/selinux/semanage.conf etc/selinux/targeted/ etc/selinux/targeted/setrans.conf etc/setroubleshoot/ etc/setroubleshoot/setroubleshoot.conf etc/sgml/ etc/sgml/sgml.conf etc/sysctl.d/ etc/sysctl.d/99-sysctl.conf -> ../sysctl.conf etc/systemd/ etc/systemd/bootchart.conf etc/systemd/journald.conf etc/systemd/logind.conf etc/systemd/system.conf etc/systemd/user.conf etc/tmpfiles.d/ etc/tmpfiles.d/puppet.conf etc/tmpfiles.d/screen.conf etc/tmpfiles.d/tuned.conf etc/tomcat/ etc/tomcat/tomcat.conf etc/tuned/ etc/tuned/tuned-main.conf etc/udev/ etc/udev/udev.conf etc/wpa_supplicant/ etc/wpa_supplicant/wpa_supplicant.conf etc/yum/ etc/yum/version-groups.conf etc/yum/yum-cron-hourly.conf etc/yum/yum-cron.conf etc/yum/pluginconf.d/ etc/yum/pluginconf.d/fastestmirror.conf etc/yum/protected.d/ etc/yum/protected.d/systemd.conf sent 2663 bytes received 254714 bytes 57194.89 bytes/sec total size is 244120 speedup is 0.95
10.変更のないファイルはハードコピーのみとして、スナップショットのように利用する
「--link-dest」オプションを利用すると、指定したディレクトリと差異のあるファイルのみをバックアップし、それ以外のファイルはすべて「--link-dest」で指定したディレクトリとのハードコピーにする事が出来る。 この機能を応用すると、こちらでも記述したようにSambaのシャドウコピーを行うためのスナップショットとして利用することが出来る。
bashrsync --link-dest=ハードコピーディレクトリ バックアップ元ディレクトリ バックアップ先ディレクトリ