Repository navigation
Expand file tree
/
Copy pathbaksnapper.sh
More file actions
executable file
·967 lines (848 loc) · 26.6 KB
/
Copy pathbaksnapper.sh
File metadata and controls
executable file
·967 lines (848 loc) · 26.6 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
#! /usr/bin/env bash
# Baksnapper - Backup snapper snapshots to backup location using
# btrfs' incremental send and receive
# SPDX-FileCopyrightText: 2015-2025 Fredrik Salomonsson <plattfot@posteo.net>
# SPDX-FileCopyrightText: 2021 Nathan Dehnel
# SPDX-FileCopyrightText: 2025 Juergen Gleiss
#
# SPDX-License-Identifier: GPL-3.0-or-later
read -rd '' help <<EOF
Usage: $0 [OPTIONS...] [--source] [ADDRESS_SRC:]SOURCE [--dest] [ADDRESS_DST:]DEST
$0 --config NAME [OPTIONS...] [ADDRESS:]PATH
Backup snapper snapshot from SOURCE to DEST using btrfs incremental
send and receive. ADDRESS_SRC/_DST are specified for remote backups.
Deprecated usage is to use --config NAME for the location of the
snapper config and PATH for a directory containing snapper snapshots.
Then control direction using --type TYPE.
Options:
-a, --all Send all snapshots in the source directory. Default
is to only send the last one.
--config NAME Name of config. (deprecated)
--source SOURCE Source path, sets the same as positional SOURCE.
Useful if setting DEST in config file.
--dest DEST Destination path, sets the same as positional DEST.
Useful if setting SOURCE in config file.
--configfile NAME Name of config file to use.
--private-key KEY Specify the private KEY file to use when connecting
to a remote backup location.
--src-private-key KEY Specify the private KEY file to use when
connecting to the remote source location.
--dest-private-key KEY Specify the private KEY file to use when
connecting to the remote destination location.
-p, --prune Prune the backups by deleting snapshots that isn't
in the source directory.
--delete LIST Delete the snapshots at the backup location that are
listed in the list then exit. The list is comma
separated.
--daemon BIN Set the name of the baksnapperd, default is to call
baksnapperd.
--delete-all Delete all backup snapshots for config
--clean Delete all incomplete backup snapshots for config.
Use if backup got interrupted.
--list Print out the source and dest snapshots then exit.
--snapshot NUMBER Backup specific snapshot NUMBER, default is the last one.
--type TYPE Specify either to backup snapshots to a server
(push) or to backup snapshots from a server
(pull). Default is to push. (deprecated)
--link Create a "latest" directory linked to the latest snapshot
-v, --verbose Verbose print out.
-h, --help Print this help and then exit.
--version Print version and then exit
Examples:
1)
$0 /.snapshots/ /mnt/backup/root
Backup the last root snapshot to /mnt/backup/root, if it is the first
time it will send the whole snapshot otherwise it will just send what
have changed.
2)
$0 --delete 1,2,3,4 /.snapshots /mnt/backup/root
Delete the root's snapshots 1,2,3 and 4 for from /mnt/backup/root,
will output a warning if a snapshot doesn't exist.
3)
$0 /.snapshots foo:/mnt/backup/root
Same as example 1 except it will send the backups to the remote
machine named foo.
4)
$0 bar:/.snapshots /mnt/backup
Pull snapshots from remote machine bar at location /.snaphosts to
/mnt/backup/root. Similar behavior as 1 and 3 for how snapshots are
transfered.
Deprecated examples:
1)
$0 --config root /mnt/backup
Backup the last root snapshot to /mnt/backup, if it is the first time
it will send the whole snapshot otherwise it will just send what have
changed.
2)
$0 --delete 1,2,3,4 --config root /mnt/backup
Delete the root's snapshots 1,2,3 and 4 for from /mnt/backup, will
output a warning if a snapshot doesn't exist.
3)
$0 --config root foo:/mnt/backup
Same as example 1 except it will send the backups to the remote
machine named foo.
Note:
This script most likely needs root to be able to backup snapshots.
Exit status:
\t0 if ok.
\t1 if option error (e.g. wrong flag etc).
Author:
Fredrik "PlaTFooT" Salomonsson
EOF
read -rd '' version <<EOF
baksnapper (baksnapper) @VERSION@
Copyright (C) 2015-2025 Fredrik Salomonsson
Copyright (C) 2021 Nathan Dehnel
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
EOF
shopt -s extglob
function cleanup {
exec 4>&-
rm -rf "${p_temp_dir}"
}
function exit-msg {
cleanup
notify-send -u critical "Done backing up $src_root. Safe to turn off computer."
}
function error {
echo "[ERROR] $1" 1>&2
exit 1
}
function warning {
echo -e "[Warning] $1" 1>&2
}
function read-config {
# Get value from line
# 1 [out]: output variable
# 2 [in]: line to parse value from
function get-value {
declare -n out=$1
out=$(echo "$2" | sed -Ee 's/^[A-Z_ ]+=[ ]*(.*?)/\1/' -e 's/#.*//')
}
# Only get value from line if output is unset
# 1 [out]: output variable
# 2 [in]: line to parse value from
function get-value-if-not-set {
declare -n out=$1
local value
get-value value "$2"
out=${out-$value}
}
# Parse value as boolean
# 1 [out]: output variable
# 2 [ini: value to interpret as boolean
function parse-bool {
declare -n out=$1
if [[ "$2" =~ YES|yes|Yes|1 ]]
then
out=1
else
out=0
fi
}
# Only get boolean from line if output is unset
# 1 [out]: output variable
# 2 [in]: line to parse as bool
function get-bool-if-not-set {
declare -n out=$1
local value
get-value value "$2"
local bool
parse-bool bool "$value"
out=${out-$bool}
}
while read -r line; do
case $line in
CONFIG*=*)
get-value-if-not-set p_config "$line"
warning "CONFIG is deprecated, use SOURCE/DEST."
;;
PATH*=*)
get-value-if-not-set p_dest "$line"
warning "PATH is deprecated, use SOURCE/DEST."
;;
(SOURCE*([[:blank:]])=*)
get-value-if-not-set p_src "$line"
;;
(DEST*([[:blank:]])=*)
get-value-if-not-set p_dest "$line"
;;
DAEMON*=*)
get-value-if-not-set p_baksnapperd "$line"
;;
PRUNE*=*)
get-bool-if-not-set p_prune "$line"
;;
ALL*=*)
get-bool-if-not-set p_all "$line"
;;
VERBOSE*=*)
get-bool-if-not-set p_verbose "$line"
;;
LINK*=*)
get-bool-if-not-set p_link "$line"
;;
TYPE*=*)
get-value-if-not-set p_type "$line"
warning "TYPE is deprecated, use SOURCE/DEST."
;;
(PRIVATE_KEY*([[:blank:]])=*)
get-value value "$line"
p_ssh_args=${p_ssh_args-" -i $value"}
p_src_ssh_args=${p_src_ssh_args-" -i $value"}
p_dest_ssh_args=${p_dest_ssh_args-" -i $value"}
;;
(SOURCE_PRIVATE_KEY*([[:blank:]])=*)
get-value value "$line"
p_src_ssh_args=${p_src_ssh_args-" -i $value"}
;;
(DEST_PRIVATE_KEY*([[:blank:]])=*)
get-value value "$line"
p_dest_ssh_args=${p_dest_ssh_args-" -i $value"}
;;
*)
;;
esac
done < "$1"
}
# Use getopt to parse the command-line arguments
if ! _args=$(getopt --name "baksnapper" \
--options "adhvp" \
--long config: \
--long configfile: \
--long source: \
--long dest: \
--long delete: \
--long daemon: \
--long private-key: \
--long src-private-key: \
--long dest-private-key: \
--long snapshot: \
--long type: \
--long all \
--long delete-all \
--long clean \
--long list \
--long help \
--long prune \
--long verbose \
--long version \
--long link \
-- "$@")
then
error "Try '$0 --help for more information.'"
fi
eval set -- "$_args"
# Default behavior is to backup
p_command="backup"
# Parse options
while [[ $# -gt 0 ]]
do
key=$1
case $key in
# Commands
--clean)
p_command=clean
shift
;;
--delete)
p_command=delete
IFS=',' read -ra p_delete_list <<< "$2"
shift 2
;;
--delete-all)
p_command=delete-all
shift
;;
--list)
p_command=list
shift
;;
# Options
-a|--all)
p_all=1
shift
;;
--config)
p_config="$2"
warning "--config is deprecated, use position arguments SOURCE DEST."
shift 2
;;
--configfile)
read-config "$2"
shift 2
;;
--source)
p_src=$2
shift 2
;;
--dest)
p_dest=$2
shift 2
;;
--daemon)
p_baksnapperd=$2
shift 2
;;
-h|--help)
echo -e "$help"
exit 0
;;
--private-key)
p_ssh_args=" -i $2"
p_dest_ssh_args=("-i" "$2")
p_src_ssh_args=("-i" "$2")
shift 2
;;
--src-private-key)
p_src_ssh_args=("-i" "$2")
shift 2
;;
--dest-private-key)
p_dest_ssh_args=("-i" "$2")
shift 2
;;
-p|--prune)
p_prune=1
shift
;;
--snapshot)
p_snapshot=$2
shift 2
;;
--type)
p_type=$2
shift 2
warning "--type is deprecated, use position arguments SOURCE DEST."
;;
--link)
p_link=1
shift
;;
-v|--verbose)
set -x
shift
;;
--version)
echo -e "$version"
exit 0
;;
--)
shift
break
;;
esac
done
## Setup #######################################################################
p_baksnapperd=${p_baksnapperd=baksnapperd}
p_all=${p_all=0}
if [[ $# -ge 2 ]]
then
p_src=$1
p_dest=$2
fi
#### Deprecated config way of setup ############################################
if [[ -n $p_config ]]
then
if [[ $# -ge 1 ]]
then
p_dest=$1
fi
[[ -z $p_dest ]] && error "No path specified!"
regex='(.*?):(.*)'
if [[ $p_dest =~ $regex ]]
then
address="${BASH_REMATCH[1]}"
ssh="ssh $p_ssh_args $address"
dest="${BASH_REMATCH[2]}"
else
dest=$p_dest
fi
if [[ -n "$ssh" ]]
then
# Sanity check for ssh
$ssh -q test-connection || error "Unable to connect to $address"
fi
case ${p_type="push"} in
pull|PULL)
sender=${ssh-$p_baksnapperd}
receiver=$p_baksnapperd
;;
push|PUSH)
sender=$p_baksnapperd
receiver=${ssh-$p_baksnapperd}
;;
*)
error "Unknown type! $p_type"
;;
esac
if ! receiver_version=$($receiver version)
then
receiver_version=1
fi
if ! sender_version=$($sender version)
then
sender_version=1
fi
if [[ $receiver_version -gt 4 ]]
then
error "receiver is too new, need to use version 1-4"
fi
if [[ $sender_version -gt 4 ]]
then
error "sender is too new, need to use version 1-4"
fi
# Get the subvolume to backup
if [[ $sender_version -ge 2 ]]
then
subvolume=$($sender get-snapper-root "$p_config")
else
if ! subvolume=$($sender list-snapper-snapshots "$p_config")
then
error "Something went wrong when fetching the snapper root from sender"
fi
fi
if [[ $sender_version -ge 4 && $($sender snapshot-type) != "snapper" ]]
then
error "config interface only supports snapper"
fi
if [[ $receiver_version -ge 4 && $($receiver snapshot-type) != "snapper" ]]
then
error "config interface only supports snapper"
fi
src_root=${subvolume:?}/.snapshots
dest_root="$dest/$p_config"
elif [[ -n "$p_src" && -n "$p_dest" ]]
then
# Setup the endpoint's directory and transfer command
# 1 [out]: will store the root of the endpoint
# 2 [out]: will store the transfer command for the endpoint
# 3 [in]: source/dest input
# 4 [in]...: ssh arguments
function setup-endpoint {
declare -n root=$1
declare -n transfer=$2
local regex='(.*?):(.*)'
local ssh
local address
local input=$3
shift 3
if [[ $input =~ $regex ]]
then
address="${BASH_REMATCH[1]}"
ssh="ssh $* $address"
root="${BASH_REMATCH[2]}"
transfer=$ssh
# Sanity check for ssh
$ssh -q test-connection || error "Unable to connect to $address"
else
# shellcheck disable=SC2034
root=$input
# shellcheck disable=SC2034
transfer=$p_baksnapperd
fi
}
setup-endpoint src_root sender "$p_src" "${p_src_ssh_args[@]}"
setup-endpoint dest_root receiver "$p_dest" "${p_dest_ssh_args[@]}"
# Get the version of the daemon
# 1 [out]: save the version to this variable
# 2 [in]: variable that contains the baksnapperd command
function get-daemon-version {
declare -n version=$1
if ! version=$(${!2} version)
then
version=1
fi
if [[ $version -gt 4 ]]
then
error "$2 is too new, need to use version 1-4"
fi
}
get-daemon-version sender_version sender
get-daemon-version receiver_version receiver
else
error "You need to specify the SOURCE and DEST or the config name to backup!"
fi
# Get what snapshot type the daemon supports
# 1 [out]: variable where it will save the snapshot type.
# 2 [in]: variable that contains the baksnapperd backend
# 3 [in]: the version of the baksnapperd backend
function get-snapshot-type {
declare -n snapshot_type=$1
if [[ $3 -ge 4 ]]
then
snapshot_type=$(${!2} snapshot-type)
else
# shellcheck disable=SC2034
backend=snapper
fi
}
get-snapshot-type sender_snapshot_type sender "$sender_version"
get-snapshot-type receiver_snapshot_type receiver "$receiver_version"
# shellcheck disable=SC2154
if [[ "$sender_snapshot_type" != "$receiver_snapshot_type" ]]
then
error "Sender and receiver expects different snapshot types: sender: \
$sender_snapshot_type, receiver: $receiver_snapshot_type"
fi
snapshot_type=$sender_snapshot_type
# Check if two snapshots are the same using arithmetic
# 1 [in]: snapshot a
# 2 [in]: snapshot b
function snapshot-same-number {
(( "$1" == "$2" ))
}
# Check if snapshot a is older than b using arithmetic
# 1 [in]: snapshot a
# 2 [in]: snapshot b
function snapshot-older-number {
(( "$1" < "$2" ))
}
# Check if two snapshots are the same using string comparison
# 1 [in]: snapshot a
# 2 [in]: snapshot b
function snapshot-same-string {
[[ "$1" == "$2" ]]
}
# Check if snapshot a is older than b using string comparison
# 1 [in]: snapshot a
# 2 [in]: snapshot b
function snapshot-older-string {
[[ "$1" < "$2" ]]
}
if [[ $snapshot_type == "snapper" ]]
then
snapshot_same=snapshot-same-number
snapshot_older=snapshot-older-number
else
snapshot_same=snapshot-same-string
snapshot_older=snapshot-older-string
fi
# Using a bitmap to represent where a snapshot is located:
# 0b01 → exist at the source
# 0b10 → exist at the destination
# 0b11 → exist at both locations, i.e. common
declare -A common
only_in_src=()
only_in_dest=()
## Setup signals ###############################################################
# EXIT --> if notify-send is installed, inform the user; always cleanup
if hash notify-send 2> /dev/null
then
notify-send -u critical "Backing up $src_root. Do not turn off computer!"
trap exit-msg EXIT
else
trap cleanup EXIT
fi
# temporary file to store the summary report
p_temp_dir=$(mktemp -d "${TEMP:-/tmp/}$(basename "$0").XXXXX")
p_summary="${p_temp_dir}/summary.txt"
printf "dest_root\tsrc\tdest\tbytes\tstart\tend\tduration\n" >"${p_summary}"
if [[ $receiver_version -ge 4 ]]
then
$receiver create-location "$dest_root" || error "Problem creating backup location"
else
$receiver create-config "$dest_root" || error "Problem creating config at backup location"
fi
## Function definitions ########################################################
# - First argument ... start-time to calculate the duration
# - Second argument ... snapper id
# Function is used by single-backup and incremental-backup
function print-statistics {
local start_time
local end_time
local duration
start_time="${1}"
end_time=$(date +%s)
duration=$(( end_time - start_time ))
tr -d '\n' < "${p_temp_dir}/${2}" >>"${p_summary}"
printf "\t%s\t%s\t%s\n" "${start_time}" "${end_time}" "${duration}" >>"${p_summary}"
}
# Gather all the snapshots available at the source.
# src_snapshosts: array containing the snapshots
# num_src_snapshots: the size of src_snapshots
function gather-sender-snapshots {
# List all the snapshots available
if [[ $sender_version -ge 2 ]]
then
mapfile -t src_snapshots < <($sender list-snapshots "$src_root")
else
mapfile -d' ' -t src_snapshots < <($sender list-snapshots "$src_root"|tr -d '\n')
fi
num_src_snapshots=${#src_snapshots[@]}
}
# Gather all the snapshots available at the destination.
# dest_snapshosts: array containing the snapshots
# num_dest_snapshots: the size of dest_snapshots
function gather-receiver-snapshots {
# List all the snapshots at the backup location
if [[ $receiver_version -ge 2 ]]
then
mapfile -t dest_snapshots < <($receiver list-snapshots "$dest_root")
else
mapfile -d' ' -t dest_snapshots < <($receiver list-snapshots "$dest_root"|tr -d '\n')
fi
num_dest_snapshots=${#dest_snapshots[@]}
}
# Compare source and destination location and sort the snapshots into:
# common: exist at both locations
# only_in_src: only exist at the source
# only_in_dest: only exist at the destination
function compare-snapshots {
idx_src=0
idx_dest=0
only_in_src=()
only_in_dest=()
# NOTE: the snapshots must be sorted in ascending order hence the
# "sort -g" when getting the snapshots.
while (( idx_src < num_src_snapshots && idx_dest < num_dest_snapshots ))
do
if $snapshot_same "${src_snapshots[idx_src]}" "${dest_snapshots[idx_dest]}"
then
common["${src_snapshots[idx_src]}"]=0b11
((++idx_src))
((++idx_dest))
elif $snapshot_older "${src_snapshots[idx_src]}" "${dest_snapshots[idx_dest]}"
then
only_in_src+=("${src_snapshots[idx_src]}")
((++idx_src))
else
only_in_dest+=("${dest_snapshots[idx_dest]}")
((++idx_dest))
fi
done
# Add the rest to respective array.
for (( ; idx_dest < num_dest_snapshots; ++idx_dest ))
do
only_in_dest+=("${dest_snapshots[idx_dest]}")
done
for (( ; idx_src < num_src_snapshots; ++idx_src ))
do
only_in_src+=("${src_snapshots[idx_src]}")
done
}
# First argument is the snapshot to send.
function single-backup {
local start_time
start_time=$(date +%s)
if [[ "$snapshot_type" == "snapper" ]]
then
$receiver create-snapshot "$dest_root" "$1" ||
error "Failed to create snapshot at backup location!"
if ! $sender send-info "$src_root" "$1" | $receiver receive-info "$dest_root" "$1"
then
$receiver remove-broken-snapshot "$dest_root" "$1"
error "Failed to send snapshot info!"
fi
fi
printf "%s\t\t%s\t" "${dest_root}" "${1}" >>"${p_summary}"
exec 4>"${p_temp_dir}/${1}"
if ! $sender send-snapshot "$src_root" "$1" | tee >( wc -c >&4 ) | $receiver receive-snapshot "$dest_root" "$1"
then
$receiver remove-broken-snapshot "$dest_root" "$1"
error "Failed to send snapshot!"
fi
exec 4>&-
print-statistics "${start_time}" "${1}"
}
# First argument is the reference snapshot and the second is the
# snapshot to backup. It will only send the difference between the
# two.
function incremental-backup {
local start_time
local end_time
local duration
start_time=$(date +%s)
echo "Incremental backup $1 $2"
if [[ "$snapshot_type" == "snapper" ]]
then
$receiver create-snapshot "$dest_root" "$2" ||
error "Failed to create snapshot at backup location!"
if ! $sender send-info "$src_root" "$2" | $receiver receive-info "$dest_root" "$2"
then
$receiver remove-broken-snapshot "$dest_root" "$1"
error "Failed to send snapshot info!"
fi
fi
printf "%s\t%s\t%s\t" "${dest_root}" "${1}" "${2}" >>"${p_summary}"
exec 4>"${p_temp_dir}/${2}"
if ! $sender send-incremental-snapshot "$src_root/"{"$1","$2"} \
| tee >( wc -c >&4 ) | $receiver receive-snapshot "$dest_root" "$2"
then
$receiver remove-broken-snapshot "$dest_root" "$1"
error "Failed to send snapshot!"
fi
exec 4>&-
print-statistics "${start_time}" "${2}"
}
# Goes over the dest snapshots and delete any broken or incomplete.
function cleanup-broken-snapshots {
for snapshot in "${dest_snapshots[@]}"
do
if $receiver incomplete-snapshot "$dest_root" "$snapshot"
then
$receiver remove-broken-snapshot "$dest_root" "$snapshot"
unset "common[$snapshot]"
fi
done
}
# Main logic for the backup
function backup {
if [[ $num_src_snapshots == 0 ]]
then
error "No snapshots found."
fi
if [[ -z $p_snapshot ]]
then
p_snapshot=${src_snapshots[-1]}
else
$sender verify-snapshot "$src_root/$p_snapshot" || exit 1
fi
local num_src_only=${#only_in_src[@]}
if [[ "$num_src_only" == 0 ]]
then
echo "Already backed up all snapshots"
return 0
fi
if [[ $receiver_version -ge 3 ]]
then
echo "Clean up any broken or incomplete snapshots at destination"
cleanup-broken-snapshots
# snapshots at dest could have be removed, gather the snapshots again.
gather-receiver-snapshots
compare-snapshots
fi
# Destination doesn't have any snapshots, send the whole snapshot.
if [[ "${#common[@]}" == 0 ]]
then
echo "Initialize backup"
if [ "$p_all" -eq 1 ]
then
# Send the first snapshot as a whole then the rest will be
# sent incremental.
local snapshot=${only_in_src[0]}
# Pop the one that got sent from the array.
only_in_src=("${only_in_src[@]:1}")
((--num_src_only))
single-backup "$snapshot"
common["$snapshot"]=0b11
else
# Send the specified snapshot
single-backup "$p_snapshot"
return 0
fi
fi
# If source only contained one snapshot, then we are done.
if [[ "$num_src_only" == 0 ]]
then
return 0
fi
# Find the first common snapshot that is lower than the specified
# snapshot.
#
# 1 [out]: parent snapshot
# 2 [in]: snapshot
function find-parent {
declare -n out=$1
local snapshot=$2
local idx=${num_src_snapshots}-1
for (( ; idx >= 0; --idx ))
do
if ( $snapshot_older "${src_snapshots[idx]}" "$snapshot" ) && \
[[ ${common[${src_snapshots[idx]}]} == 0b11 ]]
then
break
fi
done
out=${src_snapshots[idx]}
}
if [[ $p_all == 0 ]]
then
# Check that it's not already synced
if [[ ${common[$p_snapshot]} == 0b11 ]]
then
error "Already synced snapshot $p_snapshot."
fi
local parent
find-parent parent "$p_snapshot"
incremental-backup "$parent" "$p_snapshot"
return 0
else
# Find the first common snapshot that is lower than the first
# source only snapshot. This will be the start of the incremental
# backup. If no one is found it will use the lowest common one.
first_src_snapshot="${only_in_src[0]}"
find-parent parent "$first_src_snapshot"
incremental-backup "$parent" "$first_src_snapshot"
common["${src_snapshots[idx]}"]=0b11
for (( src_idx = 1; src_idx < num_src_only; ++src_idx ))
do
# Find the next source snapshot to send
local src_snapshot=${only_in_src[src_idx]}
for (( ; idx < num_src_snapshots; ++idx ))
do
if $snapshot_same "${src_snapshots[idx]}" "$src_snapshot"
then
break
fi
done
# Use the common snapshot previous to it as the parent
incremental-backup "${src_snapshots[idx-1]}" "$src_snapshot"
common["$src_snapshot"]=0b11
done
fi
}
## Main ########################################################################
gather-sender-snapshots
gather-receiver-snapshots
compare-snapshots
case $p_command in
clean)
if [[ $receiver_version -lt 3 ]]
then
error "Daemon is too old, got version $receiver_version, need at least version 3."
fi
cleanup-broken-snapshots
;;
delete)
$receiver remove-snapshots "$dest_root" "${p_delete_list[@]}"
;;
delete-all)
echo -n "Are you sure you want to delete all backup snapshots from $dest_root? (y/N): "
while true
do
read -r answer
case $answer in
y|Y)
$receiver remove-snapshots "$dest_root" "${dest_snapshots[@]}"
break
;;
n|N|"")
;;
*)
echo -ne "Please answer y or n.\n(y/N): "
;;
esac
done
;;
list)
echo "source snapshots: ${src_snapshots[*]}"
echo "dest snapshots: ${dest_snapshots[*]}"
;;
*) # Default to running the backup
backup
;;
esac
if [[ ${p_prune-0} == 1 ]]
then
$receiver remove-snapshots "$dest_root" "${only_in_dest[@]}"
fi
if [[ $p_link -eq 1 ]]
then
$receiver link-latest "$dest_root"
fi
echo "--- Statistics ---"
cat "${p_summary}"
echo "--- Statistics ---"