Merge "Update metadata.yml with correct URL"
diff --git a/.kitchen.yml b/.kitchen.yml
index 3fb19aa..7af31ff 100644
--- a/.kitchen.yml
+++ b/.kitchen.yml
@@ -51,11 +51,21 @@
       pillars-from-files:
         backupninja.sls: tests/pillar/client_rsync.sls
 
+  - name: client_rsync_backup_times
+    provisioner:
+      pillars-from-files:
+        backupninja.sls: tests/pillar/client_rsync_backup_times.sls
+
   - name: client_s3
     provisioner:
       pillars-from-files:
         backupninja.sls: tests/pillar/client_s3.sls
 
+  - name: client_s3_disabled_auto
+    provisioner:
+      pillars-from-files:
+        backupninja.sls: tests/pillar/client_s3_disabled_auto.sls
+
   - name: client_webdav
     provisioner:
       pillars-from-files:
diff --git a/.travis.yml b/.travis.yml
index 0629a98..20b5d2c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -19,15 +19,21 @@
 env:
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2016.3 SUITE=server-rdiff
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2016.3 SUITE=client-rsync
+    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2016.3 SUITE=client-rsync_backup_times
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2016.3 SUITE=client-s3
+    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2016.3 SUITE=client-s3_disabled_auto
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2016.3 SUITE=client-webdav
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2017.7 SUITE=server-rdiff
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2017.7 SUITE=client-rsync
+    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2017.7 SUITE=client-rsync_backup_times
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2017.7 SUITE=client-s3
+    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2017.7 SUITE=client-s3_disabled_auto
     - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-xenial-salt-2017.7 SUITE=client-webdav
 #    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-bionic-salt-2017.7 SUITE=server-rdiff
 #    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-bionic-salt-2017.7 SUITE=client-rsync
+#    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-bionic-salt-2017.7 SUITE=client-rsync_backup_times
 #    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-bionic-salt-2017.7 SUITE=client-s3
+#    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-bionic-salt-2017.7 SUITE=client-s3_disabled_auto
 #    - PLATFORM=epcim/salt-formulas:saltstack-ubuntu-bionic-salt-2017.7 SUITE=client-webdav
 
 before_script:
diff --git a/README.rst b/README.rst
index 3bbcf44..a05787b 100644
--- a/README.rst
+++ b/README.rst
@@ -73,6 +73,63 @@
               principal: host/${linux:network:fqdn}
               keytab: /etc/krb5.keytab
 
+Backup client with exact backup times
+
+
+.. note:: This settings will configure global backupninja backup to be
+   triggered at exactly set times.
+
+.. code-block:: yaml
+
+    backupninja:
+      client:
+        enabled: true
+        auto_backup_disabled: false
+        backup_times:
+          day_of_week: 1
+          hour: 2
+          minute: 32
+
+.. note:: This will trigger backup every monday at 2:32 AM.
+
+.. code-block:: yaml
+
+    backupninja:
+      client:
+        enabled: true
+        auto_backup_disabled: false
+        backup_times:
+          day_of_month: 24
+          hour: 14
+          minute: 12
+
+.. note:: This will trigger backup every 24th day of every month at 14:12 (2:12 PM).
+
+.. note:: Available parameters:
+   ``day_of_week`` (0, 3, 6 ...). If not set, defaults to '*'.
+   ``day_of_month`` (20, 25, 12, ...). If not set, defaults to '*'.
+     Only ``day_of_week`` or ``day_of_month`` can be defined at the same time.
+   ``hour`` (1, 10, 15, ...). If not defined, defaults to `1`. Uses 24 hour format.
+   ``minute`` (5, 10, 59, ...). If not defined, defaults to `00`.
+
+..note:: Parameter ``auto_backup_disabled`` is optional. It disables automatic
+  backup when set to true. It's set to ``false``by default when not defined.
+
+Backup client with defined number of backups to keep:
+
+..code-block:: yaml
+
+    backupninja:
+      client:
+        enabled: true
+          engine: rsync
+          host: 10.10.10.208
+          user: backupninja
+          keep: 4
+
+  ..note:: The ``keep`` parameter defines number of backups to keep. If not
+    defined it's set to ``7`` by default.
+
 Backup server rsync/rdiff
 
 .. code-block:: yaml
diff --git a/backupninja/client.sls b/backupninja/client.sls
index fd0f0bd..8b61b8f 100644
--- a/backupninja/client.sls
+++ b/backupninja/client.sls
@@ -20,6 +20,36 @@
   - user: root
   - group: root
 
+{%- if client.backup_times is defined %}
+
+delete_cron_file:
+  file.absent:
+  - name: /etc/cron.d/backupninja
+  - require:
+    - pkg: backupninja_packages
+
+create_cron_job:
+  cron.present:
+  - name: if [ -x /usr/sbin/backupninja ]; then /usr/sbin/backupninja --run; fi
+  - user: root
+  {%- if client.backup_times.day_of_week is defined %}
+  - dayweek: {{ client.backup_times.day_of_week }}
+  {%- endif %}
+  {%- if client.backup_times.day_of_month is defined %}
+  - daymonth: {{ client.backup_times.day_of_month }}
+  {%- endif %}
+  {%- if client.backup_times.hour is defined %}
+  - hour: {{ client.backup_times.hour }}
+  {%- endif %}
+  {%- if client.backup_times.minute is defined %}
+  - minute: {{ client.backup_times.minute }}
+  {%- endif %}
+  {%- if client.get('auto_backup_disable', False) %}
+  - commented: True
+  {%- endif %}
+
+{%- endif %}
+
 {%- if pillar.postgresql is defined or pillar.maas is defined %}
 backupninja_postgresql_handler:
   file.managed:
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/rsync.conf b/backupninja/files/rsync.conf
index 266eb59..19b5c26 100644
--- a/backupninja/files/rsync.conf
+++ b/backupninja/files/rsync.conf
@@ -61,7 +61,7 @@
 # that with the days config your backup gets a .0 suffix at the destination
 # folder, making it easier to turn it later to an incremental backup.
 #
-days = 7
+days = {{ client.target.keep|default(7) }}
 
 # for long storage format, specify the number of daily backup increments
 #keepdaily = 7
diff --git a/backupninja/files/rsync.sh b/backupninja/files/rsync.sh
index 354e451..c3d8241 100644
--- a/backupninja/files/rsync.sh
+++ b/backupninja/files/rsync.sh
@@ -259,7 +259,7 @@
   fi
 
   ssh_cmd_base="ssh -T -o PasswordAuthentication=no -p $port -i $id_file"
-  ssh_cmd="$ssh_cmd_base $user@$host"
+  ssh_cmd="$ssh_cmd_base $user@$host bash"
 
   if [ "$from" == "remote" ] || [ "$dest" == "remote" ]; then
     if [ "$testconnect" == "yes" ] && [ "$protocol" == "ssh" ]; then
diff --git a/backupninja/server.sls b/backupninja/server.sls
index 7417b45..58bf986 100644
--- a/backupninja/server.sls
+++ b/backupninja/server.sls
@@ -21,46 +21,23 @@
     - user: backupninja_user
     - pkg: backupninja_server_packages
 
-{%- 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 %}
-
-backupninja_key_{{ key.key }}:
-  ssh_auth.present:
+{{ server.home_dir }}/.ssh:
+  file.directory:
+  - mode: 700
   - user: backupninja
-  - name: {{ key.key }}
-  - options:
-    - no-pty
-{%- if clients %}
-    - from="{{ clients|join(',') }}"
-{%- endif %}
+  - group: backupninja
   - require:
     - file: {{ server.home_dir }}
 
-{%- else %}
-
-backupninja_key_{{ key.key }}:
-  ssh_auth.absent:
+{{ server.home_dir }}/.ssh/authorized_keys:
+  file.managed:
   - user: backupninja
-  - name: {{ key.key }}
-
-{%- endif %}
-
-{%- endfor %}
+  - 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() %}
 
@@ -82,4 +59,4 @@
 
 {%- endfor %}
 
-{%- endif %}
\ No newline at end of file
+{%- endif %}
diff --git a/tests/pillar/client_rsync_backup_times.sls b/tests/pillar/client_rsync_backup_times.sls
new file mode 100644
index 0000000..adb5a78
--- /dev/null
+++ b/tests/pillar/client_rsync_backup_times.sls
@@ -0,0 +1,15 @@
+backupninja:
+  client:
+    enabled: true
+    backup_times:
+      day_of_week: 1
+      hour: 4
+      minute: 52
+    target:
+      engine: rsync
+      host: 10.10.10.208
+      user: backupninja
+linux:
+  system:
+    name: hostname
+    domain: domain
\ No newline at end of file
diff --git a/tests/pillar/client_s3_disabled_auto.sls b/tests/pillar/client_s3_disabled_auto.sls
new file mode 100644
index 0000000..8ee38bf
--- /dev/null
+++ b/tests/pillar/client_s3_disabled_auto.sls
@@ -0,0 +1,10 @@
+backupninja:
+  client:
+    enabled: true
+    auto_backup_disabled: true
+    target:
+      engine: dup
+      url: s3+http://bucket-name/folder-name
+      auth:
+        awsaccesskeyid: awsaccesskeyid
+        awssecretaccesskey: awssecretaccesskey
\ No newline at end of file