1

I have Ubuntu installed in an USB drive. It is not a live or persistent live install. It is a regular, full install.

Is there any way to backup the USB drive to a .img or .iso file, which I can later use to restore a bootable version of Ubuntu to this USB drive or another USB drive?

Archisman Panigrahi
  • 28,338
  • 18
  • 105
  • 212
  • I suggest that you use Clonezilla because it is efficient and safe. A Clonezilla image is a directory with a set of files, and the big files are compressed. Clonezilla is smart enough to copy only used blocks (in file systems known by its tool Partclone). See this link where I explain more about Clonezilla. – sudodus Sep 09 '22 at 15:18
  • 1
    The gnome-disks app has "Create Disk Image" as one of the menu options. This will create a .img file the size of the whole USB drive. You can use the same app to restore from a .img file to another USB drive of the same or bigger size. – user68186 Sep 09 '22 at 22:00

1 Answers1

1

Note that this is a simplistic method, that will create large files already for "small" devices.

Backup:

  1. use e.g. lsblk -ape7 to find out which /dev/... you have the USB drive positioned at
    (e.g /dev/sdc to have a true dev to show below).
  2. make sure the drive is NOT mounted
  3. Open a terminal, type
    sudo dd if=/dev/sdc bs=128k | gzip --best >/place/to/store/backup.img.gz
  4. hit Enter and fill in the (root) password

Restore:
The example /dev/sdc needs to be same-size USB drive, or larger than the backup source.

Also: Make sure /dev/sdc is the CORRECT device,
all earlier content will be lost and will NOT be recoverable.

Enter gzip -c /place/to/store/backup.img.gz | sudo dd of=/dev/sdc bs=128k
in a terminal (at the bash prompt) and provide the requested input (root password).

Additional options to dd (notrunc, noerror) and gzip might be in place.

Options:
The image file produced by dd, when uncompressed (and at least of some common content types), may be mounted as a loop device:
lodev=$(losetup -f) - will create a device, if possible, and keep the name in $lodev
sudo losetup $lodev imagefile - will attempt to use the imagefile, create a 'removable' disk and mount the partitions in it.
sudo losetup -d $lodev - will dismount the image, and deconstruct the loop device.

Hannu
  • 5,374
  • 1
  • 23
  • 40