restore db

Change-Id: I3ad196e8d558aec2a33b0bbfdf304dedf34c7ff8
diff --git a/README.rst b/README.rst
index b4207ca..3f603ff 100644
--- a/README.rst
+++ b/README.rst
@@ -37,26 +37,6 @@
               host: 'localhost'
               rights: 'all privileges'
 
-Database with initial data
-
-.. code-block:: yaml
-
-    mysql:
-      server:
-        enabled: true
-        database:
-          datatabese_with_init_data:
-            encoding: 'utf8'
-            users:
-            - name: 'username'
-              password: 'password'
-              host: 'localhost'
-              rights: 'all privileges'
-            initial_data:
-              engine: backupninja
-              source: backup.host
-              host: original-host-name
-              database: original-database-name
 
 MySQL replication master with SSL
 
@@ -178,6 +158,42 @@
 MySQL client
 ------------
 
+Database with initial data (Restore DB)
+
+.. code-block:: yaml
+
+    mysql:
+      client:
+        server:
+          database:
+            admin:
+              host: localhost
+              port: 3306
+              user: ${_param:mysql_admin_user}
+              password: ${_param:mysql_admin_password}
+              encoding: utf8
+            database:
+              neutron_upgrade:
+                encoding: utf8
+                users:
+                - name: neutron
+                  password: ${_param:mysql_neutron_password}
+                  host: '%'
+                  rights: all
+                - name: neutron
+                  password: ${_param:mysql_neutron_password}
+                  host: ${_param:single_address}
+                  rights: all
+                initial_data:
+                  engine: backupninja
+                  source: ${_param:backupninja_backup_host}
+                  host: ${linux:network:fqdn}
+                  database: neutron
+
+.. note:: This client role needs to be put directly on dbs node. 
+          The provided setup restores db named neutron_upgrade with data from db called neutron.
+
+
 Database management on remote MySQL server
 
 .. code-block:: yaml
@@ -202,6 +218,7 @@
                   host: 'localhost'
                   rights: 'all privileges'
 
+
 User management on remote MySQL server
 
 .. code-block:: yaml
diff --git a/metadata/service/client/single.yml b/metadata/service/client/single.yml
new file mode 100644
index 0000000..bacb80e
--- /dev/null
+++ b/metadata/service/client/single.yml
@@ -0,0 +1,8 @@
+classes:
+- service.mysql.support
+applications:
+- mysql
+parameters:
+  mysql:
+    client:
+      enabled: true
\ No newline at end of file
diff --git a/mysql/client/server.sls b/mysql/client/server.sls
index f0e9dd0..6687d5f 100644
--- a/mysql/client/server.sls
+++ b/mysql/client/server.sls
@@ -56,6 +56,29 @@
 
 {%- endfor %}
 
+{%- if database.initial_data is defined %}
+
+/root/mysql/scripts/restore_{{ database_name }}.sh:
+  file.managed:
+  - source: salt://mysql/conf/restore.sh
+  - mode: 770
+  - template: jinja
+  - defaults:
+    database_name: {{ database_name }}
+    database: {{ database }}
+  - require:
+    - mysql_database: mysql_{{ server_name }}_database_{{ database_name }}
+
+restore_mysql_database_{{ database_name }}:
+  cmd.run:
+  - name: /root/mysql/scripts/restore_{{ database_name }}.sh
+  - unless: "[ -f /root/mysql/flags/{{ database_name }}-installed ]"
+  - cwd: /root
+  - require:
+    - file: /root/mysql/scripts/restore_{{ database_name }}.sh
+
+{%- endif %}
+
 {%- endfor %}
 
 {%- for user in server.get('users', []) %}
diff --git a/mysql/conf/restore.sh b/mysql/conf/restore.sh
index 8cce564..791fa49 100755
--- a/mysql/conf/restore.sh
+++ b/mysql/conf/restore.sh
@@ -1,22 +1,24 @@
 #!/bin/sh
 
-cd /root/mysql
-
-{%- for database in pillar.mysql.server.databases %}
-{%- if database.name == database_name %}
-
 {%- set age = database.initial_data.get("age", "0") %}
 {%- set host = database.initial_data.get("host", grains.id ) %}
-{%- set name = database.initial_data.get("database", database.name) %}
+{%- set name = database.initial_data.get("database", '') %}
 {%- set source_name = name + ".sql.gz" %}
-{%- set dest_name = database.name + ".sql.gz" %}
-{%- set target = "/root/mysql/data/" %}
+{%- set dest_name = database_name + ".sql.gz" %}
+{%- set path = "/var/backups/mysql/" %}
 
-scp backupninja@{{ database.initial_data.source }}:/srv/backupninja/{{ host }}/var/backups/mysql/mysql.{{ age }}/sqldump/{{ source_name }} {{ target }}{{ dest_name }}
+{%- if database.initial_data.source != 'localhost' %}
 
-gunzip -c {{ target }}{{ dest_name }} | mysql -h 127.0.0.1 -u {{ database.users[0].name }} -p{{ database.users[0].password }} {{ database.name }}
+scp backupninja@{{ database.initial_data.source }}:/srv/backupninja/{{ host }}{{ path }}mysql.{{ age }}/sqldump/{{ source_name }} /root/mysql/data/{{ source_name }}
 
-touch /root/mysql/flags/{{ database.name }}-installed
+gunzip -c /root/mysql/data/{{ source_name }} | mysql -u{{ database.users[0].name }} -p{{ database.users[0].password }} {{ database_name }}
+
+touch /root/mysql/flags/{{ database_name }}-installed
+
+{%- else %}
+
+gunzip -c {{ path }}sqldump/{{ source_name }} | mysql -u{{ database.users[0].name }} -p{{ database.users[0].password }} {{ database_name }}
+
+touch /root/mysql/flags/{{ database_name }}-installed
 
 {%- endif %}
-{%- endfor %} 
\ No newline at end of file
diff --git a/mysql/server/database.sls b/mysql/server/database.sls
index 26b8c5e..eb9df79 100644
--- a/mysql/server/database.sls
+++ b/mysql/server/database.sls
@@ -49,6 +49,7 @@
   - template: jinja
   - defaults:
     database_name: {{ database_name }}
+    database: {{ database }}
   - require:
     - file: mysql_dirs
     - mysql_database: mysql_database_{{ database_name }}