On Friday, 24 August 2018 09:32:23 BST Philip Webb wrote:
> I want to make a copy of a partition which I can use to replace it,
> if some catastrophe damages the partition or wipes it out ;
> it needs to be byte-byte identical, incl all permissions.

You are looking to clone a partition.  This is a bit by bit identical copy of 
the original partition.  You could clone a partition into another, so you end 
up with two identical partitions including their boot record, fs and contents, 
or save the cloned partition into an image file.

However, you may prefer to use clonezilla instead of dd.  The dd command will 
copy each and every bit and byte of the partition whether it has data on it or 
not.  It is not particularly efficient.  Clonezilla will perform better at 
this task.

Personally, I would only keep a back up of the filesystem contents with e.g. 
rsync, and reformat the partition and restore its contents in the case of a 
disaster recovery scenario.  I would prefer a fs backup, especially in the 
case of a USB stick being the backup storage media.  This is because rsync 
will only copy data which has changed since the last backup, rather than 
overwriting what is on the USB already.  I expect using something like dd 
repeatedly will reduce considerably the life of the USB stick.


> Can I use 'dd' ? -- eg 'dd if=/mnt/xxx of=/mnt/yyy',
> where the partition has been mounted at  /mnt/xxx
> & a USB stick has been mounted at  /mnt/yyy .  Will that do the job ?

Not like that.  If you intend to create a clone of the partition, rather than 
its filesystem, you need to copy the whole block device, not its mount point.

dd if=/dev/sdXx of=/media/$USER/<LABEL>/clone_of_sdXx.img bs=4096 
conv=notrunc,fsync status=progress

Where /media/$USER/<LABEL>/ is assumed to be the mountpoint of your mounted 
USB storage device.  The above will save an image file of the cloned partition 
as 'clone_of_sdXx.img' within the existing filesystem of the USB stick.  You 
could then copy this partition image to a secondary storage if you wish, 
encrypt it, compress it, etc.

You'll be able to mount this image with '-o loop' and examing or use its 
contents.

mount /media/$USER/<LABEL>/clone_of_sdXx.img /mnt/My_partition_image -o loop

ls -l /mnt/My_partition_image


To create a full partition clone on the USB stick rather than an image file 
change the of= operand into the block device your USB stick is recognised as; 
e.g.

dd if=/dev/sdXx of=/dev/sdYy bs=4096 conv=notrunc,fsync status=progress

CAUTION:  If you get /dev/sdYy wrong you *will* lose data on the device you 
overwrite.

You may have problems when you try to mount the cloned USB stick with the 
original partition in place, because they will both have identical filesystem 
UUIDs.  I haven't tried this to know what errors it may produce.


> There seems also to be an issue re 'bs=<some number of bytes>' :
> what size is best ?  i plan to use USB 3.0 for quicker copying.

You can test what block size will transfer data faster on your particular 
hardware setup. I assume in the example below the I/O bottleneck is your USB, 
rather than your HDD.

dd if=/dev/zero of=/media/$USER/<LABEL>/testfile bs=1024 count=1000000

rm /media/$USER/<LABEL>/testfile && sync

dd if=/dev/zero of=/media/$USER/<LABEL>/testfile bs=2048 count=500000

rm /media/$USER/<LABEL>/testfile && sync

and so on, each time increasing the bs to 4096, 8192, ... and halving the 
count to copy the same amount of data.  You will soon find the transfer speed 
arrives at a peak and then reduces when an I/O bottleneck is reached.  In the 
above example I have used a 1GB file, but you may want to increase this to a 
larger size to make sure you saturate your buffers.


> Does it matter how the USB stick is formatted ?
> Can I use a raw stick with the usual default VFAT formatting ?
> Might it be better to replace that with a Linux FS, eg Ext2 ?

If you are using the USB stick to save an image file to it, then you will come 
across the VFAT file size limit of 4GB.  So, use exFAT, or ext2.

If you are using a USB stick to create a partition clone it doesn't matter 
what the USB fs type is.  It will be replaced with that of the original 
partition.

HTH.
-- 
Regards,
Mick

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to