<p>I'm trying to make a backup from Mongo / K8S with this script</p>

<pre><code>export POD=$(kubectl get pods -l app=mongo-client -o custom-columns=:metadata.name -n espace-client)
kubectl exec "$POD" sh -c 'mongodump --archive' &gt; ~/backup/mongo/$(date +%F).db.dump
</code></pre>

<p>I get this error:</p>

<pre><code>Error from server (NotFound): pods "\nmongo-client-79667cc85d-tsg72" not found
</code></pre>

<p>When I check the pods, I can see <code>mongo-client-79667cc85d-tsg72</code></p>

<p>When I put the name without variable, it works well, so it might be because of initial <code>\n</code>. How can I avoid it ?
How can I remove it from name ?</p>

<p>Your <code>kubectl get pods</code> command is constrained with a namespace selector <code>-n espace-client</code>.  Your <code>kubectl exec</code> command also needs the namespace flag. </p>
