Merge "Add option to set exact backup times and disable auto backup"
diff --git a/README.rst b/README.rst
index 28e810d..9392502 100644
--- a/README.rst
+++ b/README.rst
@@ -30,6 +30,20 @@
           host: 10.10.10.208
           user: backupninja
 
+Backup client with ssh/rsync remote target with specific rsync options
+
+.. code-block:: yaml
+
+    backupninja:
+      client:
+        enabled: true
+        target:
+          engine: rsync
+          engine_opts: "-av --delete --recursive --safe-links"
+          home_dir: /srv/volumes/backup/backupninja
+          host: 10.10.10.208
+          user: backupninja
+
 Backup client with s3 remote target
 
 .. code-block:: yaml
@@ -114,6 +128,13 @@
             enabled: true
             key: ssh-key
 
+Backup server without strict client policy restriction
+
+.. code-block:: yaml
+
+    backupninja:
+      server:
+        restrict_clients: false
 
 Backup client with local storage
 
diff --git a/backupninja/files/authorized_keys b/backupninja/files/authorized_keys
new file mode 100644
index 0000000..6f54c9b
--- /dev/null
+++ b/backupninja/files/authorized_keys
@@ -0,0 +1,19 @@
+{%- from "backupninja/map.jinja" import server with context %}
+{%- for key_name, key in server.key.iteritems() %}
+{%- if key.get('enabled', False) %}
+{%- set clients = [] %}
+{%- if server.restrict_clients %}
+  {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
+    {%- if node_grains.get('backupninja', {}).get('client') %}
+    {%- set client = node_grains.backupninja.get("client") %}
+      {%- if client.get('addresses') and client.get('addresses', []) is iterable %}
+        {%- for address in client.addresses %}
+          {%- do clients.append(address|string) %}
+        {%- endfor %}
+      {%- endif %}
+    {%- endif %}
+  {%- endfor %}
+{%- endif %}
+no-pty{%- if clients %},from="{{ clients|join(',') }}"{%- endif %} {{ key.key }}
+{%- endif %}
+{%- endfor %}
\ No newline at end of file
diff --git a/backupninja/files/rdiff.conf b/backupninja/files/rdiff.conf
index 2e8bcb3..63e347d 100644
--- a/backupninja/files/rdiff.conf
+++ b/backupninja/files/rdiff.conf
@@ -19,6 +19,9 @@
 ## 
 ## Default:
 # options = 
+{% if client.target.engine_opts is defined %}
+options = {{ client.target.engine_opts }}
+{%- endif %}
 
 ## default is 0, but set to 19 if you want to lower the priority.
 ## an example setting would be:
diff --git a/backupninja/files/rsync.conf b/backupninja/files/rsync.conf
index d45b350..266eb59 100644
--- a/backupninja/files/rsync.conf
+++ b/backupninja/files/rsync.conf
@@ -25,7 +25,7 @@
 # backup partition mountpoint or backup main folder
 # this doesn't need to be a real partition, but should be at least the
 # main folder where the backup is being stored
-mountpoint = /srv/backupninja
+mountpoint = {{ client.target.get('home_dir', '/srv/backupninja') }}
 
 # folder relative do mountpoint where the backup should be stored
 backupdir = {{ system.name }}.{{ system.domain }}
@@ -129,7 +129,11 @@
 #rsync = $RSYNC
 
 # rsync command options
+{% if client.target.engine_opts is defined %}
+rsync_options = {{ client.target.engine_opts }}
+{%- else %}
 #rsync_options = "-av --delete --recursive"
+{%- endif %}
 
 # when set to 1, use numeric ids instead of user/group mappings on rsync
 #numericids =
diff --git a/backupninja/map.jinja b/backupninja/map.jinja
index 2a5db08..048d43c 100644
--- a/backupninja/map.jinja
+++ b/backupninja/map.jinja
@@ -20,11 +20,13 @@
   'Debian': {

     'pkgs': ['rsync'],

     'home_dir': '/srv/backupninja',

+    'restrict_clients': True,

     'keys': [],

   },

   'RedHat': {

     'pkgs': ['rsync'],

     'home_dir': '/srv/backupninja',

+    'restrict_clients': True,

     'keys': [],

   },

 }, merge=salt['pillar.get']('backupninja:server')) %}

diff --git a/backupninja/meta/salt.yml b/backupninja/meta/salt.yml
index 41080b9..d7f60d4 100644
--- a/backupninja/meta/salt.yml
+++ b/backupninja/meta/salt.yml
@@ -3,7 +3,22 @@
     {%- if pillar.get('backupninja', {}).get('client') %}
     {%- from "backupninja/map.jinja" import service_grains with context %}
     {{ service_grains|yaml(False)|indent(4) }}
+      {#- client addresses #}
+      {%- set addresses = [] %}
+      {%- set ips = salt['grains.get']("fqdn_ip4")|list %}
+      {%- if ips %}
+        {%- for ip in ips %}
+          {%- if not (ip|string).startswith('127.')  %}
+            {%- do addresses.append(ip) %}
+          {%- endif %}
+        {%- endfor %}
+      {%- endif %}
+      {%- if addresses %}
+      client:
+        addresses: {{ addresses|yaml }}
+      {%- endif %}
     {%- else %}
     backupninja:
       backup: {}
     {%- endif %}
+
diff --git a/backupninja/server.sls b/backupninja/server.sls
index 4be4fc4..58bf986 100644
--- a/backupninja/server.sls
+++ b/backupninja/server.sls
@@ -9,9 +9,9 @@
   user.present:
   - name: backupninja
   - system: true
-  - home: /srv/backupninja
+  - home: {{ server.home_dir }}
 
-/srv/backupninja:
+{{ server.home_dir }}:
   file.directory:
   - mode: 700
   - user: backupninja
@@ -21,27 +21,30 @@
     - user: backupninja_user
     - pkg: backupninja_server_packages
 
-{%- for key_name, key in server.key.iteritems() %}
-
-{%- if key.get('enabled', False) %}
-
-backupninja_key_{{ key.key }}:
-  ssh_auth.present:
+{{ server.home_dir }}/.ssh:
+  file.directory:
+  - mode: 700
   - user: backupninja
-  - name: {{ key.key }}
+  - group: backupninja
   - require:
-    - file: /srv/backupninja
+    - file: {{ server.home_dir }}
 
-{%- endif %}
-
-{%- endfor %}
+{{ server.home_dir }}/.ssh/authorized_keys:
+  file.managed:
+  - user: backupninja
+  - group: backupninja
+  - template: jinja
+  - source: salt://backupninja/files/authorized_keys
+  - require:
+    - file: {{ server.home_dir }}
+    - file: {{ server.home_dir }}/.ssh
 
 {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
 
 {%- for backup_name, backup in node_grains.get('backupninja', {}).get('backup', {}).iteritems() %}
 {%- for fs_include in backup.fs_includes %}
 
-/srv/backupninja/{{ node_name }}{{ fs_include }}:
+{{ server.home_dir }}/{{ node_name }}{{ fs_include }}:
   file.directory:
   - mode: 700
   - user: backupninja
@@ -56,4 +59,4 @@
 
 {%- endfor %}
 
-{%- endif %}
\ No newline at end of file
+{%- endif %}