Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): daily backup for datawarehouse bdd #315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ x-airflow-common:
AIRFLOW_VAR_MES_AIDES_AIRTABLE_KEY: ${AIRFLOW_VAR_MES_AIDES_AIRTABLE_KEY}
AIRFLOW_VAR_SOLIGUIDE_API_TOKEN: ${AIRFLOW_VAR_SOLIGUIDE_API_TOKEN}
AIRFLOW_VAR_TWOCAPTCHA_API_KEY: ${AIRFLOW_VAR_TWOCAPTCHA_API_KEY}
AIRFLOW_VAR_DATAWAREHOUSE_DI_HOST: target-db
AIRFLOW_VAR_DATAWAREHOUSE_DI_USERNAME: data-inclusion
AIRFLOW_VAR_DATAWAREHOUSE_DI_DATABASE: data-inclusion
AIRFLOW_VAR_DATAWAREHOUSE_DI_PASSWORD: data-inclusion
AIRFLOW_VAR_DATAWAREHOUSE_BUCKET_NAME: data-inclusion-lake

volumes:
- ./pipeline/dbt:/opt/airflow/dbt
Expand Down
54 changes: 54 additions & 0 deletions pipeline/dags/backup_datawarehouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from datetime import timedelta

from airflow import DAG
from airflow.models import Variable
from airflow.operators import empty
from airflow.operators.bash import BashOperator
from airflow.providers.amazon.aws.transfers.local_to_s3 import (
LocalFilesystemToS3Operator,
)
from airflow.utils.dates import days_ago

default_args = {
"owner": "airflow",
"depends_on_past": False,
"email_on_failure": False,
"email_on_retry": False,
"retries": 1,
"retry_delay": timedelta(seconds=5),
}

with DAG(
"backup_datawarehouse_to_s3",
default_args=default_args,
description="Backup PostgreSQL datawarehouse and upload to S3",
schedule_interval="0 2 * * *",
start_date=days_ago(1),
catchup=False,
):
start = empty.EmptyOperator(task_id="start")
end = empty.EmptyOperator(task_id="end")
backup_postgres = BashOperator(
task_id="backup_postgres",
bash_command=f"""
pg_dump -h {Variable.get('DATAWAREHOUSE_DI_HOST')} \
-U {Variable.get('DATAWAREHOUSE_DI_USER')} \
-d {Variable.get('DATAWAREHOUSE_DI_DATABASE')} -F c -f /tmp/backup.dump
""",
env={"PGPASSWORD": f"{Variable.get('DATAWAREHOUSE_DI_PASSWORD')}"},
)
logical_date = "{{ ds }}"
upload_to_s3 = LocalFilesystemToS3Operator(
task_id="upload_to_s3",
filename="/tmp/backup.dump",
dest_key=f"S3://{Variable.get('DATAWAREHOUSE_BUCKET_NAME')}/data/backups/datawarehouse/backup_{logical_date}.dump",
aws_conn_id="s3",
replace=True,
)

clean_up = BashOperator(
task_id="cleanup",
bash_command="rm /tmp/backup.dump",
)

(start >> backup_postgres >> upload_to_s3 >> clean_up >> end)
Loading