How to create a persistent and bootable USB of a running system on a computer without shut down.

The key would be the same as the computer and offer ways to use it and install it as is on some other hardware.

. root rights ok

quote from chat Unix & Linux

To keep the things simple, I would like to create an alive backup ... example you run on your machine you have to leave ... you plug the key you load the command and then you plug the key on another place and have everything working like at home so not just a backup not just a live usb but a persitent of your system as is.

  • what do you mean by "running system" - unable to shut it down during the process?! – jet Nov 27 '16 at 22:20
  • precision added @jet – aurelien Nov 28 '16 at 5:58
  • Maybe a tool like remastersys (or respin) may fullfill your needs – although I have to admit not knowing if these do only ISO images of your system and what you will have to do additionally to get it as bootable USB stick. – Jaleks Nov 28 '16 at 7:00
  • @Jaleks as explain in the bounty, I look for a one shot command line or bash script solution – aurelien Nov 28 '16 at 13:45
  • 1
    sorry for not delivering the cake, but just the possible receipt for bakers… – Jaleks Nov 29 '16 at 14:16

I think dd is the good way to proceed.

Meanwhile, this solution works out of the box only if there is only one /dev/sd*.

For instance, i would suggest to list all /dev/sd* except usb one then make as many partition (fdisk -n) as required on usb drive and use dd for each /dev/sd* counted.

From the link:

insert the destination USB flash drive in my workstation
delete the existing vfat partition and create a single linux partition using fdisk
create a filesystem and synchronize it:

bash# mkfs.ext3 /dev/sdb1
bash# sync ; sync

remove the usb flash drive from the workstation, put it in the target PC
mount the usb drive, move the udev filesystem out of the way, and copy the local filesystem:

bash# cd /
bash# mkdir /mnt/sda1
bash# mount /dev/sda1 /mnt/sda1
bash# mkdir udev
bash# mount --move /dev /udev
bash# cp -ax / /mnt/sda1

That copy command might take awhile. When it is done, get rid of the temporary directory /udev

bash# mount --move /udev /dev
bash# rm -fr /udev

Now to make the usb drive bootable. It should still be mounted at /mnt/sda1. First, in file /mnt/sda1/boot/grub/device.map set hd(0) to /dev/sda and in /mnt/sda1/boot/grub/menu.lst set the kernel boot options correctly for each boot configuration, eg:

title Debian GNU/Linux, kernel 2.6.18-6-486 root (hd0,0) kernel /boot/vmlinuz-2.6.18-6-486 root=/dev/sda1 ro vga=792 initrd /boot/initrd.img-2.6.18-6-486 savedefault

Finally, install grub on the usb flash drive:

bash# grub-install --root-director=/mnt/sda1 /dev/sda

All done! Now you can reboot into the flash drive.

  1. Create the same set of partitions on the USB stick (cfdisk). Identification of disks / partitions is easy with cfdisk and cat /proc/partitions
  2. Format them with the same file systems (mkfs.ext....)
  3. Mount them (i.e mount /dev/sdXY /mnt/sdXY)
  4. Copy the files for each partition to the corresponding partitions (ie cp -a /bin /boot /dev /etc /home /mnt /root /sbin /usr /var /mnt/new)
  5. cd /new mkdir proc chmod 555 proc mkdir tmp chmod 777 tmp
  6. Edit fstab in USB accordingly
  7. Install Grub on USB stick mount /dev/sdXY /mnt/ #if is not mounted already mount -t proc none /mnt/proc mount -o bind /dev /mnt/dev chroot /mnt/ /bin/bash /usr/sbin/grub-install --recheck --no-floppy /dev/sdXY

You'll need a USB hard drive that is at least as large as your computer's disk. Not just the size in use, total size.

You'll need to identify which devices in /dev/ are for your main HD and for your USB. Usually /dev/sda will be your main disk, and /dev/sdb will be your first peripheral.

You can use dd on a live filesystem, though there are risks associated with that (Using dd to clone a disk while mounted - risks?). Alternatively, you can use cat, which might be safer (When is dd suitable for copying data? (or, when are read() and write() partial)).

Assuming your main device is /dev/sda and your USB device is /dev/sdb you can use either of the following:

dd if=/dev/sda of=/dev/sdb bs=64k conv=noerror,sync

cat </dev/sda >/dev/sdb

dd's noerror causes it to continue on errors, and its sync fills partial writes with zeros so things don't get shifted out of place. Though both of these things may be reasons you'd want the dd to stop, as they indicate problems with the image dd is creating.

If you want to install the USB image onto another hard disk, you can boot into it and run the same commands. It will be important to identify which is the USB drive and which the internal HD in that case, not sure if it will treat the drive it's booting from as sda.

  • This response sounds great, I have to test before give the bounty points. – aurelien Nov 28 '16 at 14:41

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.