Full system backup with SquashFS

From ArchWiki
Jump to navigation Jump to search

It is 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.

Warning: SquashFS does not support Access Control Lists.[1]

Prepare a live medium

Creating SquashFS archives requires squashfs-tools. This package is included in the official Arch Linux installation image.

Backup in live environment

Boot into the live environment 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
#!/bin/bash
if [ $# -ne 2 ] || [ ! -d "$1" ] || [ ! -d "$2" ]; then
  echo
  echo "  mksquashfs.sh <SOURCE_DIRECTORY> <BACKUP_ARCHIVE_DIRECTORY>"
  echo
  exit 1
fi
echo -ne "\n  Have you fsck'd? "
read -r
echo
mksquashfs \
  "$1" "$2/$(date +%Y%m%d_%a).sfs" \
  -comp gzip \
  -xattrs \
  -progress \
  -mem 5G \
  -wildcards \
  -e \
  var/cache/pacman/pkg \
  var/lib/pacman/sync \
  var/log/journal \
  boot/efi \
  boot/grub \
  boot/initramfs-linux"*".img \
  boot/vmlinuz-linux

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.
# mount somewhere/backup.sfs /mnt
# cp /mnt/somefile /somewhere/damaged-somefile