Add option to set exact backup times and disable auto backup

Change-Id: Ia2f78b81d2ed958353a0174910ef2450215d3d7b
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 cf21103..28e810d 100644
--- a/README.rst
+++ b/README.rst
@@ -59,6 +59,48 @@
               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 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/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