[DO-1037] add_job_for_lic_backup (!35)

Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/35
Reviewed-by: Denis Patrakeev <denis.patrakeev@avroid.tech>
Co-authored-by: Rustam Tagaev <rustam.tagaev@avroid.tech>
Co-committed-by: Rustam Tagaev <rustam.tagaev@avroid.tech>
This commit is contained in:
Rustam Tagaev
2024-09-26 11:56:57 +03:00
committed by Denis Patrakeev
parent 1d72f7743b
commit 74ac9ea929
2 changed files with 89 additions and 0 deletions

View File

@@ -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 * * *')
}
}
}
}
}

View File

@@ -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}
"""
}
}
}