| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | cmd_dir="$(cd "$(dirnamescip-shell system . . dirname. "$0")" && pwdscip-shell system . . pwd.)" || exit $? |
| 4 | |
| 5 | set -o pipefail |
| 6 | |
| 7 | mustscip-shell shell bup 0.33.10-1 must().() { |
| 8 | local file=${BASH_SOURCE[0]} |
| 9 | local line=${BASH_LINENO[0]} |
| 10 | "$@" |
| 11 | local rc=$? |
| 12 | if testscip-shell system . . test. $rc -ne 0; then |
| 13 | echoscip-shell system . . echo. "Failed at line $line in $file" 1>&2 |
| 14 | exit $rc |
| 15 | fi |
| 16 | } |
| 17 | |
| 18 | usagescip-shell shell bup 0.33.10-1 usage().() { |
| 19 | echoscip-shell system . . echo. "Usage: bup import-rdiff-backup [-n]" \ |
| 20 | "<path to rdiff-backup root> <backup name>" |
| 21 | echoscip-shell system . . echo. "-n,--dry-run: just print what would be done" |
| 22 | exit 1 |
| 23 | } |
| 24 | |
| 25 | control_cscip-shell shell bup 0.33.10-1 control_c().() { |
| 26 | echoscip-shell system . . echo. "bup import-rdiff-backup: signal 2 received" 1>&2 |
| 27 | exit 128 |
| 28 | } |
| 29 | |
| 30 | mustscip-shell shell bup 0.33.10-1 must(). trap control_c INT |
| 31 | |
| 32 | dry_run= |
| 33 | while [scip-shell system . . `[`. "$1" = "-n" -o "$1" = "--dry-run" ]; do |
| 34 | dry_run=echo |
| 35 | shift |
| 36 | done |
| 37 | |
| 38 | bupscip-shell shell bup 0.33.10-1 bup().() |
| 39 | { |
| 40 | $dry_run "$cmd_dir/bup" "$@" |
| 41 | } |
| 42 | |
| 43 | snapshot_root="$1" |
| 44 | branch="$2" |
| 45 | |
| 46 | [scip-shell system . . `[`. -n "$snapshot_root" -a "$#" = 2 ] || usagescip-shell shell bup 0.33.10-1 usage(). |
| 47 | |
| 48 | if [scip-shell system . . `[`. ! -e "$snapshot_root/." ]; then |
| 49 | echoscip-shell system . . echo. "'$snapshot_root' isn't a directory!" |
| 50 | exit 1 |
| 51 | fi |
| 52 | |
| 53 | |
| 54 | backups=$(mustscip-shell shell bup 0.33.10-1 must(). rdiff-backup --list-increments --parsable-output "$snapshot_root") \ |
| 55 | || exit $? |
| 56 | backups_count=$(echoscip-shell system . . echo. "$backups" | mustscip-shell shell bup 0.33.10-1 must(). wc -l) || exit $? |
| 57 | counter=1 |
| 58 | echoscip-shell system . . echo. "$backups" | |
| 59 | while read timestamp type; do |
| 60 | tmpdir=$(mustscip-shell shell bup 0.33.10-1 must(). mktemp -d import-rdiff-backup-XXXXXXX) || exit $? |
| 61 | |
| 62 | echoscip-shell system . . echo. "Importing backup from $(datescip-shell system . . date. -d @$timestamp +%c) " \ |
| 63 | "($counter / $backups_count)" 1>&2 |
| 64 | echoscip-shell system . . echo. 1>&2 |
| 65 | |
| 66 | echoscip-shell system . . echo. "Restoring from rdiff-backup..." 1>&2 |
| 67 | mustscip-shell shell bup 0.33.10-1 must(). rdiff-backup -r $timestamp "$snapshot_root" "$tmpdir" |
| 68 | echoscip-shell system . . echo. 1>&2 |
| 69 | |
| 70 | echoscip-shell system . . echo. "Importing into bup..." 1>&2 |
| 71 | tmpidx=$(mustscip-shell shell bup 0.33.10-1 must(). mktemp -u import-rdiff-backup-idx-XXXXXXX) || exit $? |
| 72 | mustscip-shell shell bup 0.33.10-1 must(). bup index -ux -f "$tmpidx" "$tmpdir" |
| 73 | mustscip-shell shell bup 0.33.10-1 must(). bup save --strip --date="$timestamp" -f "$tmpidx" -n "$branch" "$tmpdir" |
| 74 | mustscip-shell shell bup 0.33.10-1 must(). rm -f "$tmpidx" |
| 75 | |
| 76 | mustscip-shell shell bup 0.33.10-1 must(). rm -rf "$tmpdir" |
| 77 | counter=$((counter+1)) |
| 78 | echoscip-shell system . . echo. 1>&2 |
| 79 | echoscip-shell system . . echo. 1>&2 |
| 80 | done |