xtrabackup restore db script

Change-Id: If818ae4d83e088a0ec219b671bf4e8c7399bf8ca
diff --git a/xtrabackup-restore-mysql-db.groovy b/xtrabackup-restore-mysql-db.groovy
new file mode 100644
index 0000000..6c8b379
--- /dev/null
+++ b/xtrabackup-restore-mysql-db.groovy
@@ -0,0 +1,83 @@
+/**
+ * Update packages on given nodes
+ *
+ * Expected parameters:
+ *   SALT_MASTER_CREDENTIALS    Credentials to the Salt API.
+ *   SALT_MASTER_URL            Full Salt API address [http://10.10.10.1:8000].
+ *
+**/
+
+def common = new com.mirantis.mk.Common()
+def salt = new com.mirantis.mk.Salt()
+
+
+def saltMaster
+
+timestamps {
+    node() {
+
+        stage('Connect to Salt API') {
+            saltMaster = salt.connection(SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
+        }
+
+        stage('Start restore') {
+            // # actual upgrade
+
+            stage('Ask for manual confirmation') {
+                input message: "Are you sure you have the correct backups ready? Do you really want to continue to restore mysql db?"
+            }
+            // database restore section
+            try {
+                salt.runSaltProcessStep(saltMaster, 'I@galera:slave', 'service.stop', ['mysql'], null, true)
+            } catch (Exception er) {
+                common.warningMsg('Mysql service already stopped')
+            }
+            try {
+                salt.runSaltProcessStep(saltMaster, 'I@galera:master', 'service.stop', ['mysql'], null, true)
+            } catch (Exception er) {
+                common.warningMsg('Mysql service already stopped')
+            }
+            try {
+                salt.cmdRun(saltMaster, 'I@galera:slave', "rm /var/lib/mysql/ib_logfile*")
+            } catch (Exception er) {
+                common.warningMsg('Files are not present')
+            }
+            try {
+                salt.cmdRun(saltMaster, 'I@galera:master', "mkdir /root/mysql/mysql.bak")
+            } catch (Exception er) {
+                common.warningMsg('Directory already exists')
+            }
+            try {
+                salt.cmdRun(saltMaster, 'I@galera:master', "rm -rf /root/mysql/mysql.bak/*")
+            } catch (Exception er) {
+                common.warningMsg('Directory already empty')
+            }
+            try {
+                salt.cmdRun(saltMaster, 'I@galera:master', "mv /var/lib/mysql/* /root/mysql/mysql.bak")
+            } catch (Exception er) {
+                common.warningMsg('Files were already moved')
+            }
+            try {
+                salt.runSaltProcessStep(saltMaster, 'I@galera:master', 'file.remove', ["/var/lib/mysql/.galera_bootstrap"], null, true)
+            } catch (Exception er) {
+                common.warningMsg('File is not present')
+            }
+            salt.cmdRun(saltMaster, 'I@galera:master', "sed -i '/gcomm/c\\wsrep_cluster_address=\"gcomm://\"' /etc/mysql/my.cnf")
+            _pillar = salt.getPillar(saltMaster, "I@galera:master", 'xtrabackup:client:backup_dir')
+            backup_dir = _pillar['return'][0].values()[0]
+            if(backup_dir == null || backup_dir.isEmpty()) { backup_dir='/var/backups/mysql/xtrabackup' }
+            print(backup_dir)
+            salt.runSaltProcessStep(saltMaster, 'I@galera:master', 'file.remove', ["${backup_dir}/dbrestored"], null, true)
+            salt.cmdRun(saltMaster, 'I@xtrabackup:client', "su root -c 'salt-call state.sls xtrabackup'")
+            salt.runSaltProcessStep(saltMaster, 'I@galera:master', 'service.start', ['mysql'], null, true)
+            sleep(5)
+            salt.runSaltProcessStep(saltMaster, 'I@galera:slave', 'service.start', ['mysql'], null, true)
+            sleep(15)
+            salt.cmdRun(saltMaster, 'I@galera:master', "su root -c 'salt-call mysql.status | grep -A1 wsrep_cluster_size'")
+
+        }
+    }
+}
+
+
+