<p>I have the following CronJob to run a backup of my database, and I'd like the backup files to be appended with the date:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- if .Values.postgresqlBackup.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  schedule: {{ .Values.postgresqlBackup.cron | quote }}
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: postgres
              image: postgres:latest
              imagePullPolicy: IfNotPresent
              command: 
                - pg_dump 
                - --username=postgres 
                - --no-password
                - --format=custom
                - --file=/backups/dragalia-api-$(date +&quot;%Y-%m-%d_%H-%M-%S&quot;).bak
                - --host={{ include &quot;dragalia-api.fullname&quot; . }}-postgresql
                - --verbose
              volumeMounts:
                - name: data
                  mountPath: /backups
              env:
                - name: PGPASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: {{ include &quot;dragalia-api.fullname&quot; . }}-postgresql
                      key: postgres-password
                      optional: false
          restartPolicy: Never
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: {{ include &quot;dragalia-api.fullname&quot; . }}-db-backup
{{- end }}
</code></pre>
<p>The job executes successfully but I am left with files like:</p>
<pre><code>docker@minikube:/dragalia-api/db-backup$ ls
'dragalia-api-$(date +&quot;%Y-%m-%d_%H-%M-%S&quot;).bak'
</code></pre>
<p>The entire filename is quoted and the string is not evaluated. How can I make it so that the string is evaluated by the shell?</p>
<p>Things I've tried:</p>
<ul>
<li>using backticks: <code>--file=/backups/dragalia-api-1`date +&quot;%Y-%m-%d_%H-%M-%S&quot;.bak` </code>: still rendered literally</li>
<li>defining a DATE env var and putting ${DATE} in the string: rendered literally</li>
<li>escaping the % signs e.g. <code>\%Y</code>: rendered literally</li>
<li>passing a multi-line string to <code>sh -c</code>: this caused the job to fail on being unable to connect to the db, so I guess <code>--host</code> was not passed properly</li>
</ul>
<p>The only other thing I can think of is passing in a shell script, but I'd rather not create another resource if possible.</p>
<p>Equivalently, since the date information is stored by the filesystem, if there's some other way to pass a unique string into the filename that would work. Anything so that it keeps rolling backups instead of just the one.</p>

lordnoob
<p>If you want to use shell substitution, then you need to execute your command <strong>with a shell</strong>. For example:</p>
<pre><code>containers:
  - name: postgres
    image: postgres:latest
    imagePullPolicy: IfNotPresent
    command:
      - /bin/sh
      - -c
      - &gt;
        pg_dump
        --username=postgres
        --no-password
        --format=custom
        --file=/backups/dragalia-api-$(date +&quot;%Y-%m-%d_%H-%M-%S&quot;).bak
        --host={{ include &quot;dragalia-api.fullname&quot; . }}-postgresql
        --verbose
</code></pre>
<p>Also, unrelated to your question, you should pin your <code>postgres</code> image to a specific version (<code>postgres:14</code>) or you'll be in for a rude surprise when <code>:latest</code> is unexpectedly a new major version.</p>
