Skip to content

Commit 7b9798e

Browse files
committed
enh: add S3 upload functionality for encrypted instance backups
1 parent fa4e45f commit 7b9798e

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

dcor_control/cli/backup.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import click
77

8-
from dcor_shared import get_ckan_config_option
8+
from dcor_shared import get_ckan_config_option, s3
99

1010
from ..backup import db_backup, delete_old_backups, gpg_encrypt
1111

@@ -34,11 +34,11 @@ def encrypted_instance_backup(key_id, skip_s3=False):
3434
now = time.strftime("%Y-%m-%d_%H-%M-%S")
3535

3636
# Get database backup
37-
click.secho("Creating database backup")
37+
click.secho("Creating database backup...")
3838
db_path = db_backup()
3939

4040
# Create a tar.bz2 file that contains the contents of /data and `dp_path`.
41-
click.secho("Creating instance backup")
41+
click.secho("Creating instance backup..")
4242
storage_path = pathlib.Path(get_ckan_config_option("ckan.storage_path"))
4343
droot = pathlib.Path("/backups/instance/")
4444
droot.mkdir(parents=True, exist_ok=True)
@@ -48,13 +48,23 @@ def encrypted_instance_backup(key_id, skip_s3=False):
4848
z.add(storage_path)
4949

5050
# create encrypted version
51-
click.secho("Encrypting instance backup")
51+
click.secho("Encrypting instance backup...")
5252
eroot = pathlib.Path("/backups/instance-encrypted/")
5353
eroot.mkdir(parents=True, exist_ok=True)
5454
eout = eroot / (dpath.name + ".gpg")
5555
gpg_encrypt(path_in=dpath, path_out=eout, key_id=key_id)
5656
click.secho(f"Created {eout}", bold=True)
5757

58+
if not skip_s3:
59+
click.secho("Uploading to S3...")
60+
bucket_prefix = get_ckan_config_option(
61+
"dcor_object_store.bucket_name").format(organization_id="")
62+
bucket_name = bucket_prefix + "00000_backup"
63+
s3.require_bucket(bucket_name)
64+
s3.upload_file(bucket_name=bucket_name,
65+
object_name=f"{now[:4]}/{now[5:7]}/{eout.name}",
66+
path=eout)
67+
5868
click.secho("Cleaning up...")
5969
delete_old_backups(
6070
backup_root=droot,

0 commit comments

Comments
 (0)