From 167cdf056ca79e236460d3021c3d91a457731c12 Mon Sep 17 00:00:00 2001 From: Andriy Novykov Date: Fri, 13 Dec 2024 14:09:36 -0500 Subject: [PATCH] Added accounting backups --- .../roles/backups/files/accounting_backup.sh | 47 +++++++++++++++++++ playbooks/roles/backups/tasks/accounting.yml | 20 ++++++++ playbooks/roles/backups/tasks/main.yml | 1 + 3 files changed, 68 insertions(+) create mode 100644 playbooks/roles/backups/files/accounting_backup.sh create mode 100644 playbooks/roles/backups/tasks/accounting.yml diff --git a/playbooks/roles/backups/files/accounting_backup.sh b/playbooks/roles/backups/files/accounting_backup.sh new file mode 100644 index 0000000..08badbb --- /dev/null +++ b/playbooks/roles/backups/files/accounting_backup.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash + +# This script automates the accounting 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/accounting_backup.log +# +# Dependencies: +# - bash +# - mysqldump +# - gzip +# - oci CLI + +LOG_FILE="/opt/oci-hpc/logs/backups/backup_accounting.log" +mkdir -p "$(dirname "$LOG_FILE")" +exec >> "$LOG_FILE" 2>&1 + +timestamp=$(date '+%Y_%m_%d') +backup_name="accounting_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 accounting 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 '/accounting/${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 accounting 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 - Completed accounting database backup process successfully." \ No newline at end of file diff --git a/playbooks/roles/backups/tasks/accounting.yml b/playbooks/roles/backups/tasks/accounting.yml new file mode 100644 index 0000000..784de73 --- /dev/null +++ b/playbooks/roles/backups/tasks/accounting.yml @@ -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: + - accounting_backup.sh + +- name: Create crontab entry to backup accounting + cron: + name: Backup accounting + minute: "0" + hour: "0" + user: '{{ ansible_user }}' + job: "/opt/oci-hpc/scripts/accounting_backup.sh" + disabled: true \ No newline at end of file diff --git a/playbooks/roles/backups/tasks/main.yml b/playbooks/roles/backups/tasks/main.yml index 4d40f74..6960f6d 100644 --- a/playbooks/roles/backups/tasks/main.yml +++ b/playbooks/roles/backups/tasks/main.yml @@ -7,3 +7,4 @@ group: '{{ ansible_user }}' - include_tasks: ldap.yml +- include_tasks: accounting.yml \ No newline at end of file