From 74ac9ea92934f4c0f2a2b2381662f0e75b1af6b4 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Thu, 26 Sep 2024 11:56:57 +0300 Subject: [PATCH] [DO-1037] add_job_for_lic_backup (!35) Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/35 Reviewed-by: Denis Patrakeev Co-authored-by: Rustam Tagaev Co-committed-by: Rustam Tagaev --- .../License-server/license_test_backup.groovy | 28 +++++++++ .../License-server/license-test-backup.groovy | 61 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 jobs-dsl/jobs/License-server/license_test_backup.groovy create mode 100644 pipelines/License-server/license-test-backup.groovy diff --git a/jobs-dsl/jobs/License-server/license_test_backup.groovy b/jobs-dsl/jobs/License-server/license_test_backup.groovy new file mode 100644 index 0000000..e9e3213 --- /dev/null +++ b/jobs-dsl/jobs/License-server/license_test_backup.groovy @@ -0,0 +1,28 @@ +pipelineJob('License-server/license-test-backup') { + description("Тест бэкапов") + definition { + cpsScm { + scm { + git { + remote { + url("${JENKINS_GIT_REPOSITORY_URL}/DevOps/jenkins-pipelines.git") + credentials("${JENKINS_GIT_CREDENTIALS_HTTP}") + } + branch('master') + } + } + scriptPath('pipelines/License-server/license-test-backup.groovy') + } + } + + properties { + disableConcurrentBuilds() + pipelineTriggers { + triggers { + cron { + spec('H 6 * * *') + } + } + } + } +} diff --git a/pipelines/License-server/license-test-backup.groovy b/pipelines/License-server/license-test-backup.groovy new file mode 100644 index 0000000..e6e0614 --- /dev/null +++ b/pipelines/License-server/license-test-backup.groovy @@ -0,0 +1,61 @@ +@Library('shared-lib') _ + +import java.text.SimpleDateFormat + +properties([ + buildDiscarder(logRotator(artifactNumToKeepStr: '10', + numToKeepStr: '10')), + disableConcurrentBuilds(), + pipelineTriggers([ + cron('H 6 * * *') + ]) + +]) + +Object date = new Date() +Object todayDate = new SimpleDateFormat("yyyy-MM-dd") + +String licBackupServer = 'h-license-manager-backup-server.avroid.cloud' + +Map configuration = [ + vaultUrl: 'https://vault.avroid.tech', + vaultCredentialId: 'vault-role', + engineVersion: 2 +] + +List sshPassword = [ + [path: 'team-devops/accounts/ldap/service_accounts/svc_jenkins', engineVersion: 2, secretValues: + [ + [vaultKey: 'password'], + ] + ] +] + +todayDate = todayDate.format(date) + +String cmdUnarchiveBackup = "bzip2 -d license_server-${todayDate}.sql.bz2" +String cmdClearDockerTmp = "docker exec postgresql rm -rf /tmp/*" +String cmdCopyRoles = "docker cp roles-${todayDate}.sql postgresql:/tmp" +String cmdCopyDB = "docker cp license_server-${todayDate}.sql postgresql:/tmp" + +String cmdRestoreRoles = "docker exec postgresql psql -U postgres -p 5432 -d postgres -f /tmp/roles-${todayDate}.sql" +String cmdDropDB = "docker exec postgresql dropdb -p 5432 -U postgres -f --if-exists license_server" +String cmdRestoreDB = "docker exec postgresql pg_restore -Fc -U postgres -p 5432 -d postgres -C -d postgres -v /tmp/license_server-${todayDate}.sql" +String cmdClearTmp = "rm -rf /home/jenkins/*" + +node('slave01') { + stage('Restore backup') { + withVault([configuration: configuration, vaultSecrets: sshPassword]) { + sh """ + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdUnarchiveBackup} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdClearDockerTmp} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdCopyRoles} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdCopyDB} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdRestoreRoles} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdDropDB} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdRestoreDB} + sshpass -p $password ssh -o StrictHostKeychecking=no jenkins@${licBackupServer} ${cmdClearTmp} + """ + } + } +}