-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package destinations | ||
|
||
import ( | ||
"github.com/sloonz/uback/container" | ||
"github.com/sloonz/uback/lib" | ||
|
||
"errors" | ||
"io" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
ErrBtrfsPath = errors.New("btrfs destination: missing path") | ||
btrfsLog = logrus.WithFields(logrus.Fields{ | ||
"destination": "btrfs", | ||
}) | ||
) | ||
|
||
type btrfsDestination struct { | ||
options *uback.Options | ||
basePath string | ||
snapshotCommand []string | ||
sendCommand []string | ||
receiveCommand []string | ||
deleteCommand []string | ||
} | ||
|
||
func newBtrfsDestination(options *uback.Options) (uback.Destination, error) { | ||
basePath := options.String["Path"] | ||
if basePath == "" { | ||
return nil, ErrFSPath | ||
} | ||
|
||
err := os.MkdirAll(basePath, 0777) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &btrfsDestination{ | ||
options: options, | ||
basePath: basePath, | ||
snapshotCommand: options.GetCommand("SnapshotCommand", []string{"btrfs", "subvolume", "snapshot"}), | ||
sendCommand: options.GetCommand("SendCommand", []string{"btrfs", "send"}), | ||
receiveCommand: options.GetCommand("ReceiveCommand", []string{"btrfs", "subvolume", "receive"}), | ||
deleteCommand: options.GetCommand("DeleteCommand", []string{"btrfs", "subvolume", "delete"}), | ||
}, nil | ||
} | ||
|
||
func (d *btrfsDestination) ListBackups() ([]uback.Backup, error) { | ||
var res []uback.Backup | ||
entries, err := os.ReadDir(d.basePath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, entry := range entries { | ||
if strings.HasPrefix(entry.Name(), ".") || strings.HasPrefix(entry.Name(), "_") || !entry.IsDir() { | ||
continue | ||
} | ||
|
||
backup, err := uback.ParseBackupFilename(entry.Name()+"-full.ubkp", true) | ||
if err != nil { | ||
btrfsLog.WithFields(logrus.Fields{ | ||
"file": entry.Name(), | ||
}) | ||
logrus.Warnf("invalid backup file: %v", err) | ||
continue | ||
} | ||
|
||
res = append(res, backup) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (d *btrfsDestination) RemoveBackup(backup uback.Backup) error { | ||
return uback.RunCommand(btrfsLog, uback.BuildCommand(d.deleteCommand, path.Join(d.basePath, string(backup.Snapshot.Name())))) | ||
} | ||
|
||
func (d *btrfsDestination) SendBackup(backup uback.Backup, data io.Reader) error { | ||
cr, err := container.NewReader(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = cr.Unseal(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd := uback.BuildCommand(d.receiveCommand, d.basePath) | ||
cmd.Stdin = cr | ||
err = uback.RunCommand(btrfsLog, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.Rename(path.Join(d.basePath, "_tmp-"+backup.Snapshot.Name()), path.Join(d.basePath, backup.Snapshot.Name())) | ||
} | ||
|
||
func (d *btrfsDestination) ReceiveBackup(backup uback.Backup) (io.ReadCloser, error) { | ||
pr, pw := io.Pipe() | ||
|
||
go func() { | ||
// btrfs source expects snapshots to be named _tmp-(snapshot), create a temporary snapshot | ||
// matching that name | ||
cmd := uback.BuildCommand(d.snapshotCommand, "-r", path.Join(d.basePath, backup.Snapshot.Name()), path.Join(d.basePath, "_tmp-"+backup.Snapshot.Name())) | ||
err := uback.RunCommand(btrfsLog, cmd) | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
defer func() { | ||
cmd := uback.BuildCommand(d.deleteCommand, path.Join(d.basePath, "_tmp-"+backup.Snapshot.Name())) | ||
_ = uback.RunCommand(btrfsLog, cmd) | ||
}() | ||
|
||
cw, err := container.NewWriter(pw, nil, "btrfs", 3) | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
|
||
cmd = uback.BuildCommand(d.sendCommand, path.Join(d.basePath, "_tmp-"+backup.Snapshot.Name())) | ||
cmd.Stdout = cw | ||
err = uback.StartCommand(btrfsLog, cmd) | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
|
||
err = cmd.Wait() | ||
|
||
if err != nil { | ||
err = cw.Close() | ||
} else { | ||
_ = cw.Close() | ||
} | ||
|
||
pw.CloseWithError(err) | ||
}() | ||
|
||
return pr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# btrfs Destination | ||
|
||
A btrfs destination can only receive unencrypted backups from a btrfs | ||
source. It will store the backup as a btrfs subvolume rather than a | ||
backup file. | ||
|
||
Note that since backups are stored as btrfs subvolumes, all backups on a | ||
btrfs destination are considered as full backups even if they were sent | ||
as incremental backups: the merge operation between the base backup and | ||
the incremental one is done automatically when the destination receive | ||
the backup. | ||
|
||
## Options | ||
|
||
### Path | ||
|
||
Required. | ||
|
||
Note: must be in a btrfs filesystem. | ||
|
||
### @SnapshotCommand | ||
|
||
Optional, defaults: `[btrfs subvolume snapshot]` | ||
|
||
### @SendCommand | ||
|
||
Optional, defaults: `[btrfs send]` | ||
|
||
### @ReceiveCommand | ||
|
||
Optional, defaults: `[btrfs receive]` | ||
|
||
### @DeleteCommand | ||
|
||
Optional, defaults: `[btrfs subvolume delete]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from .common import * | ||
|
||
class DestBtrfsTests(unittest.TestCase, SrcBaseTests): | ||
def setUp(self): | ||
test_root = os.environ.get("UBACK_BTRFS_TEST_ROOT") | ||
if test_root is None: | ||
self.tmpdir = None | ||
return | ||
|
||
ensure_dir(test_root) | ||
self.tmpdir = f"{test_root}/dest-test" | ||
if os.path.exists(self.tmpdir): | ||
raise Exception("UBACK_BTRFS_TEST_ROOT already exists") | ||
|
||
subprocess.check_call(["btrfs", "subvolume", "create", self.tmpdir]) | ||
subprocess.check_call(["btrfs", "subvolume", "create", f"{self.tmpdir}/source"]) | ||
ensure_dir(f"{self.tmpdir}/snapshots") | ||
|
||
def tearDown(self): | ||
if self.tmpdir is None: | ||
return | ||
|
||
for s in os.listdir(f"{self.tmpdir}/backups"): | ||
subprocess.check_call(["sudo", "btrfs", "subvolume", "delete", f"{self.tmpdir}/backups/{s}"]) | ||
for s in os.listdir(f"{self.tmpdir}/snapshots"): | ||
subprocess.check_call(["sudo", "btrfs", "subvolume", "delete", f"{self.tmpdir}/snapshots/{s}"]) | ||
for s in os.listdir(f"{self.tmpdir}/restore"): | ||
subprocess.check_call(["sudo", "btrfs", "subvolume", "delete", f"{self.tmpdir}/restore/{s}"]) | ||
subprocess.check_call(["sudo", "btrfs", "subvolume", "delete", f"{self.tmpdir}/source"]) | ||
subprocess.check_call(["sudo", "btrfs", "subvolume", "delete", self.tmpdir]) | ||
try: | ||
os.rmdir(os.environ.get("UBACK_BTRFS_TEST_ROOT")) | ||
except: | ||
pass | ||
|
||
def _cleanup_restore(self, d): | ||
for s in os.listdir(f"{d}/restore"): | ||
subprocess.check_call(["sudo", "btrfs", "subvolume", "delete", f"{d}/restore/{s}"]) | ||
|
||
def test_btrfs_dest(self): | ||
if self.tmpdir is None: | ||
return | ||
|
||
source = f"type=btrfs,path={self.tmpdir}/source,no-encryption=1,state-file={self.tmpdir}/state.json,snapshots-path={self.tmpdir}/snapshots,full-interval=weekly," +\ | ||
"send-command=sudo btrfs send,delete-command=sudo btrfs subvolume delete" | ||
dest = f"id=test,type=btrfs,path={self.tmpdir}/backups,no-encryption=1," +\ | ||
"send-command=sudo btrfs send,receive-command=sudo btrfs receive,delete-command=sudo btrfs subvolume delete" | ||
b1, b2, b3, b4 = self._test_src(self.tmpdir, source, dest, "receive-command=sudo btrfs receive", test_ignore=False, test_delete=True) | ||
|
||
# Check that btrfs subvolumes contains the correct data | ||
s1 = b1.split("-")[0] | ||
s2 = b2.split("-")[0] | ||
s3 = b3.split("-")[0] | ||
s4 = b4.split("-")[0] | ||
self.assertEqual(b"av1", read_file(f"{self.tmpdir}/backups/{s1}/a")) | ||
self.assertEqual(set(os.listdir(f"{self.tmpdir}/backups/{s1}")), {"a"}) | ||
self.assertEqual(b"av1", read_file(f"{self.tmpdir}/backups/{s2}/a")) | ||
self.assertEqual(b"bv1", read_file(f"{self.tmpdir}/backups/{s2}/b")) | ||
self.assertEqual(set(os.listdir(f"{self.tmpdir}/backups/{s2}")), {"a", "b"}) | ||
self.assertEqual(b"av2", read_file(f"{self.tmpdir}/backups/{s3}/a")) | ||
self.assertEqual(b"bv1", read_file(f"{self.tmpdir}/backups/{s3}/b")) | ||
self.assertEqual(set(os.listdir(f"{self.tmpdir}/backups/{s3}")), {"a", "b"}) | ||
self.assertEqual(b"av2", read_file(f"{self.tmpdir}/backups/{s4}/a")) | ||
self.assertEqual(set(os.listdir(f"{self.tmpdir}/backups/{s4}")), {"a"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters