12

I'm not sure if all the "how to backup your SD card" topics is what I'm asking for... but here we go :)

Is there a way I can make a 100% clone of my Raspberry Pi and just deploy it onto another Raspberry Pi? So I have two exactly identical copies... Or is this done by cloning the SD card using the built-in dd command?

The reason I'm asking is that I have my Raspberry Pi fully configured with Apache, PHP and some other libraries and now I just want another clone of it with the exact same configurations, etc.

Is this possible or did I answer my own question?

  • I'm surprised that piclone hasn't been mentioned so far. To me that's the most straightforward way of cloning an sd card on a rpi, and nvere had problems with it. I've already answered this question in the past so here is a link to the answer with some more details: raspberrypi.stackexchange.com/questions/66824/… Hope this helps. – LecauseAndThePi Jun 15 '17 at 10:27
  • I couldn't find piclone download anywhere – Elia Weiss Apr 11 '18 at 5:42
  • You don't ened to download it, it's already there. Just type piclone in the terminal, and it will launch the GUI. – LecauseAndThePi Apr 11 '18 at 10:57
9

@naboovalley:

Suppose the following scenario:

  • On your Raspberry Pi, go to a terminal and then go to the /dev directory. You should see, among other devices, mmcblk0, mmcblk0p1 and mmcblk0p2.
  • mmcblk0 is the "bare" SD card device, while mmcblk0p1 is the first partition of that SD card containing the kernel image and so on and mmcblk0p2 is the second partition of that SD card containing the root file system.
  • Shut down your Raspberry Pi in a decent manner (type the command "shutdown", possibly as root)
  • Put the SD card into another Linux machine. Normally, the above mentioned directories should be automatically mounted on /media/ and there should be 2 subdirectories in there (one for partition 1 of the SD card, one for partition 2 of the SD card)
  • You have created a temp directory to copy the content of your SD card. Say: ~/tmp
  • You are located in ~/tmp

You can now copy the content of the first partition o fthe original original SD card by issueing the following rsync command:

rsync -avn /media/<user>/<partition1> .

Do not put a / at the end! The above command will "dry run", which means that it will show you what it eventually will do, but will not execute it for real.

To execute for real (after you've confirmed that the dry run is doing what it should do), you can remove the n from the above command (so, rsync -av /media/<user>/<partition1> .).

Use the same command for the other partition.

Now you should have an exact copy of your original SD card on your Linux machine.

Remove the original SD card, put another -correctly formatted- SD card in your Linux machine and execute the following rsync command:

rsync -avn <partition1> /media/<user>

This is again a dry run, which should show you that the content of <partition1> in /media/<user> will be copied to the SD card.

Once you confirmed all is fine, you can again remove the n from the rsync command.

Do the same for the second partition.

This should give you an exact copy of the original SD card onto the other SD card.

Note: I checked all these on a Lubuntu machine. Directories and names may vary on other Linux distro's.

Note also that items between < > are placeholders and you have to see for yourself how they match with your environment.

  • Perfect. Thank you! Will the "dd" copy do the same as you describe or is rsync much better doing a full system copy the way I want it? – naboovalley Apr 13 '14 at 16:38
  • I don't have a lot of experience with the dd command, so I can't tell, sorry... – GeertVc Apr 17 '14 at 6:13
  • I don't get entirely the part where you say "-correctly formatted- SD card". Do we have to pre format the SD card with properly sized fat32 and ext4 partitions? Thank you – Romain Vincent May 20 '18 at 17:43
7

Yes, you can use dd or Win32 Disk Imager on Windows to take an exact image from one card to another. However, the destination card will need to be the same size or bigger to make this practical.

You could also just copy all the files on your source file system to a destination file system. That is, plug your destination card into a USB card reader and stick that in your source Raspberry Pi. Then SSH in and issue the appropriate copy commands.

One thing to bear in mind also, is if you have purchased license keys for hardware decoding of MPEG-2 or VC-1. These are tied to your hardware, so you'll need to make sure you edit the /boot/config.txt on your copy.

  • So the SD-card holds all system specific files and installed libraries? I won't need to run any installations commands when pluging in the cloned copy of the SD-card into R-Pi #2? – naboovalley Apr 10 '14 at 9:56
  • 2
    This can also presents issues with SSH Keys and IP addresses. – Steve Robillard Apr 10 '14 at 9:57
  • So on R-Pi #2 it might think the ip-address is the same as R-Pi #1? I'm guessing I can just refresh it to get a new network ip? – naboovalley Apr 10 '14 at 9:58
  • If you are using DHCP, the RPi will receive a new IP at boot automatically, but you should change the hostname if you plan to use both in the same subnet. If you use a fixed ip, you should change both the IP and the hostname – Roberto Apr 10 '14 at 11:08
  • If I look at my filesystem ("df") I have something called "rootfs" 2.1GB and /dev/root 2.1GB - my /dev/mmcblk0 sd-card only takes 10M. So when I copy "mmcblk0" my entire system will be included? Thanks guys – naboovalley Apr 10 '14 at 11:17
6

You can just copy with dd as @me-- suggests, but that's not all you have to do when cloning a system. There are certain unique system identifiers that should be changed as well.

Passwords should be reset so that a new salt is used. This keeps people from knowing you used the same password twice. Run the passwd command as both your user (usually pi) and as root.

If you use SSH, SSH keys should be re-generated. This is important for example, for making sure that if someone steals one Raspberry Pi they can't decrypt communications to the other one. Just run this command:

sudo rm /etc/ssh/ssh_host_*

Finally and most importantly, you need to change the hostname. If it's at its default it's raspberrypi. If you're like me, I ssh to the Raspberry Pi's host name, so I do ssh raspberrypi. Having two "raspberrypis" on the network makes that stop working, so the second one should be renamed to rasberrypi2 for example. You can make this change with sudo raspi-config, selecting Advanced Options, and then selecting Hostname.

3

Cloning a micro SD for backup or to move your system to another RPi3 is built in as an accessory, at least on the version of the operating system I got on the NOOBS startup SD:

  • Go to accessories;
  • Hit Copy SD;
  • Place a blank micro SD in a USB holder;
  • Place holder in open USB slot;
  • Choose the new SD;
  • Hit copy.
  • This should be the correct answer. So easy. – Jordan Aug 15 '18 at 23:00
1

You can do exactly that using the raspberry pi and a new motto SD card connected via USB. The latest version of Raspberry Pi software has a builtin software called SD cardcopier under accessories that does exactly this.

You choose your original card (so the card that you are booting from) as Source and the new formatted empty card (connected via card reader to usb on the pi) as Destination. Pretty simple UI and it worked for me first time out. The command line option on mac using dd somehow ended up having a kernel panic issue.

0

A very secure and reliable tool to copy things like root file systems (because of links which are present in the root file system) is rsync. This tool will take into account all necessary things like timestamps, links, permissions,... Perfectly suited to copy stuff like you want to do.

  • But I still only copy the sd-card content for the full system duplicate - or do I need to include something else? Thanks – naboovalley Apr 11 '14 at 7:35
  • See the new reply I posted below. – GeertVc Apr 13 '14 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.