Full system backup with SquashFS

From ArchWiki
Jump to navigation Jump to search

Overview

It's possible to make compressed read-only backup archives of whole filesystems in the SquashFS format. It is convenient since you can mount it and find/grep/cp/tree in it without decompressing the whole archive. Backup and retrieval may take less time compared to tar, but modifying an existing archive is impossible.

Prepare live CD/DVD/USB

You should have squashfs-tools installed in live CD/DVD/USB to make SquashFS archives. Please refer to Archiso#Configure the live medium on how to add squashfs-tools to packages.x86_64 and build it.

Backup in live environment

Boot into live CD/DVD/USB and mount filesystems you would like to backup.

Note: The following example is EFI-grub installation w/ sdb1 as EFI partition and sdb2 as root partition.
# fsck /dev/sdb2
# fsck /dev/sdb1
# mount -o ro /dev/sdb2 /mnt
# mount -o ro /dev/sdb1 /mnt/boot/efi
# /path/to/mksquashfs.sh SOURCE_DIRECTORY BACKUP_ARCHIVE_DIRECTORY

where

mksquashfs.sh
#!/usr/bin/env bash

# Sanity
if [ $# -ne 2 ]; then
  echo "invoke: mksquashfs.sh SOURCE_DIRECTORY BACKUP_ARCHIVE_DIRECTORY"
  exit 1
fi
echo -ne "\n\nHave you fsck'd? "
read

# Backup
mksquashfs \
  "$1" "$2/$(date +%Y%m%d_%a).sfs" \
  -comp gzip \
  -xattrs \
  -progress \
  -mem 5G \
  -wildcards \
  -e \
  boot/efi \
  boot/grub \
  boot/vmlinuz-linux \
  boot/initramfs-linux"*".img

Restore (decompress)

Warning: The following is complete but not yet tested. Data may be overwritten. Use at your own risk.
#!/bin/bash

# Path to extract files
target=/mnt

# Path to backup SquashFS archive file
archive=/somewhere/backup.sfs

unsquashfs -stat $archive
unsquashfs -force -dest $target $archive
Note: To make system bootable after restore, you should:
  1. Fix fstab
  2. arch-chroot
    1. mkinitcpio -p linux
    2. grub-install
    3. grub-mkconfig

Restore (mount and copy)

Warning: The following is not yet tested. Data may be overwritten. Use at your own risk.
  1. mount somewhere/backup.sfs /mnt
  2. cp /mnt/somefile /somewhere/damaged-somefile