Hello. Here is how to back up and restore UEFI variables and Secure Boot keys from Linux. Take the backup as root, and store it **off the machine** (USB stick, another computer), since the point is recovering when the firmware or disk has problems.

## 1. Back up

**Tools you'll need:** `efibootmgr`, `efivar`, `efitools`, `sbctl` or `mokutil` (if you use Secure Boot/shim), and `chipsec` (optional, for a full firmware dump).

```bash
sudo mkdir -p ~/efi-backup && cd ~/efi-backup
```

**a) Boot entries and boot order (human-readable)**
```bash
sudo efibootmgr -v > efibootmgr.txt
```

**b) Every EFI variable, as raw files**
```bash
sudo cp -a /sys/firmware/efi/efivars ./efivars-raw
```
Each file begins with 4 bytes of attributes, followed by the data. Copying with `-a` preserves everything. Note that some variables are read-only or firmware-managed, so a few copies may fail. That is normal.

**c) Variables in a more portable format (name, GUID, attributes, data)**
```bash
sudo efivar -l > efivar-list.txt
for v in $(efivar -l); do sudo efivar -n "$v" -p; done > efivar-dump.txt 2>&1
```

**d) Secure Boot keys (PK, KEK, db, dbx)**

With `efitools`:
```bash
for v in PK KEK db dbx; do
  sudo efi-readvar -v $v -o $v.esl
done
```
This produces `.esl` files, which are re-importable. Also save the raw authenticated variables:
```bash
for v in PK KEK db dbx; do
  sudo cp /sys/firmware/efi/efivars/$v-* ./
done
```
Also back up `dbx` and `MokList`/`MokListRT` if you use shim:
```bash
sudo mokutil --list-enrolled > mok-enrolled.txt
```

If you use **sbctl**: `sudo cp -a /var/lib/sbctl ./sbctl-backup` (this holds your *private* signing keys, so protect it).

**e) The EFI System Partition (ESP) contents**
```bash
sudo tar czf esp.tar.gz -C /boot/efi .      # or /efi, depending on your mount
```
Also record the partition table: `sudo sfdisk -d /dev/nvme0n1 > partition-table.txt`.

**f) Full firmware image (optional, most complete)**
```bash
sudo chipsec_util spi dump firmware.rom
```
This can capture everything in the SPI flash, but it may be blocked on systems with flash protection, and restoring it is risky (see below).

**g) Keep your own keys safe.** If you generated your own PK/KEK/db keys (`.key`, `.crt`, `.auth`), copy those too, as they cannot be recreated.

## 2. Restore

**Boot entries.** If `efibootmgr` entries were lost, recreate them from `efibootmgr.txt`:
```bash
sudo efibootmgr -c -d /dev/nvme0n1 -p 1 -L "Linux" -l '\EFI\ubuntu\shimx64.efi'
sudo efibootmgr -o 0001,0000       # boot order
```
Adjust disk, partition number, label and loader path to match your backup.

**ESP contents.**
```bash
sudo tar xzf esp.tar.gz -C /boot/efi
```

**Secure Boot keys.** The firmware must be in **Setup Mode** (clear the PK in firmware setup first, or the enrollment will be rejected). Then import, with PK last, since setting the PK exits Setup Mode:
```bash
sudo efi-updatevar -f db.esl db      # or -e to append instead of replace
sudo efi-updatevar -f KEK.esl KEK
sudo efi-updatevar -f PK.auth PK     # PK must be signed (.auth)
```
Files restored this way must be authenticated (`.auth`), signed by the key one level above. The raw dumps from `efi-readvar` are `.esl`, which are not signed. If you have your original `.auth` files or your PK private key, you can sign new ones with `sign-efi-sig-list`. If you don't, the usual approach in Setup Mode is to enroll `.esl` files with `efi-updatevar -f` (many firmwares accept this while in Setup Mode) or use the firmware's own key-management menu.

**Arbitrary raw variables.** Restoring directly from `efivars` is possible but only for variables that are writable. Files in `efivarfs` are immutable by default, so:
```bash
sudo chattr -i /sys/firmware/efi/efivars/VarName-GUID
# write: 4 bytes of attributes + data, e.g.
sudo cp saved-VarName-GUID /sys/firmware/efi/efivars/VarName-GUID
```
Only do this for variables you understand. Writing the wrong thing can brick some machines (notably certain Samsung laptops).

**Full firmware image.** Reflashing with `chipsec_util spi write` or `flashrom` can brick the board and is usually only appropriate with an external programmer as a fallback. Don't use it casually.

## Caveats

- Most boot and Secure Boot state can be rebuilt from the items above. Vendor-specific firmware settings (BIOS setup options, passwords, TPM state) often aren't fully exposed as EFI variables.
- **TPM-bound secrets** (BitLocker, LUKS with TPM, etc.) depend on measured boot state. Changing keys or boot entries may require re-enrolling them, so have your recovery keys saved.
- Keep the backup readable on a non-Linux system too (plain text files plus raw copies), and test your restore plan in a VM (OVMF) if you can.

If you tell me your distro, whether you use Secure Boot with shim,
your own keys, or sbctl, and whether you have a TPM-based disk
encryption setup, I can tailor the exact commands.
