---
- name: Install boto
  pip:
    name: "{{ item }}"
    state: latest 
  with_items:
  - boto3
  - botocore

- name: Get Timestamp
  shell: date +'%Y%m%d%H%M%S'
  register: timestamp

- name: Backup postgresql database
  shell: pg_dump {{ backup_db_name }} -U {{ backup_username }} -Fc | openssl enc -aes-256-cbc -a -pass pass:{{ backup_enc_password }} | gzip >{{ backup_tmp }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz

- name: Get Prefix
  shell: date +'data/%Y/%m/%d'
  register: prefix

- name: "Put the database backup to S3"
  aws_s3:
    aws_access_key: "{{ backup_aws_access_key }}"
    aws_secret_key: "{{ backup_aws_secret_key }}"
    bucket: "{{ backup_s3_bucket }}"
    object: "{{ prefix.stdout_lines[0] }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz"
    src: "/{{ backup_tmp }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz"
    mode: put


---
- name: Install boto
  pip:
    name: "{{ item }}"
    state: latest 
  with_items:
  - boto3
  - botocore

- name: Get Timestamp
  shell: date +'%Y%m%d%H%M%S'
  register: timestamp

- name: Backup postgresql database
  shell: pg_dump {{ backup_db_name }} -U {{ backup_username }} -Fc | openssl enc -aes-256-cbc -a -pass pass:{{ backup_enc_password }} | gzip >{{ backup_tmp }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz

- name: Get Prefix
  shell: date +'data/%Y/%m/%d'
  register: prefix

- name: Put the database backup to S3
  aws_s3:
    aws_access_key: "{{ backup_aws_access_key }}"
    aws_secret_key: "{{ backup_aws_secret_key }}"
    bucket: "{{ backup_s3_bucket }}"
    object: "{{ prefix.stdout_lines[0] }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz"
    src: "/{{ backup_tmp }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz"
    mode: put

- name: Remove the dump file
  file:
    path: '{{ backup_tmp }}/{{ backup_db_name }}_{{ timestamp.stdout_lines[0] }}_dump.gz'
    state: absent

Remove the file after successful backup
Remove the file after successful backup

Signed-off-by: Chris Houseknecht <5fe8b270dec7aa3e76c2b5550ed9476454e20946@redhat.com>
