Skip to content

Commit 5b0b5e0

Browse files
committed
add checksum validation
1 parent a569865 commit 5b0b5e0

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

backup_and_restore_database/backup_database/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ DB_NAME=
44
DB_USER=
55
DB_PASSWORD=
66

7+
AWS_ENDPOINT_URL=
78
AWS_ACCESS_KEY_ID=
89
AWS_SECRET_ACCESS_KEY=
910
AWS_DEFAULT_REGION=ca-central-1

backup_and_restore_database/backup_database/Dockerfile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@ RUN apt-get update && \
1919
apt-get install -y \
2020
libldap-2.5-0 \
2121
libsasl2-2 \
22+
curl unzip groff less \
2223
&& \
23-
pip install --no-cache-dir awscli && \
2424
rm -rf /var/lib/apt/lists/* && \
2525
apt-get clean
2626

27+
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
28+
unzip awscliv2.zip && \
29+
./aws/install && \
30+
rm -rf awscliv2.zip aws
31+
2732
WORKDIR /usr/src/app
2833

2934
ENV PYTHONDONTWRITEBYTECODE=1 \
3035
PYTHONUNBUFFERED=1 \
3136
AWS_DEFAULT_REGION=ca-central-1 \
32-
AWS_CLI_AUTO_PROMPT=off
33-
37+
AWS_RESPONSE_CHECKSUM_VALIDATION=when_required \
38+
AWS_REQUEST_CHECKSUM_CALCULATION=when_required
3439
# Create and switch to non-root user
3540
RUN useradd -m appuser && \
3641
chown -R appuser:appuser /usr/src/app

backup_and_restore_database/backup_database/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This directory contains a script and Docker setup to back up a PostgreSQL databa
2828
`docker build -t backup-to-s3 `
2929

3030
2. Run the container:
31-
`docker run --env-file .env backup-to-s3`
31+
`docker run --rm --env-file .env backup-to-s3`
3232

3333
## How It Works
3434
1. The script uses pg_dump to create a PostgreSQL database dump in a custom binary format (-Fc).

backup_and_restore_database/backup_database/backup_and_upload_to_s3.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
DB_NAME = os.environ["DB_NAME"]
99
DB_USER = os.environ["DB_USER"]
1010
DB_PASSWORD = os.environ["DB_PASSWORD"]
11+
ENDPOINT_URL = os.environ["AWS_ENDPOINT_URL"]
1112
S3_PATH = f"s3://{os.getenv('BUCKET_NAME')}/{date.today()}-{DB_NAME}.dump"
12-
1313
os.environ["PGPASSWORD"] = DB_PASSWORD
14-
1514
pg_dump_cmd = [
1615
"pg_dump",
1716
"-h", DB_HOST,
@@ -22,24 +21,29 @@
2221
"-v"
2322
]
2423

25-
aws_cp_cmd = ["aws", "s3", "cp", "-", S3_PATH, "--only-show-errors"]
24+
aws_cp_cmd = [
25+
"aws", "s3", "cp", "-", S3_PATH,
26+
"--endpoint-url", ENDPOINT_URL,
27+
"--debug",
28+
"--expected-size", "60000000000"
29+
]
2630

2731
print("Streaming dump into S3 bucket...")
2832

29-
dump_proc = subprocess.Popen(pg_dump_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
33+
dump_proc = subprocess.Popen(pg_dump_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
3034

31-
aws_proc = subprocess.Popen(aws_cp_cmd, stdin=dump_proc.stdout, stderr=subprocess.PIPE, text=True)
35+
aws_proc = subprocess.Popen(aws_cp_cmd, stdin=dump_proc.stdout, stderr=subprocess.PIPE)
3236

3337
dump_proc.stdout.close()
3438

3539
for line in dump_proc.stderr:
36-
print(f"{datetime.now()} {line.strip()}")
40+
print(f"{datetime.now()} {line.decode().strip()}")
3741

3842
# Wait for both to finish and get final stderr from aws
3943
_, aws_stderr = aws_proc.communicate()
4044

4145
if aws_proc.returncode != 0:
4246
print("[aws s3 cp] ERROR:")
43-
print(aws_stderr)
47+
print(aws_stderr.decode())
4448
if dump_proc.wait() != 0:
4549
print("pg_dump exited with a non-zero status.")

0 commit comments

Comments
 (0)