Skip to content

Commit 161415f

Browse files
committed
enh: quantify instance backup size
1 parent d5aee75 commit 161415f

3 files changed

Lines changed: 59 additions & 26 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
1.16.1
2+
- enh: quantify instance backup size
13
1.16.0
24
- BREAKING: file name scheme for creating backups changed
35
- BREAKING: remove `encrypted_database_backup` CLI script

dcor_control/cli/backup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def encrypted_instance_backup(key_id, skip_s3=False):
5959
click.secho("Uploading to S3...")
6060
bucket_prefix = get_ckan_config_option(
6161
"dcor_object_store.bucket_name").format(organization_id="")
62-
bucket_name = bucket_prefix + "00000-backup"
62+
bucket_name = bucket_prefix + "000000000-backup"
6363
s3.require_bucket(bucket_name)
6464
s3.upload_file(bucket_name=bucket_name,
6565
object_name=f"{now[:4]}/{now[5:7]}/{eout.name}",

dcor_control/cli/status.py

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,61 @@ def status():
4949
num_resources = 0
5050
size_resources = 0
5151
size_other = 0
52-
s3_client, s3_session, s3_resource = s3.get_s3()
53-
buckets = [b["Name"] for b in s3_client.list_buckets()["Buckets"]]
54-
for bucket in buckets:
55-
kwargs = {"Bucket": bucket,
56-
"MaxKeys": 500
57-
}
58-
while True:
59-
resp = s3_client.list_objects_v2(**kwargs)
60-
61-
for obj in resp.get("Contents", []):
62-
if obj["Key"].startswith("resource/"):
63-
num_resources += 1
64-
size_resources += obj["Size"]
65-
else:
66-
size_other += obj["Size"]
67-
68-
if not resp.get("IsTruncated"):
69-
break
70-
else:
71-
kwargs["ContinuationToken"] = resp.get(
72-
"NextContinuationToken")
73-
74-
click.echo(f"S3 buckets: {len(buckets)}")
52+
num_buckets = 0
53+
for bucket_name in s3.iter_buckets():
54+
num_buckets += 1
55+
bi = get_bucket_info(bucket_name)
56+
num_resources += bi["num_resources"]
57+
size_resources += bi["size_resources"]
58+
size_other += bi["size_other"]
59+
60+
click.echo(f"S3 buckets: {num_buckets}")
7561
click.echo(f"S3 resources number: {num_resources}")
76-
click.echo(f"S3 resources size: {size_resources/1024**3:.0f} GB")
62+
click.echo(f"S3 resources size: {size_resources/1024**3:.0f} GiB")
7763
click.echo(f"S3 total size: "
78-
f"{(size_other + size_resources) / 1024**3:.0f} GB")
64+
f"{(size_other + size_resources) / 1024**3:.0f} GiB")
65+
66+
# Backup bucket
67+
try:
68+
bbi = get_bucket_info("000000000-backup")
69+
except BaseException:
70+
click.echo("Instance backup bucket does not exist.")
71+
else:
72+
click.echo(f"S3 instance backup number: {bbi['num_other']}")
73+
click.echo(f"S3 instance backup size: "
74+
f"{bbi['size_other']/1024**3:.0f} GiB")
75+
76+
77+
def get_bucket_info(bucket_name):
78+
s3_client, s3_session, s3_resource = s3.get_s3()
79+
num_resources = 0
80+
num_other = 0
81+
size_resources = 0
82+
size_other = 0
83+
84+
kwargs = {"Bucket": bucket_name,
85+
"MaxKeys": 500
86+
}
87+
while True:
88+
resp = s3_client.list_objects_v2(**kwargs)
89+
90+
for obj in resp.get("Contents", []):
91+
if obj["Key"].startswith("resource/"):
92+
num_resources += 1
93+
size_resources += obj["Size"]
94+
else:
95+
num_other += 1
96+
size_other += obj["Size"]
97+
98+
if not resp.get("IsTruncated"):
99+
break
100+
else:
101+
kwargs["ContinuationToken"] = resp.get(
102+
"NextContinuationToken")
103+
104+
return {
105+
"num_resources": num_resources,
106+
"num_other": num_other,
107+
"size_resources": size_resources,
108+
"size_other": size_other,
109+
}

0 commit comments

Comments
 (0)