HOW TO CLONE SD CARD
Created backup image to obelisk e:
Create new SD card from live image:
pi$ sudo su
root# apt-get install dosfstools
root# apt-get install rsync
Stop Motion Service and disconnect camera
$ motionstop
Plugged in USB2 SOHOUSB reader
root# dmesg
[10346.000962] usb 1-1.2.2: new high-speed USB device number 10 using dwc_otg
[10346.105028] usb 1-1.2.2: New USB device found, idVendor=05e3, idProduct=0716
[10346.105065] usb 1-1.2.2: New USB device strings: Mfr=0, Product=1, SerialNumber=2
[10346.105081] usb 1-1.2.2: Product: USB Storage
[10346.105096] usb 1-1.2.2: SerialNumber: 000000009740
[10346.112454] usb-storage 1-1.2.2:1.0: USB Mass Storage device detected
[10346.116135] scsi0 : usb-storage 1-1.2.2:1.0
[10347.112586] scsi 0:0:0:0: Direct-Access Generic STORAGE DEVICE-6 9740 PQ: 0 ANSI: 0
[10347.120131] sd 0:0:0:0: [sda] Attached SCSI removable disk
[10347.123249] scsi 0:0:0:1: Direct-Access Generic STORAGE DEVICE-6 9740 PQ: 0 ANSI: 0
[10347.126441] sd 0:0:0:1: [sdb] Attached SCSI removable disk
[10347.129775] scsi 0:0:0:2: Direct-Access Generic STORAGE DEVICE-6 9740 PQ: 0 ANSI: 0
[10347.138187] sd 0:0:0:2: [sdc] Attached SCSI removable disk
[10347.139607] scsi 0:0:0:3: Direct-Access Generic STORAGE DEVICE-6 9740 PQ: 0 ANSI: 0
[10347.152948] sd 0:0:0:3: [sdd] Attached SCSI removable disk
[10347.153733] scsi 0:0:0:4: Direct-Access Generic STORAGE DEVICE-6 9740 PQ: 0 ANSI: 0
[10347.162079] sd 0:0:0:4: [sde] Attached SCSI removable disk
[10347.455181] sd 0:0:0:0: Attached scsi generic sg0 type 0
[10347.456126] sd 0:0:0:1: Attached scsi generic sg1 type 0
[10347.457169] sd 0:0:0:2: Attached scsi generic sg2 type 0
[10347.458213] sd 0:0:0:3: Attached scsi generic sg3 type 0
[10347.459125] sd 0:0:0:4: Attached scsi generic sg4 type 0
[10355.503741] bcm2835_audio_set_ctls:558 Controls set for stream 0
[10364.234904] sd 0:0:0:2: [sdc] 30703616 512-byte logical blocks: (15.7 GB/14.6 GiB)
[10364.237255] sd 0:0:0:2: [sdc] No Caching mode page found
[10364.237292] sd 0:0:0:2: [sdc] Assuming drive cache: write through
[10364.251476] sd 0:0:0:2: [sdc] No Caching mode page found
[10364.251516] sd 0:0:0:2: [sdc] Assuming drive cache: write through
[10364.253192] sdc: sdc1
[10365.962691] bcm2835_audio_set_ctls:558 Controls set for stream 0
root@rpints:/home/pi#
So new SD card is SDC: SDC1
First, we need to create the two partitions to hold our /boot and / (slash) filesystems.
We do this with the GNU parted utility.
root@rpints:/home/pi# parted /dev/sdc
GNU Parted 2.3
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel msdos
Warning: Partition(s) on /dev/sdc are being used.
Ignore/Cancel? I
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No?
Y
Error: Partition(s) 1 on /dev/sdc have been written, but we have been unable to inform the kernel of the change, probably because
it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
Ignore/Cancel? I
(parted) quit
Restarted system
root@rpints:/home/pi# parted /dev/sdc
GNU Parted 2.3
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart primary fat16 1MiB 64MB
(parted) mkpart primary ext4 64MB -1s
(parted) print
Model: Generic STORAGE DEVICE-6 (scsi)
Disk /dev/sdc: 15.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 64.0MB 62.9MB primary lba
2 64.0MB 15.7GB 15.7GB primary
(parted) quit
Information: You may need to update /etc/fstab.
Next format the two partitions
root@rpints:/home/pi# mkfs.vfat /dev/sdc1
mkfs.fat 3.0.26 (2014-03-07)
root@rpints:/home/pi# mkfs.ext4 -j /dev/sdc2
mke2fs 1.42.5 (29-Jul-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
956592 inodes, 3822336 blocks
191116 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=3917479936
117 block groups
32768 blocks per group, 32768 fragments per group
8176 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
root@rpints:/home/pi#
Stop mysql server:
root@rpints:/home/pi# service mysql stop (start, restart)
Now we need to mount these two partitions somewhere so we can copy data to it.
Let's make a temporary directory under /tmp/newpi and do all our work under it.
Below are the commands to mount our SD cards:
root@rpints: /home/pi# mkdir /tmp/newpi
root@rpints: mount /dev/sdc2 /tmp/newpi
root@rpints: mkdir /tmp/newpi/boot
root@rpints: mount /dev/sdc1 /tmp/newpi/boot
root@rpints: df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 13G 3.6G 8.8G 30% /
/dev/root 13G 3.6G 8.8G 30% /
devtmpfs 215M 0 215M 0% /dev
tmpfs 44M 320K 44M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 88M 0 88M 0% /run/shm
/dev/mmcblk0p5 60M 9.6M 50M 17% /boot
/dev/mmcblk0p3 27M 397K 25M 2% /media/SETTINGS
/dev/sdc2 15G 37M 14G 1% /tmp/newpi
/dev/sdc1 60M 0 60M 0% /tmp/newpi/boot
root@rpints:/home/pi#
The output of the "df" command will show us that both slash and boot have been mounted and are the expected sizes.
Next, we will execute a command to copy our existing raspbian OS installed on our running pi into our mounted SD card.
Now, a couple notes about live cloning.
If you are running any services that keep data files open (like mysql, postgresql, apt-get, or other applications) these should be shutdown to insure you get a clean copy.
We will use the rsync utility for this copy, we will tell it to only copy / (slash) and /boot, further, we will also tell rsync not to cross filesystem boundaries (--one-file-system).
Why do we specify slash (/) and boot separately? If you look above at the df listing you can see lots of other filesystems mounted.
Some of these are ramdisks and virtual filesystems (/sys, /dev, /tmp, and /proc for instance).
We do not want to copy these at all. By using the --one-file-system and specifying explicitly slash (/) and /boot, we only capture the OS tree of files. ( Note, the rsync is sensitive to trailing slashes on the source and destination directory names, make sure you run it exactly as below)
root@rpints:/home/pi# rsync -av --one-file-system / /boot /tmp/newpi/
[bazillion files copied to SD card!]
(NOOBs Users: If you are running a SD card created with NOOBs, you will need to manually modify the boot/cmdline.txt and etc/fstab files on your new SD card before you can boot. You must edit and substitute the mmcblk0p* device names for the proper ones.
In the etcfstab, slash (/) should be /dev/mmcblk0p and /boot should be /dev/mmcblk0p1.
In the boot/cmdline.txt file, root= part should be root=/dev/mmcblk0p2.
*** NOTE: NO CHANGES APPEAR TO BE NECESSARY
Once you have done that the OS that you backed up will be the only OS on the new SD card even if your NOOBs SD had multiple flavors of raspberry pi OS installed. My scripts on github discussed in the next section handle all of this automatically, which makes things a bit easier.)
These are just a few examples.
When your ready to try out your cloned SD card, you should unmount it using the following commands.
Make sure you close any shells or change your current working directory (cd) off the new devices or you will get an error when you try to unmount.
root# umount /tmp/newpi/boot
root# umount /tmp/newpi
You can now safely pull the SD card out and put it in your destination raspberry pi.
It should boot and work just like any other SD card.
AND IT WORKS! YAY!