Clone / Copy Bootable USB Keys (TrueNAS or XigmaNAS)

TrueNAS (formerly FreeNAS) and XigmaNAS are great NAS servers for your Home and/or Office environment. One of the problems is that both of these NAS Servers at one time (Xigmanas still does) recommended you to use a USB key to boot and then configure your drives as storage only in whatever RAID or Non-RAID configuration you wish. What if the USB boot device fails? It would be nice if you could make a copy of the USB bootable device, and to do so automatically rather than making them manually. You can! Thanks to the script we have developed using the dd command.

This HOWTO Blog and video will show you how to write a script to make a dual boot usb device right in the server itself, and to make an ISO image file so you can quickly make a new USB stick simply by burning the ISO file to a new USB stick. Let's get started.

1. Install TrueNAS or XigmaNAS onto a USB device as per Instructions

Xigmanas recommends you to install your boot OS onto a USB Drive. TrueNAS now recommends you to install your Boot OS onto installed SSD Drives such as SATA or the newer M.2 SSD type memory storage. You can howver install TrueNAS onto portable USB boot devices (aka USB Sticks). Install as be instructions.

2. Insert a Backup USB stick into an empty USB Slot

Both TrueNAS and XigmaNAS are based on FreeBSD, therefore the first boot device should be /dev/da0 and your backup usb should be /dev/da1. Ensure the backup USB device is larger that the original boot device. For example if you use two 32GB USB sticks one of the sticks may format out to 31.9 GB and the second will format out to slightly smaller at 31.8 GB. It may be best to use a 16GB for boot device and 32GB for the backup USB boot drive. You could leave both USB sticks in your server creating a Boot Mirror. If the first USB boot fails, your server should detect the second USB boot device and boot from it.

Use this command to determine your USB Devices

geom disk list

Output should be something like this (In my case I have a 16GB as dao (boot) and 30GB as da1 (backup)

Geom name: da0
1. Name: da0
   Mediasize: 16018046976 (15G)
   Sectorsize: 512
   Mode: r2w1e1
   descr: Lexar USB Flash Drive
   lunname: USB MEMORY BAR
   lunid: 2020030102060804
   rotationrate: unknown
   fwsectors: 63
   fwheads: 255

Geom name: da1
1. Name: da1
   Mediasize: 31998345216 (30G)
   Sectorsize: 512
   Mode: r0w0e0
   descr: General USB Flash Disk
   ident: 0353120400061110
   rotationrate: unknown
   fwsectors: 63
   fwheads: 255

3. USE DD command to copy USB to USB

dd if=/dev/da0 of=/dev/da1 bs=16k status=progress

or USE this command to copy USB to ISO YOUR_DESTINATION_PATH e.g /mnt/backup

dd if=/dev/da0 of=/mnt/backup.iso bs=16k status=progress

Note: The dd command is a very powerful tool. Be very careful in it's use, as you can easily wipe out your drives if it is not properly used. This is why dd is sometimes referred to or nicknamed "Disk Destroyer."

4. (Optional) Write a script using DD to backup as follows.

We have a sample script on our github site if wish to get it there. or you can copy / paste the following script and use it accordingly.

# Xigmanas / FreeNAS USB to USB Copy and USB to ISO Backup Shell Script
# by ([email protected])
# Performs a full copy of your Boot USB to a Backup USB Device as well
# as creates an backup ISO file of your USB boot device with date.
# e.g yyyy-mm-dd.xigmanas-backup-usb.iso
# Be sure to edit the configuration options at the beginning of the script to match your environment prior>
NOW=$(date +"%Y-%m-%d")
# Edit to suit your environment
DAYS_TO_KEEP=4    # 0 to keep forever
# Backup to Backup USB Device
# dd if=$BOOT_USB of=$BACKUP_USB bs=16k status=progress
# Backup to ISO
dd if=$BOOT_USB of=$BACKUP_PATH/$NOW-xigmanas-backup-usb.iso bs=16k status=progress
# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
  echo "Deleting backups older than $DAYS_TO_KEEP days"
  find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;

If you wish to visit our github page to get a copy of this script, this is the link.

5. Test your USB backup copy to see if it functions properly.

Shutdown your server. Then unplug the boot USB device and see if you can Bootup your Server with the Backup USB stick. If it boots properly you are successful in creating a backup of your boot USB.

Optionally you can create a new USB boot device from the ISO file simply by using a program such as Balena Etcher.

6. setup a cronjob to run the script daily or weekly.

This is an example of a cronjob to backup at 4:05AM daily

5 4 * * * /etc/

Clustered Networks

Located in Edmonton, AB Canada, Clustered Networks was Incorporated in 2001 and has offered Network / Internet and IT Consulting services for over 20 years. We offer personalized service! Call Us Today! - Click Here for our Contact Info

#xigmanas #truenas #backup

Posted in Linux Network Admin Tips, Network Security Tips, Tech How To on Nov 19, 2021