diff options
-rw-r--r-- | backup.conf | 17 | ||||
-rwxr-xr-x | send-backup | 15 | ||||
-rwxr-xr-x | send-backup.pl | 54 |
3 files changed, 71 insertions, 15 deletions
diff --git a/backup.conf b/backup.conf new file mode 100644 index 0000000..3021ad9 --- /dev/null +++ b/backup.conf @@ -0,0 +1,17 @@ +# -*- mode: yaml -*- + +sources: + - babar/bjc + - babar/ditto + - babar/shared + - babar/vms/arceus + - babar/vms/blacktop + - babar/vms/centos7 + - babar/vms/freebsd10 + - babar/vms/gandy + - babar/vms/macos10.6 + - babar/vms/munchlax + - babar/vms/silver + - babar/vms/win10 + +dest: backup diff --git a/send-backup b/send-backup deleted file mode 100755 index 520cf0b..0000000 --- a/send-backup +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -src=babar -dst=backup - -datasets=`zfs list -Ho name -r $src | tail -n +2 | cut -d/ -f2-` -for fs in $datasets; do - from=`zfs list -Ht snap -d 1 -o name -s creation $dst/$fs | tail -1 | cut -d@ -f2` - to=`zfs list -Ht snap -d 1 -o name -s creation $src/$fs | tail -1 | cut -d@ -f2` - - echo send $fs@$from to $fs@$to - if [ "x$from" != "x$to" ]; then - zfs send -RI babar/$fs@$from babar/$fs@$to | zfs recv backup/$fs - fi -done diff --git a/send-backup.pl b/send-backup.pl new file mode 100755 index 0000000..ac12f32 --- /dev/null +++ b/send-backup.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use YAML::Tiny; +use Data::Dumper; + +my $conffile = 'backup.conf'; + +my $conf = YAML::Tiny->read($conffile); +my $srcs = $conf->[0]->{sources}; +my $dest = $conf->[0]->{dest}; + +system("zfs list $dest > /dev/null 2>&1") == 0 or + die "Error: pool '$dest' not found. Is it imported?\n"; + +foreach my $src (@$srcs) { + my ($pool, $fs) = split(/\//, $src, 2); + my ($from, $to) = limits($pool, $dest, $fs); + + unless (defined $from) { + print "Warning: $dest does not contain $fs. Initialize with:\n" . + "\tzfs send -R $pool/$to | zfs recv $dest/$fs\n"; + next; + } + + if ($from eq $to) { + print "No new snapshots for $pool/$fs.\n"; + next; + } + + print "Sending to $dest: $pool/$from -> $to\n"; + my $cmd = "zfs send -RI $pool/$from $pool/$to | zfs recv $dest/$fs"; + system($cmd) == 0 or die "Error: couldn't send snapshots with:\n\t$cmd\n." +} + +sub limits { + my ($src, $dst, $fs) = @_; + + my @dstsnaps = snaps("$dst/$fs"); + my @srcsnaps = snaps("$src/$fs"); + + ($dstsnaps[$#dstsnaps], $srcsnaps[$#srcsnaps]); +} + +sub snaps { + my $fs = shift; + + map { + chomp; + (split /\//, $_, 2)[1]; + } `zfs list -Ht snap -d 1 -o name -s creation $fs`; +} |