# How to make a copy of Linux microSD

<abstract>
This article illustrates how to make a backup a Linux bootable microSD 
</abstract>


## Check the /etc/fstab contents

The recent Debian distributions use the microSD identifier to check which partitions to 
use for the boot and rootfs

Insert the microSD to clone inside a Linux PC and type:

	cat /media/$USER/rootfs/etc/fstab
	
If you have a content like this:

	UUID=30611356-143e-40ae-b5b1-f39556649c42	/	ext4	defaults,noatime	0	0
	UUID=7D9A-CB73	/boot	vfat	defaults	0	0

on the cloned microSD change it in:

	/dev/mmcblk0p2	/		ext4	defaults,noatime	0	0
	/dev/mmcblk0p1	/boot	vfat	defaults			0	0

you can change it also on the original microSD if you need to save time in case
of multiple clones

## Direct copy between microSDs

<abstract>
Use this method if you want to make a copy directly without leave
any backup on your PC
</abstract>

![](./microsd_to_microsd.jpg)

Format a new microSD following the instruction on this article: 

* @article='microsd_format'

Remove the target microSD and insert the original one. 
Three new folders will appear with the following path:

* <b>/media/$USER/BOOT</b>
* <b>/media/$USER/rootfs</b>

__$USER__ is an environment variable used by Linux for your user name.

Insert, in another reader, the just formatted target microSD. Other 
three new folder appear with path:

* <b>/media/$USER/BOOT</b>
* <b>/media/$USER/rootfs_</b>

Note the underscore (_) char at the end of each folder name.

Type these commands to copy all:

<pre class="minicom">
$ <b>sudo cp -rp /media/$USER/boot/. /media/$USER/BOOT_/. </b>
$ <b>sudo cp -rp /media/$USER/rootfs/. /media/$USER/rootfs_/.</b>
$ <b>sync</b>
$ <b>sudo umount /media/$USER/BOOT_ </b>
$ <b>sudo umount /media/$USER/rootfs_</b>
</pre>

Your copy is ready to be used, extract it and try.

## Copy from microSD to a compressed set of files on HD

<abstract>
This method is useful if you want to have a snapshot of your microSD 
contents on your hard disk to use as master to create new copy.
</abstract>

![](./microsd_to_tar.jpg)

Insert the master in your Linux PC. Three new folders will appear on your desktop:

* <b>/media/$USER/BOOT</b>
* <b>/media/$USER/rootfs</b>

Open a terminal session and type:

<pre class="minicom">
$ <b>cd /media/$USER/BOOT</b>
$ <b>sudo tar -cvjSf ../boot.tar.bz2 . </b>
...
$ <b>cd /media/$USER/rootfs</b>
$ <b>sudo tar -cvjSf ../rootfs.tar.bz2 . </b>
...
</pre>

The options mean:

* <b>-c</b> create a new archive
* <b>-v</b> verbosely list files processed
* <b>-j</b> filter the archive through bzip2
* <b>-S</b> handle sparse files efficiently
* <b>-f</b> use archive file or device ARCHIVE

On the <b>/media/$USER</b> directory will appear new three tarbal files:

* <b>boot.tar.bz2</b>
* <b>rootfs.tar.bz2</b>

These are the backup of your original microSD. They can be use to generate
a new copy followinf the next instruction.

## Create a new microSD from a compressed set of files

![](./tar_to_microsd.jpg)

Format a new microSD following the instruction on this article: 

* @article='microsd_format'

Remove the just formatted target and insert it again to automount its
contents.

Three new folders will appear with the following path:

* <b>/media/$USER/BOOT</b>
* <b>/media/$USER/rootfs</b>

$USER is an environment variable used by Linux for your user name.

If your compressed backup files are on the same directory used 
to make them type:

<pre class="minicom">
$ <b>sudo tar -xvjpSf /media/$USER/boot.tar.bz2 -C /media/$USER/BOOT </b>
$ <b>sudo tar -xvjpSf /media/$USER/rootfs.tar.bz2 -C /media/$USER/rootfs </b>
$ <b>sync</b>
$ <b>sudo umount /media/$USER/boot</b>
$ <b>sudo umount /media/$USER/rootfs</b>
</pre>

Your copy is ready to be used, extract it and try.

## For for Raspberry Pi users

If you are making a backup for a Raspberry Pi board read the PARTUUID of any uSD partition 
using this command while the uSD is inside the Linux PC

	sudo blkid /dev/sdb1
	sudo blkid /dev/sdb2

and update the file __/etc/fstab__ and __cmdline.txt__ 


@include='bio_tanzilli'

@shop='MICROSD8G-E'
@shop='MICROSD8G'
@shop='MICROSD16G'
@shop='MICROSD32G'
