From 806bf76c09b48aeb253931eda18ede0d7ae4fd8e Mon Sep 17 00:00:00 2001 From: Raghupathi Date: Mon, 3 May 2021 14:17:25 +0530 Subject: [PATCH] Mongo backup (#2412) Mongodb backup role --- ansible/mongodb-backup.yml | 9 ++ .../roles/mongodb-backup/defaults/main.yml | 2 + ansible/roles/mongodb-backup/meta/main.yml | 2 + ansible/roles/mongodb-backup/tasks/main.yml | 25 ++++ .../dev/jobs/Core/jobs/MongoBackup/config.xml | 130 ++++++++++++++++++ pipelines/backup/mongodb-backup/Jenkinsfile | 46 +++++++ 6 files changed, 214 insertions(+) create mode 100644 ansible/mongodb-backup.yml create mode 100644 ansible/roles/mongodb-backup/defaults/main.yml create mode 100644 ansible/roles/mongodb-backup/meta/main.yml create mode 100644 ansible/roles/mongodb-backup/tasks/main.yml create mode 100644 deploy/jenkins/jobs/OpsAdministration/jobs/dev/jobs/Core/jobs/MongoBackup/config.xml create mode 100644 pipelines/backup/mongodb-backup/Jenkinsfile diff --git a/ansible/mongodb-backup.yml b/ansible/mongodb-backup.yml new file mode 100644 index 0000000000..95ff1e7d61 --- /dev/null +++ b/ansible/mongodb-backup.yml @@ -0,0 +1,9 @@ +- hosts: "{{ host }}" + become: yes + vars_files: + - ['{{inventory_dir}}/secrets.yml'] + environment: + AZURE_STORAGE_ACCOUNT: "{{ sunbird_management_storage_account_name }}" + AZURE_STORAGE_KEY: "{{ sunbird_management_storage_account_key }}" + roles: + - mongodb-backup diff --git a/ansible/roles/mongodb-backup/defaults/main.yml b/ansible/roles/mongodb-backup/defaults/main.yml new file mode 100644 index 0000000000..82a51650a5 --- /dev/null +++ b/ansible/roles/mongodb-backup/defaults/main.yml @@ -0,0 +1,2 @@ +mongo_backup_dir: '/tmp/mongo-backup' +mongo_backup_azure_container_name: "{{ mongo_backup_azure_container_name }}" diff --git a/ansible/roles/mongodb-backup/meta/main.yml b/ansible/roles/mongodb-backup/meta/main.yml new file mode 100644 index 0000000000..a124d4f7cb --- /dev/null +++ b/ansible/roles/mongodb-backup/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - azure-cli diff --git a/ansible/roles/mongodb-backup/tasks/main.yml b/ansible/roles/mongodb-backup/tasks/main.yml new file mode 100644 index 0000000000..219ced55ea --- /dev/null +++ b/ansible/roles/mongodb-backup/tasks/main.yml @@ -0,0 +1,25 @@ +- name: Create the directory + file: path={{ mongo_backup_dir }} state=directory recurse=yes + +- set_fact: + mongo_backup_file_name: "mongo-backup-{{ ansible_hostname }}-{{ lookup('pipe', 'date +%Y-%m-%d') }}" + +- set_fact: + mongo_backup_file_path: "{{ mongo_backup_dir }}/{{ mongo_backup_file_name }}" + +- name: take dump + shell: "mongodump --out={{ mongo_backup_file_path }}" + +- name: Compress the backup file + shell: "tar -czf {{ mongo_backup_file_path }}.tar.gz {{ mongo_backup_file_path }}" + +- name: upload to azure + include_role: + name: artifacts-upload-azure + vars: + artifact: "{{ mongo_backup_file_name }}.tar.gz" + artifact_path: "{{ mongo_backup_file_path }}.tar.gz" + artifacts_container: "{{ mongo_backup_azure_container_name }}" + +- name: clean up backup dir after upload + file: path={{ mongo_backup_dir }} state=absent diff --git a/deploy/jenkins/jobs/OpsAdministration/jobs/dev/jobs/Core/jobs/MongoBackup/config.xml b/deploy/jenkins/jobs/OpsAdministration/jobs/dev/jobs/Core/jobs/MongoBackup/config.xml new file mode 100644 index 0000000000..9e3a16fcc3 --- /dev/null +++ b/deploy/jenkins/jobs/OpsAdministration/jobs/dev/jobs/Core/jobs/MongoBackup/config.xml @@ -0,0 +1,130 @@ + + + + false + + + + -1 + -1 + -1 + 5 + + + + + false + false + + + + + private_branch + + choice-parameter-189747244589805 + 1 + + false + + + + false + + + Mongodb + OpsAdministration/dev/Core/Mongodb + + + ET_FORMATTED_HTML + true + + + branch_or_tag + + choice-parameter-189747247305087 + 1 + + false + + + + false + + + Mongodb + OpsAdministration/dev/Core/Mongodb + + + ET_FORMATTED_HTML + true + + + host + Host group name of server for which we need backup + mongodb-backup + false + + + + + 0 + 0 + + false + project + false + + + + + + + 00 1 * * * + + + + + + + 2 + + + https://github.com/project-sunbird/sunbird-devops.git + + + + + ${branch_or_tag} + + + false + + + + true + false + + 0 + false + + + + pipelines/backup/mongodb-backup/Jenkinsfile + false + + + false + diff --git a/pipelines/backup/mongodb-backup/Jenkinsfile b/pipelines/backup/mongodb-backup/Jenkinsfile new file mode 100644 index 0000000000..4cfbaee250 --- /dev/null +++ b/pipelines/backup/mongodb-backup/Jenkinsfile @@ -0,0 +1,46 @@ +@Library('deploy-conf') _ +node() { + try { + String ANSI_GREEN = "\u001B[32m" + String ANSI_NORMAL = "\u001B[0m" + String ANSI_BOLD = "\u001B[1m" + String ANSI_RED = "\u001B[31m" + String ANSI_YELLOW = "\u001B[33m" + + stage('checkout Private repo') { + cleanWs() + checkout scm + } + + ansiColor('xterm') { + stage('deploy'){ + values = [:] + envDir = sh(returnStdout: true, script: "echo $JOB_NAME").split('/')[-3].trim() + module = sh(returnStdout: true, script: "echo $JOB_NAME").split('/')[-2].trim() + jobName = sh(returnStdout: true, script: "echo $JOB_NAME").split('/')[-1].trim() + currentWs = sh(returnStdout: true, script: 'pwd').trim() + ansiblePlaybook = "${currentWs}/ansible/mongodb-backup.yml" + ansibleExtraArgs = "--extra-vars \"host=${params.host}\" --vault-password-file /var/lib/jenkins/secrets/vault-pass" + values.put('currentWs', currentWs) + values.put('env', envDir) + values.put('module', module) + values.put('jobName', jobName) + values.put('ansiblePlaybook', ansiblePlaybook) + values.put('ansibleExtraArgs', ansibleExtraArgs) + println values + ansible_playbook_run(values) + currentBuild.result = "SUCCESS" + currentBuild.description = "Private: ${params.private_branch}" + } + } + } + catch (err) { + currentBuild.result = "FAILURE" + throw err + } + + finally { + slack_notify(currentBuild.result) + email_notify() + } +}