#!/usr/bin/env bash # Backs up what MIGRATION.md section 1 lists to the Kingston SATA SSD (sda): # the built system and disko script, WoW's settings and addons, and the home # folder. Safe to run again: the first run formats the drive (only if it is # empty), later runs only copy what changed. # # sudo ~/git/dot/tools/backup-to-sata set -euo pipefail disk=/dev/disk/by-id/ata-KINGSTON_SA400S37120G_50026B76820FD3CE mnt=/mnt/backup user=vizie home=/home/$user repo=$home/git/dot wow="$home/Games/battlenet/drive_c/Program Files (x86)/World of Warcraft/_retail_" if [ "$EUID" -ne 0 ]; then echo "Run it with sudo: sudo $0" >&2 exit 1 fi if [ ! -e "$disk" ]; then echo "The Kingston drive ($disk) isn't there." >&2 exit 1 fi # Format only a drive with nothing on it, or reuse the backup drive from an # earlier run. Anything else (partitions, another file system) stops here. label=$(blkid -o value -s LABEL "$disk" || true) fstype=$(blkid -o value -s TYPE "$disk" || true) parts=$(lsblk -n -o NAME "$disk" | wc -l) if [ "$fstype" = btrfs ] && [ "$label" = backup ]; then echo "== Backup drive already formatted, updating it" elif [ -z "$fstype" ] && [ "$parts" -eq 1 ]; then echo "== Formatting $(readlink -f "$disk") (empty Kingston SSD) as 'backup'" mkfs.btrfs -L backup "$disk" udevadm settle else echo "The Kingston drive has something on it ($fstype${label:+, label $label}); not touching it." >&2 exit 1 fi mkdir -p "$mnt" if ! mountpoint -q "$mnt"; then mount -o compress=zstd /dev/disk/by-label/backup "$mnt" fi install -d -o "$user" "$mnt/nix-cache" "$mnt/wow" echo "== Saving the built system and the disko script" sudo -u "$user" bash -euo pipefail -c ' cd "$1" sys=$(nix build --no-link --print-out-paths .#nixosConfigurations.desktop.config.system.build.toplevel) disko=$(nix build --no-link --print-out-paths .#nixosConfigurations.desktop.config.system.build.diskoScript) nix copy --to "file://$2/nix-cache?compression=zstd" "$sys" "$disko" echo "$sys" > "$2/nix-cache/system" echo "$disko" > "$2/nix-cache/disko" ' _ "$repo" "$mnt" echo "== WoW settings and addons" rsync -a --delete "$wow/WTF" "$wow/Interface" "$mnt/wow/" echo "== Home folder (about 83 GB the first time)" rsync -aHAXx --delete --info=progress2 --filter="merge $repo/backup-filter" "$home/" "$mnt/$user/" sync echo "== Done" du -sh "$mnt"/* df -h "$mnt" umount "$mnt"