Add restore script for Maas DB

Change-Id: I8e758900f55aabde167feb5f9544b0d9507da570
diff --git a/README.rst b/README.rst
index f6c1f9f..71c356d 100644
--- a/README.rst
+++ b/README.rst
@@ -215,6 +215,16 @@
         enabled: true
         role: master/slave
 
+MAAS region service with backup data
+
+.. code-block:: yaml
+    maas:
+      region:
+        database:
+          initial_data:
+            source: cfg01.local
+            host: 192.168.0.11
+
 Module function's example:
 ==========================
 
@@ -298,4 +308,4 @@
 Any questions or feedback is always welcome so feel free to join our IRC
 channel:
 
-    #salt-formulas @ irc.freenode.net
+    #salt-formulas @ irc.freenode.net
\ No newline at end of file
diff --git a/maas/files/restore.sh b/maas/files/restore.sh
new file mode 100644
index 0000000..3d8e786
--- /dev/null
+++ b/maas/files/restore.sh
@@ -0,0 +1,42 @@
+{%- from "maas/map.jinja" import region with context}
+
+{%- set database = region.get("database", {}) %}
+
+{%- if database.host is defined %}
+{%- set pghost = database.get("host", "") %}
+export PGHOST={{ pghost }}
+{%- endif %}
+{%- if database.user is defined %}
+{%- set pguser = database.get("user", "") %}
+export PGUSER={{ pguser }}
+{%- endif %}
+export PGPASSFILE=/root/.pgpass
+
+{%- set db_name = database.get("name", "maasdb") %}
+{%- set backupninja_host = database.initial_data.get("host", grains.id ) %}
+{%- if database.initial_data.age is defined %}
+{%- set age = database.initial_data.get("age", "0") %}
+{%- else %}{%- set age = "0" %}{%- endif %}
+{%- set backupninja_source = database.initial_data.get("source", "cfg01.local")%}
+{%- set source_name = db_name + ".pg_dump.gz" %}
+{%- set dest_name = db_name + ".pg_dump.gz" %}
+{%- set target = "/root/postgresql/data/" %}
+
+
+scp backupninja@{{ backupninja_host }}:/srv/backupninja/{{ backupninja_source }}/var/backups/postgresql/postgresql.{{ age }}/{{ source_name }} {{ target }}{{ dest_name }} 
+gunzip -d -1 -f {{ target }}{{ dest_name }}
+
+scp -r backupninja@{{ backupninja_host }}:/srv/backupninja/{{ backupninja_source }}/etc/maas/maas.{{ age }} /etc/maas
+scp -r backupninja@{{ backupninja_host }}:/srv/backupninja/{{ backupninja_source }}/var/lib/maas/maas.{{ age }} /var/lib/maas
+
+sudo systemctl stop maas-dhcpd.service
+sudo systemctl stop maas-rackd.service
+sudo systemctl stop maas-regiond.service
+
+pg_restore {{ target }}{{ db_name }}.pg_dump --dbname={{ db_name }} --no-password -c
+
+touch /root/maas/flags/{{ db_name }}-installed
+
+sudo systemctl start maas-dhcpd.service
+sudo systemctl start maas-rackd.service
+sudo systemctl start maas-regiond.service
\ No newline at end of file
diff --git a/maas/region.sls b/maas/region.sls
index 90d6657..6dfc4dc 100644
--- a/maas/region.sls
+++ b/maas/region.sls
@@ -20,6 +20,24 @@
   - require:
     - pkg: maas_region_packages
 
+{%- if region.database.initial_data is defined %}
+
+/root/maas/scripts/restore_{{ region.database.name }}.sh:
+  file.managed:
+    - source: salt://maas/files/restore.sh
+    - mode: 770
+    - template: jinja
+
+restore_maas_database_{{ region.database.name }}:
+  cmd.run:
+  - name: /root/maas/scripts/restore_{{ region.database.name }}.sh
+  - unless: "[ -f /root/maas/flags/{{ region.database.name }}-installed ]"
+  - cwd: /root
+  - require:
+    - file: /root/maas/scripts/restore_{{ region.database.name }}.sh
+
+{%- endif %}
+
 {%- if region.get('enable_iframe', False)  %}
 
 /etc/apache2/conf-enabled/maas-http.conf: