Merge "Add backup job"
diff --git a/jobs/backups/jenkins-rsync.excludes b/jobs/backups/jenkins-rsync.excludes
new file mode 100644
index 0000000..c62e470
--- /dev/null
+++ b/jobs/backups/jenkins-rsync.excludes
@@ -0,0 +1,16 @@
+.cache/
+.config/
+.devops/
+.jenkins/
+.local/
+.groovy/
+.packer.d/
+.pyenv/
+caches/
+config-history/
+jenkins-backups/
+jobs/*/builds/
+plugins/
+tools/
+war/
+workspace/
diff --git a/jobs/backups/sre-jenkins-backup.sh b/jobs/backups/sre-jenkins-backup.sh
index c63c877..48f9914 100755
--- a/jobs/backups/sre-jenkins-backup.sh
+++ b/jobs/backups/sre-jenkins-backup.sh
@@ -1,19 +1,12 @@
 #!/bin/bash -ex
-set -e
+set -ex
 cd "$(dirname "$0")"
-DATE=$(date +%F)
-SHOST=sre-ci.scc.mirantis.net
-SSH_CMD="ssh $SHOST"
-BACKUP_MAIN_DIR="${HOME}/sre/jenkins-backup"
-BACKUP_MAIN_REMOTE_DIR="/home/$USER/jenkins"
-echo "Prepare for backup"
-$SSH_CMD sudo rm -rf ${BACKUP_MAIN_REMOTE_DIR}/backups/$DATE
-$SSH_CMD rm -f ${BACKUP_MAIN_REMOTE_DIR}/${DATE}.tar.gz
-$SSH_CMD mkdir -p ${BACKUP_MAIN_REMOTE_DIR}/backups/$DATE
-scp ./jenkins-rsync.includes $SHOST:${BACKUP_MAIN_REMOTE_DIR}/jenkins-rsync.includes
-$SSH_CMD sudo rsync -avz --include-from=${BACKUP_MAIN_REMOTE_DIR}/jenkins-rsync.includes --exclude=\'*\' /var/jenkins_home/ ${BACKUP_MAIN_REMOTE_DIR}/backups/$DATE/
-$SSH_CMD sudo chown -R sgalkin:sgalkin ${BACKUP_MAIN_REMOTE_DIR}/backups
-$SSH_CMD tar czvf ${BACKUP_MAIN_REMOTE_DIR}/backups/${DATE}.tar.gz ${BACKUP_MAIN_REMOTE_DIR}/backups/$DATE
-$SSH_CMD rm -rf ${BACKUP_MAIN_REMOTE_DIR}/backups/$DATE
-mkdir -p ${BACKUP_MAIN_DIR}/backups
-scp $SHOST:${BACKUP_MAIN_REMOTE_DIR}/backups/${DATE}.tar.gz ${BACKUP_MAIN_DIR}/backups/
+DATE=$(date +%F-%H%M%S)
+BACKUP_MAIN_DIR="${JENKINS_HOME}/jenkins-backups/"
+STORAGE_DIR="${WORKSPACE:?}/storage/"
+mkdir -p "${BACKUP_MAIN_DIR:?}/${DATE}"
+rm -f "${BACKUP_MAIN_DIR}/${DATE}.tar.gz"
+rsync --exclude-from="${WORKSPACE}/jobs/backups/jenkins-rsync.excludes" -avzq "${JENKINS_HOME}" "${BACKUP_MAIN_DIR:?}/$DATE" --info=progress2
+mv "${STORAGE_DIR}/plugin_list.txt" "${BACKUP_MAIN_DIR}/${DATE}"
+tar -czf "${STORAGE_DIR}/sre-jenkins-backup-${DATE}.tar.gz" -C "${BACKUP_MAIN_DIR}" "${DATE}"
+
diff --git a/jobs/pipelines/self-backup-jenkins-files.groovy b/jobs/pipelines/self-backup-jenkins-files.groovy
new file mode 100644
index 0000000..ca13f47
--- /dev/null
+++ b/jobs/pipelines/self-backup-jenkins-files.groovy
@@ -0,0 +1,49 @@
+
+@NonCPS
+def getPluginList(){
+  def pluginList = new ArrayList(Jenkins.instance.pluginManager.plugins)
+  def pluginListText = ""
+  pluginList.each{
+    plugin ->
+      pluginListText += """${plugin.getShortName()}.hpi\n"""
+  }
+  return pluginListText
+}
+
+node('sre-team-infra') {
+  timestamps() {
+    ansiColor('xterm') {
+      stage('Checkout') {
+        checkout scm
+      }
+      storageDir = "${WORKSPACE}/storage"
+      stage('Prepare storage dir'){
+        sh "rm -rf ${storageDir} && mkdir -p ${storageDir}"
+      }
+      stage('Generate plugin urls'){
+        pluginList = getPluginList()
+        writeFile file: "${storageDir}/plugin_list.txt", text: "${pluginList}"
+      }
+      stage('Generate backup archive'){
+        sh 'bash jobs/backups/sre-jenkins-backup.sh'
+      }
+      stage('Upload backups to remote hosts'){
+        remoteHosts = ['172.19.119.254', 'srv02-srt.srt.mirantis.net', 'srv08-srt.infra.mirantis.net']
+        errCount = 0
+        sshagent(credentials: ['maintenance-team-ssh']) {
+          remoteHosts.each{ host ->
+            try {
+              sh "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null jenkins@${host} mkdir -p /home/jenkins/sre-jenkins-backups"
+              sh "rsync -avz ${storageDir}/*.tar.gz -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' jenkins@${host}:/home/jenkins/sre-jenkins-backups/"
+            } catch (Exception e) {
+                errCount+=1
+            }
+            if (remoteHosts.size() == errCount) {
+             throw Exception('All remote hosts are not reachable!')
+            }
+          }
+        }
+      }
+    }
+  }
+}
diff --git a/jobs/project.yaml b/jobs/project.yaml
index fcef52f..31ca31a 100644
--- a/jobs/project.yaml
+++ b/jobs/project.yaml
@@ -54,6 +54,8 @@
       # - mark-reports - can't moved to JJB, a lot of parameters is not supported like Inject environment variables
       # - diff_params_reclass_system - can't moved to JJB, unsupported parameters
       # - diff_params_reclass_system - can't moved to JJB, unsupported parameters
+      - self-backup-jenkins-files:
+          trigger_time: H H * * 0
       - self-deploy-jobs
       - self-deploy-virtualenvs
       - cleanup-jenkins-agents
diff --git a/jobs/templates/self-backup-jenkins-files.yaml b/jobs/templates/self-backup-jenkins-files.yaml
new file mode 100644
index 0000000..543eed2
--- /dev/null
+++ b/jobs/templates/self-backup-jenkins-files.yaml
@@ -0,0 +1,26 @@
+---
+- job-template:
+    project-type: pipeline
+    concurrent: false
+    disabled: false
+    description: '{job-description}'
+    name: self-backup-jenkins-files
+    triggers:
+      - timed: '{obj:trigger_time}'
+    parameters:
+    - string:
+        default: 'master'
+        description: 'tcp-qa review refspec'
+        name: TCP_QA_REFS
+        trim: 'true'
+    pipeline-scm:
+      lightweight-checkout: false
+      scm:
+      - git:
+          branches:
+          - FETCH_HEAD
+          refspec: '${{TCP_QA_REFS}}'
+          url: https://gerrit.mcp.mirantis.com/mcp/tcp-qa
+      script-path: jobs/pipelines/self-backup-jenkins-files.groovy
+    logrotate:
+      daysToKeep: 365