bin/.archived/btrfs_backup

39 lines
1.3 KiB
Plaintext
Raw Normal View History

2023-11-26 02:45:49 +01:00
#!/usr/bin/env bash
2024-10-08 09:42:38 +02:00
SNAP_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
2023-11-26 02:45:49 +01:00
# Check if device is mounted
2024-10-08 09:42:38 +02:00
if ! grep "/mnt/storage" /etc/mtab >/dev/null; then
logger -p error -t btrfs_backup "ERROR: /mnt/storage is not mounted."
2023-11-26 02:45:49 +01:00
exit 1
fi
create_snapshot() {
if ! btrfs subvolume snapshot -r "$1" "${1}/.snapshots/$2-$SNAP_DATE" >/dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error creating snapshot of $1" | tee -a "$LOGFILE"
notify-send -i computer-fail "Error creating snapshot of $1"
exit 1
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Create snapshot of $1: OK" | tee -a "$LOGFILE"
fi
}
send_snapshot() {
mkdir -p "/srv/backup/$SNAP_DATE"
if ! btrfs send -q "${1}/.snapshots/$2-$SNAP_DATE" | btrfs receive -q "/srv/backup/$SNAP_DATE"; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error sending snapshot of $1 to /srv/backup" | tee -a "$LOGFILE"
notify-send -i computer-fail "Error sending snapshot of $1 to /srv/backup"
exit 1
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Send snapshot of $1 to /srv/backup: OK" | tee -a "$LOGFILE"
fi
}
# Create root and home snapshots
create_snapshot "/" "root"
create_snapshot "/home" "home"
# Send root and home snapshots
send_snapshot "/" "root"
send_snapshot "/home" "home"