[Unit]
Description=Restic backup

[Service]
Type=oneshot
User={{ restic_user }}

CPUQuota={{ 25 * ansible_processor_vcpus }}%

{% if restic_ssh_enabled %}
Environment="RESTIC_REPOSITORY=sftp:{{ restic_ssh_host }}:{{ restic_repository_name }}"
{% else %}
Environment="RESTIC_REPOSITORY={{ restic_repository }}"
{% endif -%}
Environment="RESTIC_PASSWORD={{ restic_password}}"

{% if restic_aws_access_key_id is defined and restic_aws_secret_access_key is defined %}
Environment="AWS_ACCESS_KEY_ID={{ restic_aws_access_key_id}}"
Environment="AWS_SECRET_ACCESS_KEY={{ restic_aws_secret_access_key}}"
{% endif %}

{% if restic_check %}
ExecStartPre={{ restic_path }} check
{% endif -%}

{% for folder in restic_folders_combined %}
ExecStart={{ restic_path }} backup --verbose {{ folder }}
{% endfor -%}

{% for database in restic_databases %}
ExecStart=/bin/sh -c "{{ database.dump_command }} {{ '| pigz |' if restic_dump_compression_enabled else '|' }} {{ restic_path }} backup --verbose --stdin --stdin-filename {{ database.name }}{{ '.sql.gz' if restic_dump_compression_enabled else '.sql' }}"
{% endfor -%}

{% if restic_forget %}
ExecStartPost={{ restic_path }} {% if restic_keep_last is defined and restic_keep_last != false %}--keep-last{{ restic_keep_last }}{% endif %}
  {% if restic_keep_hourly is defined and restic_keep_hourly != false %}--keep-hourly {{ restic_keep_hourly }}{% endif %}
  {% if restic_keep_daily is defined and restic_keep_daily != false %}--keep-daily {{ restic_keep_daily }}{% endif %}
  {% if restic_keep_weekly is defined and restic_keep_weekly != false %}--keep-weekly {{ restic_keep_weekly }}{% endif %}
  {% if restic_keep_monthly is defined and restic_keep_monthly != false %}--keep-monthly {{ restic_keep_monthly }}{% endif %}
  {% if restic_keep_yearly is defined and restic_keep_yearly != false %}--keep-yearly {{ restic_keep_yearly }}{% endif %}
  {% if restic_keep_within is defined and restic_keep_within != false %}--keep-within {{ restic_keep_within }}{% endif %}
{% endif -%} forget

{% if restic_prune %}
ExecStartPost={{ restic_path }} prune
{% endif -%}
