dot

vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks

71 lines2543 bytesexecutable
1#!/usr/bin/env bash
2# Backs up what MIGRATION.md section 1 lists to the Kingston SATA SSD (sda):
3# the built system and disko script, WoW's settings and addons, and the home
4# folder. Safe to run again: the first run formats the drive (only if it is
5# empty), later runs only copy what changed.
6#
7# sudo ~/git/dot/tools/backup-to-sata
8set -euo pipefail
9
10disk=/dev/disk/by-id/ata-KINGSTON_SA400S37120G_50026B76820FD3CE
11mnt=/mnt/backup
12user=vizie
13home=/home/$user
14repo=$home/git/dot
15wow="$home/Games/battlenet/drive_c/Program Files (x86)/World of Warcraft/_retail_"
16
17# sudo's PATH leaves out Nix (installed with the official installer here).
18export PATH="$PATH:/nix/var/nix/profiles/default/bin"
19
20if [ "$EUID" -ne 0 ]; then
21 echo "Run it with sudo: sudo $0" >&2
22 exit 1
23fi
24if [ ! -e "$disk" ]; then
25 echo "The Kingston drive ($disk) isn't there." >&2
26 exit 1
27fi
28
29# Format only a drive with nothing on it, or reuse the backup drive from an
30# earlier run. Anything else (partitions, another file system) stops here.
31label=$(blkid -o value -s LABEL "$disk" || true)
32fstype=$(blkid -o value -s TYPE "$disk" || true)
33parts=$(lsblk -n -o NAME "$disk" | wc -l)
34if [ "$fstype" = btrfs ] && [ "$label" = backup ]; then
35 echo "== Backup drive already formatted, updating it"
36elif [ -z "$fstype" ] && [ "$parts" -eq 1 ]; then
37 echo "== Formatting $(readlink -f "$disk") (empty Kingston SSD) as 'backup'"
38 mkfs.btrfs -L backup "$disk"
39 udevadm settle
40else
41 echo "The Kingston drive has something on it ($fstype${label:+, label $label}); not touching it." >&2
42 exit 1
43fi
44
45mkdir -p "$mnt"
46if ! mountpoint -q "$mnt"; then
47 mount -o compress=zstd /dev/disk/by-label/backup "$mnt"
48fi
49install -d -o "$user" "$mnt/nix-cache" "$mnt/wow"
50
51echo "== Saving the built system and the disko script"
52sudo -u "$user" env PATH="$PATH" bash -euo pipefail -c '
53 cd "$1"
54 sys=$(nix build --no-link --print-out-paths .#nixosConfigurations.desktop.config.system.build.toplevel)
55 disko=$(nix build --no-link --print-out-paths .#nixosConfigurations.desktop.config.system.build.diskoScript)
56 nix copy --to "file://$2/nix-cache?compression=zstd" "$sys" "$disko"
57 echo "$sys" > "$2/nix-cache/system"
58 echo "$disko" > "$2/nix-cache/disko"
59' _ "$repo" "$mnt"
60
61echo "== WoW settings and addons"
62rsync -a --delete "$wow/WTF" "$wow/Interface" "$mnt/wow/"
63
64echo "== Home folder (about 83 GB the first time)"
65rsync -aHAXx --delete --info=progress2 --filter="merge $repo/backup-filter" "$home/" "$mnt/$user/"
66
67sync
68echo "== Done"
69du -sh "$mnt"/*
70df -h "$mnt"
71umount "$mnt"