diff options
author | Andrew Cady <d@jerkface.net> | 2023-05-21 22:48:10 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2023-05-21 22:48:10 -0400 |
commit | ba97b75f340b9f109a80c17ab77cb5cbee155013 (patch) | |
tree | d8e5fc2c2d08ef0be4e6dc91b3b76da24fc648f4 | |
parent | 717eaef80d5129dd0b0335529b8bc9316b573c50 (diff) |
use time embedded in filename instead of stat
verify the head snapshot is a read-only snapshot early
-rwxr-xr-x | push-btrfs | 34 |
1 files changed, 24 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | MAX_AGE_SECONDS=60 | 2 | MIN_AGE_SECONDS=60 |
3 | CONFIG_DIR=/etc/btrfs-backup/remotes | 3 | CONFIG_DIR=/etc/btrfs-backup/remotes |
4 | 4 | ||
5 | die() | 5 | die() |
@@ -15,7 +15,7 @@ read_config() | |||
15 | 15 | ||
16 | check_dependencies() | 16 | check_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 | ||
34 | is_readonly_subvolume() | ||
35 | { | ||
36 | btrfs subvolume show -- "$1" | egrep -q '^ Flags:.*\breadonly\b' | ||
37 | } | ||
38 | |||
34 | get_age() | 39 | get_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 | ||
81 | is_subvolume "$src" | 86 | is_subvolume "$src" |
82 | 87 | ||
83 | if [ "$(get_age "$remote_head")" -le "$MAX_AGE_SECONDS" ] | 88 | if [ "$remote_head" ] |
84 | then | 89 | then |
85 | echo "Up-to-date." >&2 | 90 | is_readonly_subvolume "$remote_head" |
86 | exit | 91 | |
87 | else | 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 | ||
90 | fi | 98 | fi |
91 | 99 | ||
92 | btrfs send ${remote_head:+ -p "$remote_head"} -- "$new_snapshot" | pv | btrfs_receive "$dst" | 100 | new_snapshot=${src%/}/.snapshot~$(date -Ins) |
101 | btrfs subvolume snapshot -r -- "$src" "$new_snapshot" | ||
102 | if ! btrfs send ${remote_head:+ -p "$remote_head"} -- "$new_snapshot" | pv | btrfs_receive "$dst" | ||
103 | then | ||
104 | btrfs subvolume delete "$new_snapshot" | ||
105 | exit 1 | ||
106 | fi | ||
93 | jq --arg h "$new_snapshot" '. | .head=$h' <"$config_file" >"$config_file_temp" | 107 | jq --arg h "$new_snapshot" '. | .head=$h' <"$config_file" >"$config_file_temp" |
94 | mv -T -- "$config_file_temp" "$config_file" | 108 | mv -T -- "$config_file_temp" "$config_file" |
95 | 109 | ||