#!/usr/bin/env bash set -Eeuo pipefail IFS=$'\n\t' readonly REPO_DIR="$( cd "$(dirname "${BASH_SOURCE[0]}")/.." pwd )" log() { printf '[*] %s\n' "$*" } warn() { printf '[!] %s\n' "$*" >&2 } copy_root_file() { local source="$1" local destination="$2" local mode="${3:-0644}" if sudo test -f "$source"; then sudo install \ -D \ -o "$(id -u)" \ -g "$(id -g)" \ -m "$mode" \ "$source" \ "$destination" else warn "Fichier absent, ignoré : $source" fi } mkdir -p \ "$REPO_DIR/pkglist" \ "$REPO_DIR/inventory/systemd" \ "$REPO_DIR/inventory/storage" \ "$REPO_DIR/inventory/boot" \ "$REPO_DIR/rootfs/etc/snapper/configs" \ "$REPO_DIR/rootfs/etc/systemd" \ "$REPO_DIR/rootfs/usr/local/sbin" log "Listes des paquets explicitement installés" pacman -Qqen | LC_ALL=C sort -u \ > "$REPO_DIR/pkglist/pacman.txt" pacman -Qqem | LC_ALL=C sort -u \ > "$REPO_DIR/pkglist/aur.txt" pacman -Qdtq 2>/dev/null | LC_ALL=C sort -u \ > "$REPO_DIR/pkglist/orphans.txt" || true log "Inventaire systemd système" systemctl list-unit-files \ --type=service \ --state=enabled \ --no-legend \ --no-pager | LC_ALL=C sort \ > "$REPO_DIR/inventory/systemd/system-enabled.txt" systemctl --type=service \ --state=running \ --no-legend \ --no-pager | LC_ALL=C sort \ > "$REPO_DIR/inventory/systemd/system-running.txt" systemctl list-timers \ --all \ --no-legend \ --no-pager | LC_ALL=C sort \ > "$REPO_DIR/inventory/systemd/system-timers.txt" systemctl --failed \ --no-legend \ --no-pager \ > "$REPO_DIR/inventory/systemd/system-failed.txt" || true log "Inventaire systemd utilisateur" systemctl --user list-unit-files \ --type=service \ --state=enabled \ --no-legend \ --no-pager | LC_ALL=C sort \ > "$REPO_DIR/inventory/systemd/user-enabled.txt" systemctl --user --type=service \ --state=running \ --no-legend \ --no-pager | LC_ALL=C sort \ > "$REPO_DIR/inventory/systemd/user-running.txt" systemctl --user list-timers \ --all \ --no-legend \ --no-pager | LC_ALL=C sort \ > "$REPO_DIR/inventory/systemd/user-timers.txt" systemctl --user --failed \ --no-legend \ --no-pager \ > "$REPO_DIR/inventory/systemd/user-failed.txt" || true log "Inventaire du stockage" lsblk -e7 \ -o NAME,MODEL,SIZE,ROTA,TRAN,FSTYPE,FSVER,LABEL,UUID,PARTUUID,MOUNTPOINTS \ > "$REPO_DIR/inventory/storage/lsblk.txt" findmnt \ > "$REPO_DIR/inventory/storage/findmnt.txt" cp /etc/fstab \ "$REPO_DIR/inventory/storage/fstab.current" sudo btrfs subvolume list / \ > "$REPO_DIR/inventory/storage/btrfs-subvolumes.txt" sudo btrfs filesystem usage / \ > "$REPO_DIR/inventory/storage/btrfs-usage.txt" sudo btrfs scrub status / \ > "$REPO_DIR/inventory/storage/btrfs-scrub.txt" || true free -h \ > "$REPO_DIR/inventory/storage/memory.txt" swapon --show \ > "$REPO_DIR/inventory/storage/swap.txt" if command -v nvme >/dev/null 2>&1 && [[ -e /dev/nvme0 ]]; then sudo nvme smart-log /dev/nvme0 \ > "$REPO_DIR/inventory/storage/nvme-health.txt" else : > "$REPO_DIR/inventory/storage/nvme-health.txt" fi log "Sauvegarde des configurations système" copy_root_file \ /etc/pacman.conf \ "$REPO_DIR/rootfs/etc/pacman.conf" copy_root_file \ /etc/makepkg.conf \ "$REPO_DIR/rootfs/etc/makepkg.conf" copy_root_file \ /etc/mkinitcpio.conf \ "$REPO_DIR/rootfs/etc/mkinitcpio.conf" copy_root_file \ /etc/nftables.conf \ "$REPO_DIR/rootfs/etc/nftables.conf" copy_root_file \ /etc/limine-snapper-sync.conf \ "$REPO_DIR/rootfs/etc/limine-snapper-sync.conf" copy_root_file \ /etc/tlp.d/10-archasp.conf \ "$REPO_DIR/rootfs/etc/tlp.d/10-archasp.conf" copy_root_file \ /etc/systemd/zram-generator.conf \ "$REPO_DIR/rootfs/etc/systemd/zram-generator.conf" copy_root_file \ /etc/snapper/configs/root \ "$REPO_DIR/rootfs/etc/snapper/configs/root" copy_root_file \ /usr/local/sbin/batlimit-set \ "$REPO_DIR/rootfs/usr/local/sbin/batlimit-set" \ 0755 copy_root_file \ /usr/local/sbin/vmware-stack \ "$REPO_DIR/rootfs/usr/local/sbin/vmware-stack" \ 0755 log "Génération du template Limine" if sudo test -f /boot/limine.conf; then mkdir -p "$REPO_DIR/templates" sudo awk ' /^[[:space:]]*### Auto-generated by limine-snapper-sync/ { exit } { print } ' /boot/limine.conf | sed -E \ 's/root=UUID=[[:xdigit:]-]+/root=UUID=@ROOT_UUID@/g' \ > "$REPO_DIR/templates/limine.conf" else warn "Fichier absent : /boot/limine.conf" fi log "Sauvegarde terminée" printf '\n' printf 'Pacman : %s\n' \ "$(wc -l < "$REPO_DIR/pkglist/pacman.txt")" printf 'AUR : %s\n' \ "$(wc -l < "$REPO_DIR/pkglist/aur.txt")" printf 'Orphelins : %s\n' \ "$(wc -l < "$REPO_DIR/pkglist/orphans.txt")" printf '\n' git -C "$REPO_DIR" status --short || true