4

*Disclaimer: I can follow directions for Terminal, but I am not a nix user by default.

I have a Raspberry Pi that I just had to restore. Before I get anything else installed I would like to create a clone image that I can restore if required.

I have done this by inserting the SD card in my Mac and using the following:

sudo dd if=/dev/rdiskx of=/path/to/image bs=1m

If I SSH from my Mac, can I create a clone across the network to my machine? There is not enough room on the SD card to do it locally.

Steve Robillard
  • 34,158
  • 17
  • 102
  • 108
Kray
  • 63
  • 1
  • 1
  • 4

3 Answers3

5

it's possible to run ssh to issue a command on the remote end and catch the output on your local machine, something along the lines of

ssh user@192.168.xxx.xx 'dd if=/dev/mmcblk0 bs=10M' | cat > sd_image.img

but actually it's not recommended to read SD card while the system is running from the same SD, because it's very easy to get an inconsistent file system image and you'd much better off using SD card reader on your Mac for creating images with the command you shown above.

lenik
  • 11,503
  • 1
  • 29
  • 37
3

Have a look at my instructions here for using rsync to maintain a backup; at the end there is a brief paragraph about using it via ssh.

If you pay heed the part about what should go in the rsync-exclude.txt list, you can do this while the pi is running. You can also use -e in place of the --rsh I use in the other example, or if you have no special options you need for ssh, just omit it; rsync will use it by default when it sees a network address. So:

rsync -aEv --delete --exclude-from=/rsync-exclude.txt root@yourpi.ip:/ /backup/path

Updates the backup. Using it to restore is explained in the other answer.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • 1
    have you really read the question? how do you propose to create an SD card **image** using rsync? – lenik Feb 20 '14 at 23:43
  • I did try your suggestion, and got it to work (with a few changes). However there are issues, and I doubt it could be used to restore (at least by anyone without extensive Linux knowledge) http://raspberrypi.stackexchange.com/q/13611/8697 – Milliways Feb 21 '14 at 00:45
  • @lenik : I read the question. I was responding to the desire "to create a clone image that I can restore". If people are determined to use `dd` as a backup tool, that's up to them, but at the same time -- just like a question with posted code containing an obvious programming faux pas -- it should be indicated that this is *the canonically wrong* method and provide *a* more normative, orthodox alternative. Hopefully this at least helps to prevent other people being led astray: "Oh, here's a question about how to back up my pi" -- no, this is a question about how *not* to back up the pi. – goldilocks Feb 21 '14 at 06:28
  • Thanks Goldilocks! Appreciate the info on using rsync over dd, and the details instructions! I am hoping to get to try this today, but it's looking more and more like it will be next week. – Kray Feb 21 '14 at 14:16
  • @Kray : You'll need `rsync` on the Mac side if you are doing it from there -- I'm not a big OSX user, but it appears to be standard equipment, fortunately. It maybe a slightly older version than is used on raspbian/linux/the pi but [the man page](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/rsync.1.html) matches up. – goldilocks Feb 21 '14 at 14:26
  • @goldilocks Thanks again! Debating on posting another topic about this. Can I do a clone that will JUST do the contents? I built my system on a 16GB card so my image is 16GB, but would like to restore to a 4GB or 8GB card... Worth a new topic? – Kray Mar 03 '14 at 20:18
  • One of the advantages of mirroring the filesystem (as opposed to cloning an image) is that the mirror only takes up as much space as actually needed. In other words, if the original system has a 16GB fs only 2GB of which are used, you are only mirroring the 2GB of actual files, meaning a system synced with that only needs 2GB. – goldilocks Mar 03 '14 at 20:42
  • @goldilocks So the rsync will allow for this over the DD as well? – Kray Mar 03 '14 at 20:46
  • Yeah. `rsync` clones a directory tree; it's copying files, whereas `dd` copies raw data blocks from the device. It isn't aware of any files or filesystem beyond the one containing the device node (`/dev/foo`). If you've ever copied cassette tapes, same idea. Whether there are actually any songs on the tape, etc., it's the same process. You just copy 30 min of tape. Likewise, `dd` just copies X amount of data, much of which could be empty junk. It is a useful tool, just not so much for the purpose of backing up a filesystem. – goldilocks Mar 03 '14 at 20:52
1

The problem with a dd-created image is: you can only write it back to a mSD card that is larger than the image. So if your image is 16GB, trying to write the image onto another 16GB card may chop off some sectors, because the new card has a few sectors less than the old card from which the image was taken. Trying to write an "oversized" image to some USB sticks or mSDs may brick them, as I found out the hard way.

TheDiveO
  • 1,551
  • 1
  • 9
  • 15
  • I kept wondering why the 1-to-1 clones periodically had bad internal lib packages. +1 to lesson learned the hard way. – Elysiumplain Jan 25 '21 at 20:40
  • one would expect the USB stick/mSD controllers not to FUBAR when writing beyond what's given to them, but then I should have expected the opposite instead of expecting a tried and tested mass product ;) The FUBAR'ed product was a very well-know brand with lots of patents on the subject. – TheDiveO Jan 26 '21 at 15:09