Skip to content

Instantly share code, notes, and snippets.

@Guillawme
Forked from marcan/fedora-autorestic.md
Created October 29, 2024 16:01
Show Gist options
  • Save Guillawme/1f2c829bc481eed25ef82801f730d702 to your computer and use it in GitHub Desktop.
Save Guillawme/1f2c829bc481eed25ef82801f730d702 to your computer and use it in GitHub Desktop.
Setting up autorestic on Fedora (Asahi) for automated backups with btrfs
  • Install restic and autorestic
  • sudo mkdir -p /var/cache/restic
  • Set up /etc/autorestic.yml roughly as follows:
backends:
    mybackend:
        # Your backend options here, see the documentation
global:
    all:
        cache-dir: /var/cache/restic
    forget:
        # Your backup history options here
        keep-daily: 14
        keep-monthly: 12
        keep-weekly: 8
locations:
    home:
        from:
            - /home/@snap
        type: ""
        to:
            - mybackend
        cron: '0 3 * * *'
        hooks:
            dir: ""
            prevalidate:
                - btrfs subvolume delete /home/@snap || true
                - btrfs subvolume snapshot /home/ /home/@snap
            before: []
            after:
                - btrfs subvolume delete /home/@snap
            # Optionally, put something here to signal the successful (or not) backup for monitoring
            #success:
            #    - /usr/local/bin/autorestic-pusher.sh
            #failure: []
        options:
            backup:
                exclude:
                    - '*.nobak'
                    - .cache/*
                one-file-system:
                    - true
        forget: prune
        copyoption: {}
    root:
        from:
            - /@snap/var
            - /@snap/etc
            - /@snap/srv
            - /@snap/usr/local
            - /@snap/root
        type: ""
        to:
            - mybackend
        cron: '0 2 * * *'
        hooks:
            dir: ""
            prevalidate:
                - btrfs subvolume delete /@snap || true
                - btrfs subvolume snapshot / /@snap
            before: []
            after:
                - btrfs subvolume delete /@snap
            # Optionally, put something here to signal the successful (or not) backup for monitoring
            #success:
            #    - /usr/local/bin/autorestic-pusher.sh
            #failure: []
        options:
            backup:
                exclude:
                    - '*.nobak'
                    - .cache/*
                    - var/swap/swapfile
                    - var/cache
                    - var/lib/mock/*/root/
                one-file-system:
                    - true
        forget: prune
        copyoption: {}
version: 2

This configures backups of the volatile root filesystem folders at 2AM every day, and of /home at 3AM every day. Root and home are handled separately because they are different btrfs subvolumes in Fedora Asahi, so they are snapshotted separately.

  • Create /etc/systemd/system/autorestic.service:
[Unit]
Description=Autorestic backups
Wants=autorestic.timer

[Service]
# This is useful for 0.17.0 and later to play nicely with btrfs snapshots
Environment="RESTIC_FEATURES=device-id-for-hardlinks=true"
Type=oneshot
ExecStart=/usr/bin/autorestic -c /etc/autorestic.yml --ci exec -a unlock
ExecStart=/usr/bin/autorestic -c /etc/autorestic.yml --ci cron

[Install]
WantedBy=multi-user.target

Note: the unlock command is necessary to clear out stale locks, otherwise backups can stall or forget might not run to clear out old backups. It should not remove any actual active locks (but that should never happen anyway as long as you don't invoke restic manually or from multiple hosts on the same repo).

  • Create /etc/systemd/system/autorestic.timer:
[Unit]
Description=autorestic
Requires=autorestic.service

[Timer]
Unit=autorestic.service
OnBootSec=15min
OnUnitActiveSec=10min

[Install]
WantedBy=timers.target
  • sudo systemctl enable --now autorestic.timer

To check that everything is set up properly for your backend, do autorestic -c /etc/autorestic.yml check (this will take a long time once the repo has data, but should be fast initially).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment