-
GitHubdp 백업파일을 저장할 repository 생성(e.g.
wicksome/jenkins-backup
) -
백업 파일 사이즈에 제한될 수 있으므로
.gitattributes
추가(commit/push)$ git lfs track "*.tar.gz" $ ls .gitattributes
-
SSH 키 생성
$ ssh-keygen -t rsa -f jenkins_backup $ ls jenkins_backup jenkins_backup.pub
-
공개키(
jenkins_backup.pub
)는 repo에 등록Settings → Deploy keys → Add deploy key
-
비공개키(
jenkins_backup
)는 jenkins에 등록Add Credentials → Kind:
SSH Username with private key
→ ID:jenkins-backup
-
jenkins-backup.sh을 개인의 backup 저장소에 이동
https://github.com/sue445/jenkins-backup-script.git 에서 가져와서 사용해도 되나 두번 clone 받는 비용을 줄이기 위해 선 이동 작업 진행
-
Jenkinsfile을 활용하여 백업 작업 등록
def BACKUP_DATETIME = new Date().format('yyyyMMddHHmmss', TimeZone.getTimeZone('Asia/Seoul')) pipeline { agent { label 'master' } triggers { cron('TZ=Asia/Seoul\n0 3 * * *') } environment { JENKINS_HOST = '' } stages { stage('Init') { steps { script { JENKINS_HOST = sh(script: "echo ${env.BUILD_URL} | awk -F[/:] '{print \$4}'", returnStdout: true).trim() } } } stage('Checkout') { steps { sh "git lfs install" checkout([ $class : 'GitSCM', branches : [[name: 'refs/remotes/origin/master']], userRemoteConfigs: [[url: 'git@github.com:wicksome/jenkins-backup.git', credentialsId: 'jenkins-backup']], extensions : [ [$class: 'GitLFSPull'], [$class: 'CheckoutOption', timeout: 20], [$class : 'CloneOption', depth : 0, noTags : false, reference: '/other/optional/local/reference/clone', shallow : false, timeout : 120] ] ]) } } stage('Backup') { when { expression { fileExists('./jenkins-backup.sh') } } steps { sh "./jenkins-backup.sh ${env.JENKINS_HOME} ${JENKINS_HOST}_${BACKUP_DATETIME}.tar.gz" } } stage('Upload') { steps { // setting git config sh ''' git config user.name 'JENKINS\' git config user.email 'noreplay@nowhere' ''' // rotate backup files sh "find . -type f -name '${JENKINS_HOST}_*' | grep -v \"${BACKUP_DATETIME}\" | xargs rm -f" // git commit sh """ git add -A git ls-files --deleted | xargs git add git commit -m "Backup the jenkins home directory" """ // git push sshagent(['jenkins-backup']) { sh(""" #!/usr/bin/env bash set +x export GIT_SSH_COMMAND="ssh -oStrictHostKeyChecking=no" git remote set-head origin -a git pull origin master git push origin HEAD:master """) } } } } post { failure { script { def mailRecipients = 'opid911@gmail.com' def jobName = currentBuild.fullDisplayName emailext body: '''${SCRIPT, template="groovy-html.template"}''', mimeType: 'text/html', subject: "${jobName} 실패 - ${JENKINS_HOST}", to: "${mailRecipients}", replyTo: "${mailRecipients}", recipientProviders: [[$class: 'CulpritsRecipientProvider']] } } } }
References
-
SSH Agent Plugin -
sshagent
step -
Git Large File Storage -
.gitattributes
설정this exceeds GitHub Enterprise's file size limit of 100.00 MB