summaryrefslogtreecommitdiff
path: root/assets/2026-01-17-zfs-sanoid-feature-request.txt
blob: 87207f2c0d59b83c64b45cb09a7ba116657ae4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
ZFS Detection and Sanoid Installation
======================================

When archsetup runs, it should detect if the system is on ZFS and install sanoid.

Detection:
- Check if root filesystem is ZFS: `findmnt -n -o FSTYPE /` returns "zfs"
- Or check if zpool exists: `zpool list -H 2>/dev/null`

If ZFS detected:
1. Install sanoid from AUR: `yay -S sanoid`
2. Create /etc/sanoid/sanoid.conf (see below)
3. Enable the timer: `systemctl enable --now sanoid.timer`
4. Create the syncoid replication script and systemd units (see below)

Context:
- install-archzfs can't install sanoid (AUR package)
- archsetup already has AUR helper setup, so it's the right place to install it
- syncoid (for TrueNAS replication) comes with the sanoid package

Added: 2026-01-17

================================================================================
SANOID CONFIGURATION (/etc/sanoid/sanoid.conf)
================================================================================

# Sanoid configuration for ZFS snapshots
# Less aggressive - TrueNAS handles long-term backups

#############################
# Templates
#############################

[template_production]
    # Local rollback capability
    hourly = 6
    daily = 7
    weekly = 2
    monthly = 1
    autosnap = yes
    autoprune = yes

[template_backup]
    # Less frequent for large/static data
    hourly = 0
    daily = 3
    weekly = 2
    monthly = 1
    autosnap = yes
    autoprune = yes

[template_none]
    autosnap = no
    autoprune = yes

#############################
# Datasets
#############################

[zroot/ROOT/default]
    use_template = production

[zroot/home]
    use_template = production
    recursive = yes

[zroot/media]
    use_template = backup

[zroot/vms]
    use_template = backup

[zroot/var/log]
    use_template = production

[zroot/var/lib/pacman]
    use_template = production

[zroot/var/cache]
    use_template = none

[zroot/var/tmp]
    use_template = none

[zroot/tmp]
    use_template = none

================================================================================
SYNCOID REPLICATION SCRIPT (/usr/local/bin/zfs-replicate)
================================================================================

#!/bin/bash
# zfs-replicate - Replicate ZFS datasets to TrueNAS
#
# Usage:
#   zfs-replicate              # Replicate all configured datasets
#   zfs-replicate [dataset]    # Replicate specific dataset

set -e

# TrueNAS Configuration
# Try local network first, fall back to tailscale
TRUENAS_LOCAL="truenas.local"
TRUENAS_TAILSCALE="truenas"
TRUENAS_USER="root"
TRUENAS_POOL="vault"
BACKUP_PATH="backups"  # TODO: Configure actual path

# Datasets to replicate
DATASETS="zroot/ROOT/default zroot/home zroot/media zroot/vms"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

info()  { echo -e "${GREEN}[INFO]${NC} $1"; }
warn()  { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }

command -v syncoid >/dev/null 2>&1 || error "syncoid not found. Install sanoid package."

# Determine which host to use
determine_host() {
    if ping -c 1 -W 2 "$TRUENAS_LOCAL" &>/dev/null; then
        echo "$TRUENAS_LOCAL"
    elif ping -c 1 -W 2 "$TRUENAS_TAILSCALE" &>/dev/null; then
        echo "$TRUENAS_TAILSCALE"
    else
        error "Cannot reach TrueNAS at $TRUENAS_LOCAL or $TRUENAS_TAILSCALE"
    fi
}

TRUENAS_HOST=$(determine_host)
info "Using TrueNAS host: $TRUENAS_HOST"

# Single dataset mode
if [[ -n "$1" ]]; then
    dataset="$1"
    dest="$TRUENAS_USER@$TRUENAS_HOST:$TRUENAS_POOL/$BACKUP_PATH/${dataset#zroot/}"
    info "Replicating $dataset -> $dest"
    syncoid --recursive "$dataset" "$dest"
    exit 0
fi

# Full replication
info "Starting ZFS replication to $TRUENAS_HOST"
echo ""

for dataset in $DATASETS; do
    dest="$TRUENAS_USER@$TRUENAS_HOST:$TRUENAS_POOL/$BACKUP_PATH/${dataset#zroot/}"
    info "Replicating $dataset -> $dest"

    if syncoid --recursive "$dataset" "$dest"; then
        info "  Success"
    else
        warn "  Failed (will retry next run)"
    fi
    echo ""
done

info "Replication complete."

================================================================================
SYSTEMD SERVICE (/etc/systemd/system/zfs-replicate.service)
================================================================================

[Unit]
Description=ZFS Replication to TrueNAS
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/zfs-replicate
User=root

[Install]
WantedBy=multi-user.target

================================================================================
SYSTEMD TIMER (/etc/systemd/system/zfs-replicate.timer)
================================================================================

[Unit]
Description=Run ZFS replication nightly

[Timer]
OnCalendar=*-*-* 02:00:00
RandomizedDelaySec=1800
Persistent=true

[Install]
WantedBy=timers.target

================================================================================
ENABLE REPLICATION
================================================================================

After SSH key auth is set up to TrueNAS:
  systemctl enable --now zfs-replicate.timer