summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2023-05-21 22:48:10 -0400
committerAndrew Cady <d@jerkface.net>2023-05-21 22:48:10 -0400
commitba97b75f340b9f109a80c17ab77cb5cbee155013 (patch)
treed8e5fc2c2d08ef0be4e6dc91b3b76da24fc648f4
parent717eaef80d5129dd0b0335529b8bc9316b573c50 (diff)
use time embedded in filename instead of stat
verify the head snapshot is a read-only snapshot early
-rwxr-xr-xpush-btrfs34
1 files changed, 24 insertions, 10 deletions
diff --git a/push-btrfs b/push-btrfs
index 5e9b537..3284b69 100755
--- a/push-btrfs
+++ b/push-btrfs
@@ -1,5 +1,5 @@
1#!/bin/bash 1#!/bin/bash
2MAX_AGE_SECONDS=60 2MIN_AGE_SECONDS=60
3CONFIG_DIR=/etc/btrfs-backup/remotes 3CONFIG_DIR=/etc/btrfs-backup/remotes
4 4
5die() 5die()
@@ -15,7 +15,7 @@ read_config()
15 15
16check_dependencies() 16check_dependencies()
17{ 17{
18 for c in flock jq btrfs pv realpath stat date mv ln rm 18 for c in flock jq btrfs pv realpath stat egrep date mv ln rm
19 do 19 do
20 command -v $c >/dev/null 20 command -v $c >/dev/null
21 done 21 done
@@ -31,10 +31,15 @@ is_subvolume()
31 btrfs subvolume show -- "$1" >/dev/null 2>&1 31 btrfs subvolume show -- "$1" >/dev/null 2>&1
32} 32}
33 33
34is_readonly_subvolume()
35{
36 btrfs subvolume show -- "$1" | egrep -q '^ Flags:.*\breadonly\b'
37}
38
34get_age() 39get_age()
35{ 40{
36 now=$(date +%s) 41 now=$(date +%s)
37 then=$(stat -L -c %Y "$1") 42 then=$(date +%s -d "${1##*.snapshot~}")
38 echo $((now - then)) 43 echo $((now - then))
39} 44}
40 45
@@ -80,16 +85,25 @@ esac
80 85
81is_subvolume "$src" 86is_subvolume "$src"
82 87
83if [ "$(get_age "$remote_head")" -le "$MAX_AGE_SECONDS" ] 88if [ "$remote_head" ]
84then 89then
85 echo "Up-to-date." >&2 90 is_readonly_subvolume "$remote_head"
86 exit 91
87else 92 AGE=$(get_age "$remote_head")
88 new_snapshot=${src%/}/.snapshot~$(date -Ins) 93 if [ "$AGE" -le "$MIN_AGE_SECONDS" ]
89 btrfs subvolume snapshot -r -- "$src" "$new_snapshot" 94 then
95 echo "Up-to-date." >&2
96 exit
97 fi
90fi 98fi
91 99
92btrfs send ${remote_head:+ -p "$remote_head"} -- "$new_snapshot" | pv | btrfs_receive "$dst" 100new_snapshot=${src%/}/.snapshot~$(date -Ins)
101btrfs subvolume snapshot -r -- "$src" "$new_snapshot"
102if ! btrfs send ${remote_head:+ -p "$remote_head"} -- "$new_snapshot" | pv | btrfs_receive "$dst"
103then
104 btrfs subvolume delete "$new_snapshot"
105 exit 1
106fi
93jq --arg h "$new_snapshot" '. | .head=$h' <"$config_file" >"$config_file_temp" 107jq --arg h "$new_snapshot" '. | .head=$h' <"$config_file" >"$config_file_temp"
94mv -T -- "$config_file_temp" "$config_file" 108mv -T -- "$config_file_temp" "$config_file"
95 109