-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
0 parents
commit 1681f6f
Showing
4 changed files
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM python:onbuild | ||
|
||
ENTRYPOINT [ "python", "./volume-backup.py" ] |
Empty file.
Empty file.
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,66 @@ | ||
import os | ||
import sys | ||
import tarfile | ||
import shutil | ||
|
||
|
||
volume_dir = "/volume" | ||
backup_dir = "/backup" | ||
tmp_dir = "/tmp" | ||
data_dir = "data" | ||
|
||
|
||
def backup(archive_name): | ||
with tarfile.open(backup_dir + "/" + archive_name, "w:bz2") as tar: | ||
#TODO maybe instead of tarbomb copy to backup or archive_name | ||
# but then make sure permissions work (copy2 doesn't keep them) | ||
tar.add(volume_dir, ".") | ||
|
||
|
||
def restore(archive_name): | ||
|
||
for f in os.listdir(volume_dir): | ||
fp = volume_dir + "/" + f | ||
if os.path.isfile(fp): | ||
os.unlink(fp) | ||
elif os.path.isdir(fp): | ||
shutil.rmtree(fp) | ||
else: | ||
raise Exception("Neither a file nor a directory: " + fp) | ||
|
||
with tarfile.open(backup_dir + "/" + archive_name, "r:bz2") as tar: | ||
#TODO: validate if archive contains only relative paths and no .. | ||
#TODO: numeric_owner will cause trouble when using uid mapping - make it configurable | ||
tar.extractall(volume_dir, numeric_owner=True) | ||
|
||
|
||
def usage(): | ||
print("Usage: volume-backup <backup|restore> <archive>") | ||
exit(1) | ||
|
||
def main(): | ||
if not os.path.isdir(volume_dir): | ||
print(volume_dir + " directory does not exist") | ||
if not os.path.isdir(backup_dir): | ||
print(backup_dir + " directory does not exist") | ||
|
||
#TODO: validate that volume is not used, list containers / names | ||
|
||
if not len(sys.argv) == 3: | ||
usage() | ||
|
||
operation = sys.argv[1] | ||
|
||
archive_name = sys.argv[2] | ||
if not archive_name.endswith(".tar.bz2"): | ||
archive_name += ".tar.bz2" | ||
|
||
if operation == "backup": | ||
backup(archive_name) | ||
elif operation == "restore": | ||
restore(archive_name) | ||
else: | ||
usage() | ||
|
||
if __name__ == "__main__": | ||
main() |