Skip to content
This repository was archived by the owner on Jun 15, 2025. It is now read-only.

Commit 65c62c5

Browse files
committed
Add --reverse option
When restoring data from a backup the source and the destination repositories are logically swapped over the creation case. That is, if you backed up your data from /snapshots to /backup, for instance, you would restore it from /backup to /snapshots. This behavior is logical and intuitive. However, sometimes it is more easily to have the program deal with this swapping of the parameters in order to not have to create an entirely new command line with these two parameters reversed. For that matter, this change introduces the --reverse option that essentially swaps the source and the destination repository. Note that although not intended, the newly introduced option can also be used in the non-restore case, i.e., when creating a new snapshot in the source directory and synchronizing it with a destination repository.
1 parent 267c5ba commit 65c62c5

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

btrfs-backup/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,15 @@ To restore the latest snapshot for the given subvolume from the backup,
5151
the following command can be used:
5252

5353
``$ btrfs-backup --restore --subvolume=subvolume/ backup/ snapshots/``
54+
55+
Alternatively, you can use the --reverse option to keep the order of the
56+
source and destination repository that was used during backup (as
57+
opposed to restoration). This option exists for convenience only, so
58+
that not the entire command line has to be amended but rather two
59+
options can be appended to convert a backup operation into a restore
60+
operation.
61+
62+
```
63+
$ btrfs-backup --subvolume=subvolume/ snapshots/ backup/ --restore
64+
--reverse
65+
```

btrfs-backup/src/deso/btrfs/main.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
"""The main module interfaces with the user input and sets up bits required for execution."""
2121

22+
from deso.btrfs.alias import (
23+
alias,
24+
)
2225
from sys import (
2326
stderr,
2427
)
@@ -87,6 +90,10 @@ def main(argv):
8790
"This option triggers the opposite behavior to what is done by "
8891
"default.",
8992
)
93+
parser.add_argument(
94+
"--reverse", action="store_true", dest="reverse", default=False,
95+
help="Reverse (i.e., swap) the source and destination repositories.",
96+
)
9097
parser.add_argument(
9198
"-s", "--subvolume", action="append", metavar="subvolume", nargs=1,
9299
dest="subvolumes", required=True,
@@ -101,8 +108,15 @@ def main(argv):
101108
# Note that argv contains the path to the program as the first element
102109
# which we kindly ignore.
103110
namespace = parser.parse_args(argv[1:])
104-
# The namespace's subvolumes are stored as a list of list of strings.
105-
# Convert it to a list of strings.
106-
subvolumes = [x for x, in namespace.subvolumes]
107-
return run(subvolumes, namespace.src, namespace.dst,
108-
restore=namespace.restore)
111+
112+
with alias(namespace) as ns:
113+
# The namespace's subvolumes are stored as a list of list of
114+
# strings. Convert it to a list of strings.
115+
subvolumes = [x for x, in ns.subvolumes]
116+
117+
if ns.reverse:
118+
src_repo, dst_repo = ns.dst, ns.src
119+
else:
120+
src_repo, dst_repo = ns.src, ns.dst
121+
122+
return run(subvolumes, src_repo, dst_repo, restore=ns.restore)

btrfs-backup/src/deso/btrfs/test/testMain.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ def backup():
7878
"""Invoke the program to backup snapshots/subvolumes."""
7979
run(m.path("snapshots"), b.path("backup"))
8080

81-
def restore():
81+
def restore(*options, reverse=False):
8282
"""Invoke the program to restore snapshots/subvolumes."""
83-
run(b.path("backup"), m.path("snapshots"), "--restore")
83+
if not reverse:
84+
src = b.path("backup")
85+
dst = m.path("snapshots")
86+
else:
87+
src = m.path("snapshots")
88+
dst = b.path("backup")
89+
90+
run(src, dst, "--restore", *options)
8491

8592
with alias(self._mount) as m:
8693
# We backup our data to a different btrfs volume.
@@ -113,6 +120,19 @@ def restore():
113120
self.assertContains(m.path(user, "data", "movie.mp4"), "abcdefgh")
114121
self.assertContains(m.path(root, ".ssh", "key.pub"), "1234567890")
115122

123+
# Case 3) Once again delete all snapshots but this time
124+
# restore them in conjunction with the --reverse
125+
# option.
126+
wipeSubvolumes(m.path("snapshots"))
127+
128+
# This time we use the '--reverse' option.
129+
restore("--reverse", reverse=True)
130+
131+
user, root = glob(m.path("snapshots", "*"))
132+
133+
self.assertContains(m.path(user, "data", "movie.mp4"), "abcdefgh")
134+
self.assertContains(m.path(root, ".ssh", "key.pub"), "1234567890")
135+
116136

117137
if __name__ == "__main__":
118138
main()

0 commit comments

Comments
 (0)