<p>I am backuping my Postgresql database using this cronjob:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  schedule: &quot;0 2 * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup
            image: postgres:10.4
            command: [&quot;/bin/sh&quot;]
            args: [&quot;-c&quot;, 'echo &quot;$PGPASS&quot; &gt; /root/.pgpass &amp;&amp; chmod 600 /root/.pgpass &amp;&amp; pg_dump -Fc -h &lt;host&gt; -U &lt;user&gt; &lt;db&gt; &gt; /var/backups/backup.dump']
            env:
            - name: PGPASS
              valueFrom:
                secretKeyRef:
                  name: pgpass
                  key: pgpass
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          restartPolicy: Never
          volumes:
          - name: postgres-backup-storage
            hostPath:
              path: /var/volumes/postgres-backups
              type: DirectoryOrCreate
</code></pre>
<p>The cronjob gets successfully executed, the backup is made and saved in the container of the Job but this container is stopped after successful execution of the script.
of course I want to access the backup files in the container but I can't because it is stopped/terminated.</p>
<p>is there a way to execute shell commands in a container after it is terminated, so I can access the backup files saved in the container?</p>
<p>I know that I could do that on the node, but I don't have the permission to access it.</p>

<p><a href="https://stackoverflow.com/users/3638081/confused-genius">@confused genius</a> gave me a great idea to create another same container to access the dump files so this is the solution that works:</p>
<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: postgres-backup
spec:
  schedule: &quot;0 2 * * *&quot;
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: postgres-backup
            image: postgres:10.4
            command: [&quot;/bin/sh&quot;]
            args: [&quot;-c&quot;, 'echo &quot;$PGPASS&quot; &gt; /root/.pgpass &amp;&amp; chmod 600 /root/.pgpass &amp;&amp; pg_dump -Fc -h &lt;host&gt; -U &lt;user&gt; &lt;db&gt; &gt; /var/backups/backup.dump']
            env:
            - name: PGPASS
              valueFrom:
                secretKeyRef:
                  name: dev-pgpass
                  key: pgpass
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          - name: postgres-restore
            image: postgres:10.4
            volumeMounts:
            - mountPath: /var/backups
              name: postgres-backup-storage
          restartPolicy: Never
          volumes:
          - name: postgres-backup-storage
            hostPath:
            # Ensure the file directory is created.
              path: /var/volumes/postgres-backups
              type: DirectoryOrCreate
</code></pre>
<p>after that one just needs to sh into the &quot;postgres-restore&quot; container and access the dump files.</p>
<p>thanks</p>
