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

320 backups billing #323

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions playbooks/roles/backups/files/billing_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

# This script automates the Billing backup process, including:
# - Extracting database information using `mysqldump`.
# - Compressing the backup file.
# - Uploading the backup to Object Storage using `oci` CLI.
#
# Logs are written to /opt/oci-hpc/logs/backups/backup_billing.log
#
# Dependencies:
# - bash
# - mysqldump
# - gzip
# - oci CLI

LOG_FILE="/opt/oci-hpc/logs/backups/backup_billing.log"
mkdir -p "$(dirname "$LOG_FILE")"
exec >> "$LOG_FILE" 2>&1

timestamp=$(date '+%Y_%m_%d')
backup_name="billing_backup_${timestamp}.sql"
db_backup_path="/tmp/$backup_name"
mysql_credentials="/home/ubuntu/.billing.cnf"

echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO - Starting Billing database backup process."

dump_cmd="/usr/bin/mysqldump --defaults-extra-file=${mysql_credentials} --single-transaction -B billing --result-file=${db_backup_path}"
gzip_db_cmd="gzip -f ${db_backup_path}"
oci_db_cmd="oci os object put --force --bucket-name backups --name '/billing/${backup_name}.gz' --file ${db_backup_path}.gz"
remove_db_cmd="rm ${db_backup_path}.gz"

run_command() {
local cmd="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO - Running command: $cmd"
if ! output=$(eval "$cmd" 2>&1); then
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR - Command '$cmd' failed with error: $output"
exit 1
fi
}

# Backup billing database
run_command "$dump_cmd"
run_command "$gzip_db_cmd"
run_command "$oci_db_cmd"
run_command "$remove_db_cmd"

echo "$(date '+%Y-%m-%d %H:%M:%S') - INFO - Billing database backup process completed successfully."
20 changes: 20 additions & 0 deletions playbooks/roles/backups/tasks/billing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
- name: Copy scripts
become: true
copy:
src: '{{ item }}'
dest: '/opt/oci-hpc/scripts/{{ item }}'
force: no
owner: '{{ ansible_user }}'
group: '{{ ansible_user }}'
mode: 0770
with_items:
- billing_backup.sh

- name: Create crontab entry to backup billing
cron:
name: Backup billing
minute: "0"
hour: "0"
user: '{{ ansible_user }}'
job: "/opt/oci-hpc/scripts/billing_backup.sh"
disabled: true
1 change: 1 addition & 0 deletions playbooks/roles/backups/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
group: '{{ ansible_user }}'

- include_tasks: ldap.yml
- include_tasks: billing.yml