<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Sqlite – Serverküche</title><link>https://serverkueche.de/en/tags/sqlite/</link><description>Sqlite – Neueste Beiträge von Serverküche</description><generator>Hugo</generator><language>en-US</language><managingEditor>feedback@serverkueche.de (Serverküche)</managingEditor><webMaster>feedback@serverkueche.de (Serverküche)</webMaster><copyright>2026 Serverküche</copyright><lastBuildDate>Thu, 03 Dec 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://serverkueche.de/en/tags/sqlite/index.xml" rel="self" type="application/rss+xml"/><item><title>Database backups with Docker: the volume copy loses data silently</title><link>https://serverkueche.de/en/tutorials/database-backups-docker/</link><pubDate>Thu, 03 Dec 2026 00:00:00 +0000</pubDate><author>feedback@serverkueche.de (Serverküche)</author><guid>https://serverkueche.de/en/tutorials/database-backups-docker/</guid><description>Measured, not claimed: what a volume copy really loses under load – plus a script that finds every database container itself and fails loudly.</description><content:encoded><![CDATA[<p>&ldquo;Never back up a running database file&rdquo; appears in every backup guide, usually without
evidence. We measured it on our test server, and the result is more uncomfortable than the
usual warning.</p>
<p>The copy is <strong>not</strong> broken. It starts, it passes every integrity check, and that is exactly
why nobody notices that data is missing.</p>
<h2 id="what-are-we-building">What are we building?</h2>
<p>A backup script that finds every running database container by itself, so you never have
to add a line for a new app. When something goes wrong it fails loudly instead of quietly
dropping an empty file. Plus the matching restore commands per engine, each one timed.</p>
<p>Covered: PostgreSQL and MariaDB/MySQL, plus SQLite as a separate case, where container
discovery cannot work by design.</p>
<p>Every number below comes from this site&rsquo;s test VPS: PostgreSQL 18.6 with two million rows
(195 MB) and MariaDB 12.3.3 with one million rows.</p>
<h2 id="requirements">Requirements</h2>
<ul>
<li>A server with Docker and at least one containerised database
(<a href="/en/tutorials/docker-compose-basics/">Compose basics</a>).</li>
<li><code>zstd</code> for compression: <code>apt install zstd</code>.</li>
<li>A working <a href="/en/tutorials/restic-backups/">restic backup</a>. This article produces the
dumps; restic moves them off the server. Both together are the backup. A dump file on
the same disk is not one.</li>
</ul>
<div class="not-prose my-6 overflow-hidden rounded-xl border border-paprika-200 bg-paprika-50 dark:border-paprika-800 dark:bg-paprika-900/20"
     data-track-content data-content-name="Affiliate-Box · /en/tutorials/database-backups-docker/" data-content-piece="VPS 1000 G12.5">
  <div class="flex items-center justify-between border-b border-paprika-200 bg-paprika-100 px-4 py-1.5 text-xs font-semibold uppercase tracking-wide text-paprika-700 dark:border-paprika-800 dark:bg-paprika-900/40 dark:text-paprika-300">
    <span>🍳 Recommendation</span>
    <span title="Links marked with * are affiliate links.">Ad</span>
  </div>
  <div class="flex flex-col gap-4 p-4 sm:flex-row sm:items-center sm:justify-between">
    <div>
      <p class="text-lg font-bold text-slate-900 dark:text-white">VPS 1000 G12.5</p>
      <p class="mt-1 text-sm text-slate-600 dark:text-slate-300">4 vCores · 8 GB RAM · 128 GB SSD</p>
      <p class="mt-1 text-sm font-semibold text-paprika-700 dark:text-paprika-400">from €14.50/month</p>
      <p class="mt-2 text-sm text-slate-600 dark:text-slate-400">The server every measurement was taken on.</p>
    </div>
    <a href="https://www.netcup.com/en/server/vps/vps-1000-g12.5-iv-24m-eu?ref=44083#vps-1000-g12.5-iv-12m-eu" rel="sponsored noopener" target="_blank"
   data-track-event="Affiliate|netcup: Affiliate-Box|VPS 1000 G12.5 · {page}"
   class="inline-flex shrink-0 items-center justify-center rounded-lg bg-paprika-600 px-5 py-2.5 font-semibold text-white transition-colors hover:bg-paprika-700">
  Go to netcup →
</a>

  </div><div class="px-4 pb-4"><div class="not-prose my-3 rounded-lg border border-herb-500/40 bg-herb-50 px-3 py-2 text-sm text-slate-700 dark:bg-herb-900/20 dark:text-slate-200"
     data-voucher-api="vps-1000" data-voucher-msg-empty="No code is available for this plan right now – use the €5.95 code." data-voucher-msg-limit="No more codes today – use the €5.95 code or try again tomorrow." data-voucher-msg-error="The code couldn&#39;t be fetched right now – use the €5.95 code."
     data-voucher-hint="Empty the cart first – the code adds the server itself.">
  <p class="flex flex-wrap items-center gap-x-2 gap-y-1"><span>💶 <span class="font-semibold">1 month free</span>:</span>
    <button type="button" data-voucher-reveal data-copy-title="Click to copy"
            class="cursor-pointer rounded bg-white px-2 py-0.5 font-mono text-sm font-semibold text-herb-800 hover:ring-2 hover:ring-herb-500/40 dark:bg-slate-800 dark:text-herb-400">Show code</button>
    <button type="button" data-voucher-next hidden
            class="rounded border border-herb-500/40 px-2 py-0.5 text-xs text-herb-800 hover:bg-white dark:text-herb-400 dark:hover:bg-slate-800">another code</button>
  </p><p class="mt-1 text-xs text-slate-500 dark:text-slate-400" data-voucher-hint-target>
    Single use, valid for VPS 1000 G12.5.
    <a href="https://www.netcup.com/en/checkout/cart?ref=44083" rel="sponsored noopener" target="_blank"
       data-track-event="Affiliate|netcup: Gutschein einlösen|VPS 1000 G12.5 · {page}"
       class="font-medium text-herb-800 underline underline-offset-2 hover:text-herb-900 dark:text-herb-400">Redeem in the cart →</a>
  </p><p class="mt-2 flex flex-wrap items-center gap-x-2 gap-y-1 border-t border-herb-500/30 pt-2">
    <span>💶 <span class="font-semibold">€5.95 voucher</span> for new netcup customers:</span>
    <button type="button" data-voucher-code data-track-voucher="36nc17844976032"
            title="Click to copy" class="cursor-pointer rounded bg-white px-2 py-0.5 font-mono text-sm font-semibold text-herb-800 hover:ring-2 hover:ring-herb-500/40 dark:bg-slate-800 dark:text-herb-400">36nc17844976032</button>
    <span class="text-xs text-slate-500 dark:text-slate-400">always valid · not for domains or VPS Lite</span>
  </p><p class="mt-1 text-xs"><a href="/en/netcup-voucher/" class="font-medium text-herb-800 underline underline-offset-2 hover:text-herb-900 dark:text-herb-400">All netcup vouchers →</a></p>
</div></div>
</div>

<h2 id="step-by-step">Step by step</h2>
<h3 id="step-1-what-the-volume-copy-actually-does">Step 1: What the volume copy actually does</h3>
<p>The setup: a PostgreSQL database under constant load, four processes committing individual
transactions without pause. While that runs, we copy the volume the way people usually do:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">docker run --rm -v YOUR_VOLUME:/from:ro -v copy:/to alpine:3.22 sh -c <span class="s1">&#39;cp -a /from/. /to/&#39;</span></span></span></code></pre></div>
</div>
<p>Then we start a second container on the copy. Four runs:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">run 1 (cp -a)      copy  1.8 s | container running | id in copy 1858  original 1959   lost 101
</span></span><span class="line"><span class="cl">run 2 (cp -a)      copy  1.3 s | container running | id in copy 2521  original 2595   lost 74
</span></span><span class="line"><span class="cl">run 3 (cp -a)      copy  1.3 s | container running | id in copy 3125  original 3202   lost 77
</span></span><span class="line"><span class="cl">slow copy          copy 70.2 s | container running | id in copy 3892  original 10856  lost 6964</span></span></code></pre></div>
</div>
<p>Every copy starts. Every one recovers cleanly. The base table holds exactly its two million
rows in all four cases, and <code>bt_index_check</code> reports no problem with the index. The log
shows nothing but what follows any power cut:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">LOG:  database system was not properly shut down; automatic recovery in progress
</span></span><span class="line"><span class="cl">LOG:  redo starts at 0/1BA1C50
</span></span><span class="line"><span class="cl">LOG:  redo done at 0/1668A1B8</span></span></code></pre></div>
</div>
<p>And yet committed transactions are missing: orders a customer received a confirmation for.
The copy is a consistent state from the moment it <strong>started</strong>, not from the moment it
finished.</p>
<p>The reason lies in <code>cp</code> itself: it reads the files one after another, not in a single
instant. PostgreSQL finds a write-ahead log state from the early phase of the copy and
replays exactly up to there. Everything committed afterwards sits in files that do not
match that state and gets discarded. The database does precisely the right thing. It just
cannot know that it is restoring the state from a minute ago rather than from just now.</p>
<p>The fourth line is the real warning. When we artificially stretched the same copy to 70
seconds, almost 7,000 transactions were gone. The loss grows with the duration, and a real
20 GB database does not copy itself in one and a half seconds. MariaDB behaves the same
way: <code>InnoDB: Starting crash recovery</code>, data complete, 106 transactions gone.</p>
<div class="not-prose my-6 rounded-lg border-l-4 p-4 border-amber-400 bg-amber-50 dark:border-amber-700 dark:bg-amber-900/20">
  <p class="mb-1 flex items-center gap-2 font-semibold text-slate-900 dark:text-white">
    <span aria-hidden="true">⚠️</span>Why this is worse than a broken backup
  </p>
  <div class="prose-kitchen text-sm">A broken backup announces itself at the first restore attempt. This one restores cleanly,
starts, and the application runs. Only weeks later does somebody ask where their data went,
and by then the earlier state has long dropped out of the retention window.</div>
</div>
<h3 id="step-2-the-dump-as-the-counter-design">Step 2: The dump as the counter-design</h3>
<p>A dump is the opposite: a state from a single transaction, with a defined point in time, and
it tells you when it fails. The PostgreSQL variants, measured against the same database:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">pg_dumpall raw          1.3 s  108M
</span></span><span class="line"><span class="cl">pg_dumpall | zstd       1.3 s   22M
</span></span><span class="line"><span class="cl">pg_dump -Fc             2.8 s   21M
</span></span><span class="line"><span class="cl">mariadb-dump | zstd     2.0 s  5.0M</span></span></code></pre></div>
</div>
<p>The second line is remarkable: compression costs no extra time and saves 80 percent.
The dump is slower than <code>zstd</code> can compress anyway.</p>
<p>The choice between the two PostgreSQL tools comes down to a detail that gets expensive in
an emergency. <code>pg_dump</code> saves one database, <code>pg_dumpall</code> the whole cluster, <strong>including
roles</strong>. We put a <code>readonly</code> role into the test database, the kind every second application
creates for reports, and restored both dumps:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">pg_dumpall:   CREATE ROLE readonly  → included
</span></span><span class="line"><span class="cl">pg_dump -Fc:  CREATE ROLE readonly  → not included</span></span></code></pre></div>
</div>
<p>Restoring from the <code>-Fc</code> dump then looks like this:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">pg_restore: error: could not execute query: ERROR:  role &#34;readonly&#34; does not exist
</span></span><span class="line"><span class="cl">Command was: GRANT SELECT ON TABLE public.bestellungen TO readonly;
</span></span><span class="line"><span class="cl">pg_restore: warning: errors ignored on restore: 2</span></span></code></pre></div>
</div>
<p>The data is there, the permissions are not: the app starts and is allowed to do nothing.
That is why the script below uses <code>pg_dumpall</code>. <code>pg_dump -Fc</code> is the better format
(smaller, selective, restorable in parallel), but then you need
<code>pg_dumpall --globals-only</code> alongside it.</p>
<h3 id="step-3-the-script-that-finds-your-databases-itself">Step 3: The script that finds your databases itself</h3>
<p>The core is an idea worth remembering: you do not maintain a list of your databases, the
script reads it from Docker, via the image name of the running container.</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">docker ps --filter <span class="nv">status</span><span class="o">=</span>running --format <span class="s1">&#39;{{.Names}}\t{{.Image}}&#39;</span></span></span></code></pre></div>
</div>
<p>This has a side effect that eliminates an entire class of problem: because the dump tool
runs inside the container, client and server versions always match. The classic
annoyance of a host <code>pg_dump</code> being too old for the server cannot occur in the first place.</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/bin/bash
</span></span></span><span class="line"><span class="cl"><span class="c1"># Backs up every running database container the script finds by itself.</span>
</span></span><span class="line"><span class="cl"><span class="nb">set</span> -euo pipefail
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nv">TARGET</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">TARGET</span><span class="k">:-</span><span class="p">/var/backups/databases</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">KEEP_DAYS</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">KEEP_DAYS</span><span class="k">:-</span><span class="nv">14</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">EXCLUDE</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">EXCLUDE</span><span class="k">:-</span><span class="p">^</span>$<span class="si">}</span><span class="s2">&#34;</span>          <span class="c1"># e.g. &#34;old-testdb|zammad-backup&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">MIN_BYTES</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">MIN_BYTES</span><span class="k">:-</span><span class="nv">1024</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">PING_URL</span><span class="o">=</span><span class="s2">&#34;</span><span class="si">${</span><span class="nv">PING_URL</span><span class="k">:-</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nv">POSTGRES_RE</span><span class="o">=</span><span class="s1">&#39;(^|/)postgres(:|$)&#39;</span>
</span></span><span class="line"><span class="cl"><span class="nv">MARIADB_RE</span><span class="o">=</span><span class="s1">&#39;(^|/)(mariadb|mysql)(:|$)&#39;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nv">failed</span><span class="o">=</span><span class="m">0</span>
</span></span><span class="line"><span class="cl">log<span class="o">()</span> <span class="o">{</span> <span class="nb">printf</span> <span class="s1">&#39;%s  %s\n&#39;</span> <span class="s2">&#34;</span><span class="k">$(</span>date +%H:%M:%S<span class="k">)</span><span class="s2">&#34;</span> <span class="s2">&#34;</span><span class="nv">$*</span><span class="s2">&#34;</span><span class="p">;</span> <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">containers<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  docker ps --filter <span class="nv">status</span><span class="o">=</span>running --format <span class="s1">&#39;{{.Names}}\t{{.Image}}&#39;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">    <span class="p">|</span> awk -F<span class="s1">&#39;\t&#39;</span> -v <span class="nv">re</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$1</span><span class="s2">&#34;</span> <span class="s1">&#39;$2 ~ re {print $1}&#39;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">    <span class="p">|</span> grep -Ev <span class="s2">&#34;</span><span class="nv">$EXCLUDE</span><span class="s2">&#34;</span> <span class="o">||</span> <span class="nb">true</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">back_up<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  <span class="nb">local</span> <span class="nv">name</span><span class="o">=</span><span class="nv">$1</span> <span class="nv">ext</span><span class="o">=</span><span class="nv">$2</span><span class="p">;</span> <span class="nb">shift</span> <span class="m">2</span>
</span></span><span class="line"><span class="cl">  <span class="nb">local</span> <span class="nv">dir</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$TARGET</span><span class="s2">/</span><span class="nv">$name</span><span class="s2">&#34;</span><span class="p">;</span> mkdir -p <span class="s2">&#34;</span><span class="nv">$dir</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="nb">local</span> <span class="nv">out</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$dir</span><span class="s2">/</span><span class="k">$(</span>date +%Y%m%d-%H%M<span class="k">)</span><span class="s2">.</span><span class="nv">$ext</span><span class="s2">.zst&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="nb">local</span> <span class="nv">tmp</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$out</span><span class="s2">.incomplete&#34;</span> size
</span></span><span class="line"><span class="cl">  <span class="k">if</span> <span class="s2">&#34;</span><span class="nv">$@</span><span class="s2">&#34;</span> <span class="p">|</span> zstd -q -o <span class="s2">&#34;</span><span class="nv">$tmp</span><span class="s2">&#34;</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">    <span class="nv">size</span><span class="o">=</span><span class="k">$(</span>stat -c%s <span class="s2">&#34;</span><span class="nv">$tmp</span><span class="s2">&#34;</span><span class="k">)</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="o">[</span> <span class="s2">&#34;</span><span class="nv">$size</span><span class="s2">&#34;</span> -lt <span class="s2">&#34;</span><span class="nv">$MIN_BYTES</span><span class="s2">&#34;</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">      log <span class="s2">&#34;FAILED  </span><span class="nv">$name</span><span class="s2"> – dump is only </span><span class="nv">$size</span><span class="s2"> bytes, discarded&#34;</span>
</span></span><span class="line"><span class="cl">      rm -f <span class="s2">&#34;</span><span class="nv">$tmp</span><span class="s2">&#34;</span><span class="p">;</span> <span class="nv">failed</span><span class="o">=</span>1<span class="p">;</span> <span class="k">return</span>
</span></span><span class="line"><span class="cl">    <span class="k">fi</span>
</span></span><span class="line"><span class="cl">    mv <span class="s2">&#34;</span><span class="nv">$tmp</span><span class="s2">&#34;</span> <span class="s2">&#34;</span><span class="nv">$out</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">    log <span class="s2">&#34;OK      </span><span class="nv">$name</span><span class="s2"> – </span><span class="k">$(</span>numfmt --to<span class="o">=</span>iec <span class="s2">&#34;</span><span class="nv">$size</span><span class="s2">&#34;</span><span class="k">)</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="k">else</span>
</span></span><span class="line"><span class="cl">    log <span class="s2">&#34;FAILED  </span><span class="nv">$name</span><span class="s2"> – dump failed&#34;</span>
</span></span><span class="line"><span class="cl">    rm -f <span class="s2">&#34;</span><span class="nv">$tmp</span><span class="s2">&#34;</span><span class="p">;</span> <span class="nv">failed</span><span class="o">=</span><span class="m">1</span>
</span></span><span class="line"><span class="cl">  <span class="k">fi</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># A helper container reads SQLite straight from the app&#39;s data directory (step 5).</span>
</span></span><span class="line"><span class="cl">back_up_sqlite<span class="o">()</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">  <span class="nb">local</span> <span class="nv">name</span><span class="o">=</span><span class="nv">$1</span> <span class="nv">dir</span><span class="o">=</span><span class="nv">$2</span> <span class="nv">file</span><span class="o">=</span><span class="nv">$3</span>
</span></span><span class="line"><span class="cl">  back_up <span class="s2">&#34;</span><span class="nv">$name</span><span class="s2">&#34;</span> db docker run --rm -v <span class="s2">&#34;</span><span class="nv">$dir</span><span class="s2">&#34;</span>:/data alpine:3.22 sh -c <span class="se">\
</span></span></span><span class="line"><span class="cl">    <span class="s2">&#34;test -f /data/</span><span class="nv">$file</span><span class="s2"> &amp;&amp; apk add -q --no-cache sqlite &gt;/dev/null \
</span></span></span><span class="line"><span class="cl"><span class="s2">     &amp;&amp; sqlite3 /data/</span><span class="nv">$file</span><span class="s2"> \&#34;VACUUM INTO &#39;/tmp/backup.db&#39;\&#34; &amp;&amp; cat /tmp/backup.db&#34;</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">for</span> c in <span class="k">$(</span>containers <span class="s2">&#34;</span><span class="nv">$POSTGRES_RE</span><span class="s2">&#34;</span><span class="k">)</span><span class="p">;</span> <span class="k">do</span>
</span></span><span class="line"><span class="cl">  back_up <span class="s2">&#34;</span><span class="nv">$c</span><span class="s2">&#34;</span> sql docker <span class="nb">exec</span> <span class="s2">&#34;</span><span class="nv">$c</span><span class="s2">&#34;</span> sh -c <span class="s1">&#39;pg_dumpall -U &#34;${POSTGRES_USER:-postgres}&#34;&#39;</span>
</span></span><span class="line"><span class="cl"><span class="k">done</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">for</span> c in <span class="k">$(</span>containers <span class="s2">&#34;</span><span class="nv">$MARIADB_RE</span><span class="s2">&#34;</span><span class="k">)</span><span class="p">;</span> <span class="k">do</span>
</span></span><span class="line"><span class="cl">  back_up <span class="s2">&#34;</span><span class="nv">$c</span><span class="s2">&#34;</span> sql docker <span class="nb">exec</span> <span class="s2">&#34;</span><span class="nv">$c</span><span class="s2">&#34;</span> sh -c <span class="se">\
</span></span></span><span class="line"><span class="cl">    <span class="s1">&#39;MYSQL_PWD=&#34;${MARIADB_ROOT_PASSWORD:-${MYSQL_ROOT_PASSWORD:-}}&#34; \
</span></span></span><span class="line"><span class="cl"><span class="s1">     exec mariadb-dump --all-databases --single-transaction --quick -uroot&#39;</span>
</span></span><span class="line"><span class="cl"><span class="k">done</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># One line per SQLite app: name, data directory, file (step 5)</span>
</span></span><span class="line"><span class="cl"><span class="c1"># back_up_sqlite linkding /opt/linkding/data db.sqlite3</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">find <span class="s2">&#34;</span><span class="nv">$TARGET</span><span class="s2">&#34;</span> -type f -name <span class="s1">&#39;*.zst&#39;</span> -mtime +<span class="s2">&#34;</span><span class="nv">$KEEP_DAYS</span><span class="s2">&#34;</span> -delete
</span></span><span class="line"><span class="cl">find <span class="s2">&#34;</span><span class="nv">$TARGET</span><span class="s2">&#34;</span> -type f -name <span class="s1">&#39;*.incomplete&#39;</span> -mtime +1 -delete
</span></span><span class="line"><span class="cl">find <span class="s2">&#34;</span><span class="nv">$TARGET</span><span class="s2">&#34;</span> -mindepth <span class="m">1</span> -type d -empty -delete
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="o">[</span> <span class="s2">&#34;</span><span class="nv">$failed</span><span class="s2">&#34;</span> -ne <span class="m">0</span> <span class="o">]</span><span class="p">;</span> <span class="k">then</span>
</span></span><span class="line"><span class="cl">  log <span class="s2">&#34;At least one dump failed.&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="o">[</span> -n <span class="s2">&#34;</span><span class="nv">$PING_URL</span><span class="s2">&#34;</span> <span class="o">]</span> <span class="o">&amp;&amp;</span> curl -fsS -m <span class="m">10</span> --retry <span class="m">3</span> <span class="s2">&#34;</span><span class="nv">$PING_URL</span><span class="s2">/fail&#34;</span> &gt;/dev/null <span class="o">||</span> <span class="nb">true</span>
</span></span><span class="line"><span class="cl">  <span class="nb">exit</span> <span class="m">1</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span><span class="line"><span class="cl">log <span class="s2">&#34;All dumps fine.&#34;</span>
</span></span><span class="line"><span class="cl"><span class="o">[</span> -n <span class="s2">&#34;</span><span class="nv">$PING_URL</span><span class="s2">&#34;</span> <span class="o">]</span> <span class="o">&amp;&amp;</span> curl -fsS -m <span class="m">10</span> --retry <span class="m">3</span> <span class="s2">&#34;</span><span class="nv">$PING_URL</span><span class="s2">&#34;</span> &gt;/dev/null <span class="o">||</span> true</span></span></code></pre></div>
</div>
<p>Save the script as <code>/usr/local/bin/db-backup.sh</code> and make it executable:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">chmod +x /usr/local/bin/db-backup.sh
</span></span><span class="line"><span class="cl">db-backup.sh</span></span></code></pre></div>
</div>
<p>One run on the test server:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">20:54:20  OK      dbtest-shop-db-1 – 22M
</span></span><span class="line"><span class="cl">20:54:22  OK      dbtest-wiki-db-1 – 5.0M
</span></span><span class="line"><span class="cl">20:54:22  All dumps fine.</span></span></code></pre></div>
</div>
<p>Two MariaDB flags deserve an explanation, because they are often missing.
<code>--single-transaction</code> takes the dump from a single read transaction. That is the
difference between &ldquo;state as of 3 a.m.&rdquo; and &ldquo;somewhere between 3:00 and 3:02&rdquo;, and it does
not lock the tables. The flag only applies to transactional engines, though; an old MyISAM
or Aria table lying around is not covered. <code>--quick</code> fetches rows one at a time instead of
loading a whole table into memory first. On a small server that is the difference between
a dump and an OOM kill.</p>
<h3 id="step-4-three-details-the-obvious-version-gets-wrong">Step 4: Three details the obvious version gets wrong</h3>
<p>The version of this script you find everywhere looks almost identical, but it has three
places that get expensive in daily use. We reproduced all three on our test server.</p>
<p><strong>A failed dump looks exactly like a successful one.</strong> We ran a database container in which
the server is not reachable, precisely the situation shortly after a restart. Without
<code>set -o pipefail</code> this happens:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">pg_dumpall: error: connection to server on socket … failed: No such file or directory
</span></span><span class="line"><span class="cl">Pipeline exit status: 0
</span></span><span class="line"><span class="cl">-rw-r----- 1 root root 13 20261004-2100.sql.zst
</span></span><span class="line"><span class="cl">Decompressed content: 0 bytes</span></span></code></pre></div>
</div>
<p><code>zstd</code> writes a formally valid file with zero content, and because only the last command in
a pipeline counts, the shell reports success. Fourteen nights later the retention rule has
deleted the last real dump. The same scenario with the script above:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">20:55:05  FAILED  empty-db – dump failed
</span></span><span class="line"><span class="cl">20:55:07  OK      dbtest-shop-db-1 – 22M
</span></span><span class="line"><span class="cl">20:55:09  OK      dbtest-wiki-db-1 – 5.0M
</span></span><span class="line"><span class="cl">20:55:09  At least one dump failed.
</span></span><span class="line"><span class="cl">Exit status: 1</span></span></code></pre></div>
</div>
<p>The remaining databases are still backed up, because one broken container must not abort
the night. But the run ends with 1, and nothing half-finished is left in the target
directory. That
is what writing to <code>.incomplete</code> and renaming only after the size check is for.</p>
<p><strong>Otherwise the password sits in the process list.</strong> The common variant passes it with
<code>-p</code>:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">sh -c mariadb-dump --all-databases --single-transaction -uroot -p&lt;THE-REAL-PASSWORD&gt;</span></span></code></pre></div>
</div>
<p>That line is visible not only inside the container but on the host; <code>ps</code> is enough. The
fact that <code>mariadb-dump</code> overwrites its own arguments with <code>-px xxxxxxxx</code> afterwards does
not help, because the surrounding <code>sh -c</code> line keeps the plain text. The version above sets
<code>MYSQL_PWD</code> inside the container from its own environment variable; the command line
then contains nothing but variable names, and <code>ps</code> no longer finds the password:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Processes whose command line contains the password: none</span></span></code></pre></div>
</div>
<div class="not-prose my-6 rounded-lg border-l-4 p-4 border-herb-400 bg-herb-50 dark:border-herb-700 dark:bg-herb-900/20">
  <p class="mb-1 flex items-center gap-2 font-semibold text-slate-900 dark:text-white">
    <span aria-hidden="true">🧑‍🍳</span>MARIADB_PWD does not exist
  </p>
  <div class="prose-kitchen text-sm">The obvious guess would be <code>MARIADB_PWD</code>, matching <code>MARIADB_ROOT_PASSWORD</code>. That variable
does not exist, and the dump then runs into <code>Access denied … (using password: NO)</code>. Even on
MariaDB 12 and 13 the environment variable is still called <code>MYSQL_PWD</code>.</div>
</div>
<p><strong>The retention rule deletes directories too.</strong> The usual line reads
<code>find … -mtime +14 -delete -type f</code>. The problem is the order: <code>-delete</code> is evaluated
before <code>-type f</code> is even tested.</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">-mtime +14 -delete -type f     → the empty old directory is GONE
</span></span><span class="line"><span class="cl">-type f -mtime +14 -delete     → the directory survives</span></span></code></pre></div>
</div>
<p>In daily use this goes unnoticed for a long time, because a directory refreshes its
modification time as soon as a file inside it disappears. But a container that has been
gone for two weeks takes its backup directory with it.</p>
<h3 id="step-5-sqlite-where-container-discovery-cannot-work">Step 5: SQLite, where container discovery cannot work</h3>
<p>Vaultwarden, Linkding, FreshRSS and many others need no database container at all: their
database is a file inside the app container. A script searching by image name will never
find it.</p>
<p>And simply copying the file is tricky for another reason. In WAL mode, which practically
all of these apps use, freshly written data initially lives only in the <code>notes.db-wal</code>
file next to it:</p>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">-rw-r--r--  64786432  notes.db
</span></span><span class="line"><span class="cl">-rw-r--r--     32768  notes.db-shm
</span></span><span class="line"><span class="cl">-rw-r--r--   5805112  notes.db-wal</span></span></code></pre></div>
</div>
<p>Back up only <code>notes.db</code> and you leave those 5.8 MB behind. The copy still passes
<code>PRAGMA integrity_check</code> with <code>ok</code>, so the check does not protect you here.</p>
<p>The documented route would be <code>sqlite3 .backup</code>. Under sustained load, however, we got a
surprise:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">timeout <span class="m">30</span> sqlite3 /data/notes.db <span class="s2">&#34;.backup /tmp/copy.db&#34;</span></span></span></code></pre></div>
</div>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Terminated
</span></span><span class="line"><span class="cl">Exit status: 143   duration: 30 s</span></span></code></pre></div>
</div>
<p>Nothing. SQLite&rsquo;s backup interface starts over whenever somebody writes to the source
database. With enough write traffic it never reaches the end. On a first attempt the same
command hung for seventeen minutes before we aborted it.</p>
<p><code>VACUUM INTO</code> is reliable instead. It reads in a single read transaction and therefore
completes even under load:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">sqlite3 /data/notes.db <span class="s2">&#34;VACUUM INTO &#39;/tmp/copy.db&#39;;&#34;</span></span></span></code></pre></div>
</div>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Exit status: 0   duration: 0 s
</span></span><span class="line"><span class="cl">Rows in the copy: 595910
</span></span><span class="line"><span class="cl">Integrity:        ok</span></span></code></pre></div>
</div>
<p>App images rarely ship the <code>sqlite3</code> for this, though. Vaultwarden, Linkding and FreshRSS
all come without it. That is why in the script above a short-lived helper container reads
the database straight from the app&rsquo;s data directory while the app keeps running. The
function <code>back_up_sqlite</code> contains two details:</p>
<ul>
<li><code>VACUUM INTO</code> aborts with <code>output file already exists</code> if the target file is already
there. So the copy is made in the <code>/tmp</code> of the helper container, which disappears
afterwards. Otherwise every second night would fail.</li>
<li>The <code>test -f</code> up front is needed because on a typo in the file name <code>sqlite3</code> silently
creates a new, empty database in the app&rsquo;s directory.</li>
</ul>
<p>Each app gets one line at the marked spot in the script, right below the MariaDB loop, with
name, data directory and file. Not at the end: there it would sit behind the <code>exit 1</code> of the
failure case.</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">back_up_sqlite linkding /opt/linkding/data db.sqlite3
</span></span><span class="line"><span class="cl">back_up_sqlite vaultwarden /root/vaultwarden/vw-data db.sqlite3</span></span></code></pre></div>
</div>
<p>The helper container fetches <code>sqlite</code> from the Alpine package sources on every run. Without
network access the line fails loudly like any other dump.</p>
<div class="not-prose my-6 rounded-lg border-l-4 p-4 border-sky-300 bg-sky-50 dark:border-sky-800 dark:bg-sky-900/20">
  <p class="mb-1 flex items-center gap-2 font-semibold text-slate-900 dark:text-white">
    <span aria-hidden="true">ℹ️</span>What about Redis, Valkey, MongoDB?
  </p>
  <div class="prose-kitchen text-sm"><p>Redis and Valkey run in almost every self-hosting recipe as a cache or session store.
Both may be lost; the application rebuilds them. A backup only pays off if you
deliberately use Redis as a database, for queues say; then you need persistence enabled and
back up the <code>dump.rdb</code>.</p>
<p>MongoDB fits the pattern: another loop over <code>(^|/)mongo(:|$)</code> with
<code>mongodump --archive</code>. Mongo does not appear anywhere in this site&rsquo;s recipes, though, which
is why the block is not in the script above: we do not put untested code in a tutorial.</p>
</div>
</div>
<h3 id="step-6-the-restore-timed">Step 6: The restore, timed</h3>
<p>A dump nobody has ever restored is not a backup but a hope. All three routes played through
once, each into a fresh, empty container:</p>
<table>
	<thead>
			<tr>
					<th>Route</th>
					<th>Duration</th>
					<th>Result</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>pg_dumpall</code> → <code>psql</code></td>
					<td>6.1 s</td>
					<td>2,000,000 rows, role <code>readonly</code> back</td>
			</tr>
			<tr>
					<td><code>pg_restore -j4</code> from <code>-Fc</code></td>
					<td>6.2 s</td>
					<td>2,000,000 rows, roles missing (exit 1)</td>
			</tr>
			<tr>
					<td><code>mariadb</code> from <code>mariadb-dump</code></td>
					<td>7.8 s</td>
					<td>1,000,000 rows</td>
			</tr>
	</tbody>
</table>
<p>PostgreSQL:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">zstd -dc YOUR_DUMP.sql.zst <span class="p">|</span> docker <span class="nb">exec</span> -i YOUR_CONTAINER psql -U YOUR_USER -d postgres</span></span></code></pre></div>
</div>
<p>MariaDB:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">zstd -dc YOUR_DUMP.sql.zst <span class="p">|</span> docker <span class="nb">exec</span> -i -e <span class="nv">MYSQL_PWD</span><span class="o">=</span><span class="s2">&#34;YOUR_PASSWORD&#34;</span> YOUR_CONTAINER mariadb -uroot</span></span></code></pre></div>
</div>
<p>A dump made with <code>--all-databases</code> also contains the user table of the server it came from.
Until the container&rsquo;s next restart the new container&rsquo;s root password still applies, after
that the old one from the backup. So set <code>MARIADB_ROOT_PASSWORD</code> in the new container to the
old password right away, or you lock yourself out at the first restart.</p>
<div class="not-prose my-6 rounded-lg border-l-4 p-4 border-amber-400 bg-amber-50 dark:border-amber-700 dark:bg-amber-900/20">
  <p class="mb-1 flex items-center gap-2 font-semibold text-slate-900 dark:text-white">
    <span aria-hidden="true">⚠️</span>Wait for real readiness, not for the ping
  </p>
  <div class="prose-kitchen text-sm"><p>Our first restore attempt failed with <code>ERROR 1045 (28000): Access denied for user 'root'@'localhost'</code> even though the password was correct. During first-time initialisation
MariaDB starts a temporary server that already answers <code>mariadb-admin ping</code>; the final
one with your password is not running yet. So wait for a real query instead:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="k">until</span> docker <span class="nb">exec</span> -e <span class="nv">MYSQL_PWD</span><span class="o">=</span><span class="s2">&#34;</span><span class="nv">$PW</span><span class="s2">&#34;</span> YOUR_CONTAINER mariadb -uroot -e <span class="s2">&#34;SELECT 1;&#34;</span> &gt;/dev/null 2&gt;<span class="p">&amp;</span>1<span class="p">;</span> <span class="k">do</span> sleep 2<span class="p">;</span> <span class="k">done</span></span></span></code></pre></div>
</div>
<p>That takes a few seconds, between 6 and 14 on our test server.</p>
</div>
</div>
<h3 id="step-7-every-night-and-then-off-the-server">Step 7: Every night, and then off the server</h3>
<p>A <a href="/en/tutorials/systemd-basics/">systemd timer</a> starts the script, and <code>PING_URL</code> reports
the outcome to <a href="/en/tutorials/healthchecks-monitor-jobs/">Healthchecks</a>. A run that never
happens goes unnoticed otherwise. First the service:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">systemctl edit --full --force db-backup.service</span></span></code></pre></div>
</div>
<div class="sk-code">
  <span class="sk-code-head">INI</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ini" data-lang="ini"><span class="line"><span class="cl"><span class="k">[Unit]</span>
</span></span><span class="line"><span class="cl"><span class="na">Description</span><span class="o">=</span><span class="s">Database dumps</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">[Service]</span>
</span></span><span class="line"><span class="cl"><span class="na">Type</span><span class="o">=</span><span class="s">oneshot</span>
</span></span><span class="line"><span class="cl"><span class="na">Environment</span><span class="o">=</span><span class="s">PING_URL=https://YOUR_HEALTHCHECKS_URL/YOUR_UUID</span>
</span></span><span class="line"><span class="cl"><span class="na">ExecStart</span><span class="o">=</span><span class="s">/usr/local/bin/db-backup.sh</span></span></span></code></pre></div>
</div>
<p>Then the timer. It runs at 2 a.m., before the 2:30 a.m. restic job from the
<a href="/en/tutorials/restic-backups/">restic tutorial</a>, so the fresh dumps are included:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">systemctl edit --full --force db-backup.timer</span></span></code></pre></div>
</div>
<div class="sk-code">
  <span class="sk-code-head">INI</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-ini" data-lang="ini"><span class="line"><span class="cl"><span class="k">[Unit]</span>
</span></span><span class="line"><span class="cl"><span class="na">Description</span><span class="o">=</span><span class="s">Database dumps every night before the restic job</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">[Timer]</span>
</span></span><span class="line"><span class="cl"><span class="na">OnCalendar</span><span class="o">=</span><span class="s">*-*-* 02:00:00</span>
</span></span><span class="line"><span class="cl"><span class="na">Persistent</span><span class="o">=</span><span class="s">true</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">[Install]</span>
</span></span><span class="line"><span class="cl"><span class="na">WantedBy</span><span class="o">=</span><span class="s">timers.target</span></span></span></code></pre></div>
</div>
<p><code>Persistent=true</code> catches up on a run the server missed while it was off. Enable and check:</p>
<div class="sk-code">
  <span class="sk-code-head">Terminal</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">systemctl <span class="nb">enable</span> --now db-backup.timer
</span></span><span class="line"><span class="cl">systemctl list-timers db-backup.timer</span></span></code></pre></div>
</div>
<div class="sk-code">
  <span class="sk-code-head">Ausgabe</span>
  <div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">NEXT                        LEFT LAST PASSED UNIT            ACTIVATES
</span></span><span class="line"><span class="cl">Tue 2026-12-01 02:00:00 CET   8h -         - db-backup.timer db-backup.service</span></span></code></pre></div>
</div>
<p>If a dump fails, the service ends as <code>failed</code> and <code>systemctl --failed</code> lists it, on top of
the failure ping to Healthchecks.</p>
<p>That completes the division of labour: this script produces consistent dumps on the server,
<a href="/en/tutorials/restic-backups/">restic</a> encrypts them and moves them elsewhere. Without the
second half your backup sits on the same disk as the original and shares its fate.</p>
<h2 id="when-things-go-wrong">When things go wrong</h2>
<div class="troubleshoot not-prose">
<p><strong>The dump is there, but all permissions are missing after the restore.</strong> You backed up
with <code>pg_dump</code> instead of <code>pg_dumpall</code>. Roles and passwords belong to the cluster, not to
the individual database, so they are not in a database dump. Either switch to <code>pg_dumpall</code>
or additionally back up <code>pg_dumpall --globals-only</code> and restore that first.</p>
<p><strong><code>FAILED … dump is only 846 bytes, discarded</code> for a new app.</strong> The database is still
empty, and its dump stays below the <code>MIN_BYTES</code> minimum of 1024 bytes the script uses to
catch empty dumps. Once the app has created its tables, the dump is larger. Until then,
exclude the container via <code>EXCLUDE</code> rather than lowering the threshold.</p>
<p><strong><code>pg_dumpall: error: … role &quot;root&quot; does not exist</code>.</strong> The container does not set
<code>POSTGRES_USER</code>, so the script passes an empty user name and the client falls back to the
Linux user. The <code>${POSTGRES_USER:-postgres}</code> in the script above catches exactly this.</p>
<p><strong><code>sqlite3 .backup</code> never returns.</strong> The backup interface restarts whenever the source
database is written to, and under sustained load it never reaches the end. Use
<code>VACUUM INTO</code> instead – it reads in a single transaction and finishes in a fraction of a
second even under load.</p>
<p><strong><code>ERROR 1045 (28000): Access denied</code> when restoring into a fresh MariaDB container.</strong> The
container is still initialising, running a temporary server without your password. Do not
wait for <code>mariadb-admin ping</code>, wait for a real query to succeed.</p>
<p><strong>The container with <code>postgres:18</code> no longer starts after switching.</strong> As of PostgreSQL 18
the volume belongs at <code>/var/lib/postgresql</code> rather than <code>/var/lib/postgresql/data</code> – the
images store data per major version under <code>18/docker</code>. With the old path startup aborts,
even with an empty volume. If you already have data in the old layout, move it via a dump
rather than just bending the mount – step by step in PostgreSQL major upgrade in a container <span class="text-sm text-slate-500 dark:text-slate-400">(Tutorial expected in January 2027)</span>.</p>

</div>
<p class="mt-3 text-sm text-slate-500 dark:text-slate-400">
  Your problem is not listed?
  <a href="/en/errors/" class="font-medium text-paprika-700 hover:underline dark:text-paprika-400">Search all error messages →</a>
</p>

<h2 id="maintenance--backups">Maintenance &amp; backups</h2>
<p><strong>Once a quarter: a real restore.</strong> Not &ldquo;the dump is 22 MB, it&rsquo;ll be fine&rdquo;, but a fresh
container, the dump in, count the rows. Going by the numbers above that costs under ten
seconds of compute and is the only proof there is.</p>
<p><strong>With every new app: take one look.</strong> The script finds Postgres, MariaDB and MySQL by
itself, but not an app with SQLite or a more exotic database. A glance at <code>docker ps</code>
after setting it up is enough, and for SQLite you add a <code>back_up_sqlite</code> line.</p>
<p><strong>Match retention to reality.</strong> Fourteen days is a good default as long as somebody notices
within fourteen days that something is missing. For data that is rarely touched that is
optimistic. Then add a monthly copy to the
<a href="/en/tutorials/restic-backups/">restic retention rule</a>.</p>
<p>And the most uncomfortable habit last: after each night, do not check whether the backup
ran, but whether it did not. That is what the exit status is for, and what the ping is
for. A silent script is not a good sign, it is no sign at all.</p>
]]></content:encoded></item></channel></rss>