Linux化計画(10)–SDカードのバックアップ

SDカードのバックアップをします。

VNCクライアントの実行までうまくできるようになったところで、そろそろSDカードのバックアップをしておかないと、なんだか怖いことになりそうです。当初のSDカードを作ったときのツールを使えばバックアップは取れるようですが、せっかくラズパイが使えるようになったのに、Windowsでバックアップするというのは面白くありませんので、ここはラズパイのUSBポートにSDカードリーダを差し込んでバックアップする方法にこだわりたいところです。

調べてみると、X-WindowsでSD Card Copierというのがありました。

https://www.raspberrypi.org/blog/another-update-raspbian/

しかし、できることならワンラインコマンドで対応したいので、もう少し調べてみたところ、rpi-cloneというツール(script)があるようです。

全体のアイデアはこちらから

BackupPi: Raspberry Pi のバックアップとリストアを Raspberry Pi 自身で行う方法
http://qiita.com/UedaTakeyuki/items/4a78c38b3070e8ef57e5

今回は、apt-getではなくgitでファイルを落としてからディレクトリに移動させる方法でした。
https://github.com/billw2/rpi-clone

こちらを参考にしました。

Raspberry PiでIoTなシステム開発:SDカードのバックアップはrpi-cloneでやろう
http://www.homu.net/raspberry-pi%E3%81%A7iot%E3%81%AA%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0%E9%96%8B%E7%99%BA%EF%BC%9Asd%E3%82%AB%E3%83%BC%E3%83%89%E3%81%AE%E3%83%90%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%E3%81%AFrpi/

一回目はうまくいかなかったのですが、二回目はうまくいきました。
おそらく、カードを強く押し込んでいなかったため接触不良があったのでしょう。

Linuxのデバイスはドライブではなくデバイス名でしか出てこないので、同じメディアを繋いだ場合には、どちらかを峻別する必要があります。他方、デバイスに係らず入出力が統一的に扱えるという利便性もあります。

pi@raspberrypi:~ $ sudo fdisk -l

これで、以下のディスクが認識されます。

Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 137215 129024 63M c W95 FAT32 (LBA)
/dev/mmcblk0p2 137216 30253055 30115840 14.4G 83 Linux

Disk /dev/sda: 14.4 GiB, 15489564672 bytes, 30253056 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device Boot Start End Sectors Size Id Type
/dev/sda1 8192 30253055 30244864 14.4G c W95 FAT32 (LBA)

これをみて、SDA1をコピー先にすればよいのかと思ったのですが、下記のように叱られました。

pi@raspberrypi:/usr/local/bin/rpi-clone $ sudo ./rpi-clone sda1 -f

Destination disk 'sda1' does not exist.
Plug the destination SD card into a card reader connected to a
USB port. If it does not show up as 'sda1', then do a
'cat /proc/partitions' to see where it might be.

そこでパーティションが区切られているところを確かめ。

pi@raspberrypi:/usr/local/bin/rpi-clone $ cat /proc/partitions

major minor #blocks name

1 0 4096 ram0
1 1 4096 ram1
1 2 4096 ram2
1 3 4096 ram3
1 4 4096 ram4
1 5 4096 ram5
1 6 4096 ram6
1 7 4096 ram7
1 8 4096 ram8
1 9 4096 ram9
1 10 4096 ram10
1 11 4096 ram11
1 12 4096 ram12
1 13 4096 ram13
1 14 4096 ram14
1 15 4096 ram15
179 0 15126528 mmcblk0
179 1 64512 mmcblk0p1
179 2 15057920 mmcblk0p2
8 0 15126528 sda ← これがUSBに挿したSDカード

どうやらsdaのようです。

pi@raspberrypi:/usr/local/bin/rpi-clone $ sudo ./rpi-clone sda -f

-fオプションは強制的に上書きクローニングします。

Forcing a partition initialization of destination disk sda
The existing destination disk 'sda' partitions are:
Disk /dev/sda: 15490MB
Partition Table: msdos

Number Start End Size Type File system Flags
1 4.19MB 15490MB 15485MB primary fat32 lba

*** All data on destination disk sda will be overwritten! ***

Do you want to initialize the destination disk /dev/sda? (yes/no): y

Number Start End Size Type File system Flags
1 4.19MB 70.3MB 66.1MB primary fat16 lba
2 70.3MB 15490MB 15419MB primary ext4

Your booted /dev/mmcblk0p2 rootfs existing label:
You may enter a label for the destination rootfs /dev/sda2: ←ここで一旦待ちが入りますが、しばらく黙ってリターン

======== Clone Summary ========
Clone mode : rsync all files to sda root file system
Clone destination disk : sda
Clone destination rootfs : /dev/sda2 (no label) on /mnt/clone
Clone destination bootfs : /dev/sda1 on /mnt/clone/boot
Verbose mode : off
===============================
Final check, is it Ok to proceed with the clone (yes/no)?: y
=> Mounting /dev/sda2 (no label) on /mnt/clone
=> Mounting /dev/sda1 on /mnt/clone/boot
===============================
Starting the filesystem rsync to sda
(This may take several minutes)...

※ここで一旦画面が止まったようになりますが、USBの方にLEDなどが付いていれば、クローニングしている間は点滅していますから、進行していることが分かります。

終わると次のメッセージが。

*** Done with clone to /dev/sda ***
Started: 16:39:18 Finished: 17:02:52 ←結構時間がかかっています

Hit Enter when ready to unmount the /dev/sda partitions... ← 終わるとこれがでるのでリターン

unmounting /mnt/clone/boot
unmounting /mnt/clone
===============================

クローンの作業は20分以上かかりました。
念のため、パーティションを確かめます。

pi@raspberrypi:~ $ cat /proc/partitions

major minor #blocks name

1 0 4096 ram0
1 1 4096 ram1
1 2 4096 ram2
1 3 4096 ram3
1 4 4096 ram4
1 5 4096 ram5
1 6 4096 ram6
1 7 4096 ram7
1 8 4096 ram8
1 9 4096 ram9
1 10 4096 ram10
1 11 4096 ram11
1 12 4096 ram12
1 13 4096 ram13
1 14 4096 ram14
1 15 4096 ram15
179 0 15126528 mmcblk0
179 1 64512 mmcblk0p1
179 2 15057920 mmcblk0p2
8 0 15126528 sda
8 1 64512 sda1
8 2 15057920 sda2

179(SDスロット)の3つのパーティションが、USB#8のパーティションにクローンされました。容量が同じです。

最後に、いったん電源を切って、クローンのSDカードをUSBスロットに入れてリブート。
問題なくリブートができました。

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


計算式を埋めてください * Time limit is exhausted. Please reload CAPTCHA.