<?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/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>MySQL | A DevOps Blog</title><link>https://blog.bsdserver.nl/tag/mysql/</link><description>Hands-on DevOps and homelab content: Kubernetes clusters, self-hosted services, CI/CD pipelines and infrastructure as code, by Patrick de Ruiter of We Build Your Cloud.</description><language>en</language><image><url>https://blog.bsdserver.nl/images/app-icon.svg</url><title>A DevOps Blog</title><link>https://blog.bsdserver.nl/</link></image><lastBuildDate>Wed, 18 Nov 2026 09:00:00 +0100</lastBuildDate><atom:link href="https://blog.bsdserver.nl/tag/mysql/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title>MySQL Backup with Ansible</title><link>https://blog.bsdserver.nl/mysql-backup-with-ansible/</link><guid isPermaLink="false">693b51f179eb54000160a4ac</guid><dc:creator>Patrick de Ruiter</dc:creator><pubDate>Thu, 11 Dec 2025 23:21:21 +0000</pubDate><description>MySQL Backup with Ansible As someone with a lot of technical knowledge, I sometimes forget the finer details, like specific command-line options.</description><category>Automation</category><category>MySQL</category><category>Ansible</category><category>Backup</category><category>DevOps</category><media:content url="https://blog.bsdserver.nl/content/images/2026/01/mysql-backup-ansible.png" medium="image"/><content:encoded><![CDATA[<p>Taking a MySQL backup by hand means remembering the right <code>mysqldump</code> options, the compression step and the copy to another server. Forget one detail and you end up with an empty or missing backup.</p>
<p>I put tasks like this in small Ansible playbooks. This one dumps a MySQL database, compresses it with gzip, and copies it to a backup server with rsync over SSH.</p>
<h2 id="why-a-playbook">Why a playbook? <a class="anchor" data-pagefind-ignore href="#why-a-playbook" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>No forgotten options</strong>: the exact commands are written down once.</li>
<li><strong>Consistency</strong>: every run does the same steps, in the same order.</li>
<li><strong>Shareable</strong>: anyone in the team can run the same backup without missing a step.</li>
</ul>
<h3 id="when-to-use-it">When to use it <a class="anchor" data-pagefind-ignore href="#when-to-use-it" aria-label="Link to this section">#</a></h3>
<ul>
<li><strong>Use it</strong> for ad-hoc backups before a change, or as a simple scheduled backup of a small database.</li>
<li><strong>Skip it</strong> for large databases or point-in-time recovery; a logical dump of the whole database every run does not scale there.</li>
</ul>
<h2 id="how-it-works">How it works <a class="anchor" data-pagefind-ignore href="#how-it-works" aria-label="Link to this section">#</a></h2>
<table>
	<thead>
			<tr>
					<th>Component</th>
					<th>Runs on</th>
					<th>Job</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Play 1: dump</td>
					<td>MySQL server (<code>mysql</code> group)</td>
					<td>Runs <code>mysqldump</code>, compresses to <code>/tmp/backup/&lt;database&gt;.sql.gz</code>.</td>
			</tr>
			<tr>
					<td>Play 2: copy</td>
					<td>Backup server (<code>backup</code> group), delegated to the MySQL server</td>
					<td>rsync pushes <code>/tmp/backup/</code> to the backup server.</td>
			</tr>
	</tbody>
</table>
<p>The flow: <code>mysqldump</code> → <code>gzip</code> → <code>/tmp/backup</code> on the MySQL server → rsync over SSH → <code>backup_dest</code> on the backup server.</p>
<h2 id="the-playbook">The Playbook <a class="anchor" data-pagefind-ignore href="#the-playbook" aria-label="Link to this section">#</a></h2>
<p>Two plays: the first creates the dump on the MySQL server, the second copies it off the server.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nn">---</span><span class="w">
</span></span></span><span class="line"><span class="cl">- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Backup MySQL server</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">hosts</span><span class="p">:</span><span class="w"> </span><span class="l">mysql</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">become</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">tasks</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Create backup directory</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">ansible.builtin.file</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">path</span><span class="p">:</span><span class="w"> </span><span class="l">/tmp/backup</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">state</span><span class="p">:</span><span class="w"> </span><span class="l">directory</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">mode</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;0755&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Dump MySQL database</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">ansible.builtin.shell</span><span class="p">:</span><span class="w"> </span><span class="p">&gt;-</span><span class="sd">
</span></span></span><span class="line"><span class="cl"><span class="sd">        set -o pipefail &amp;&amp;
</span></span></span><span class="line"><span class="cl"><span class="sd">        mysqldump -u{{ mysql_user }} -p{{ mysql_password | quote }} {{ mysql_database }}
</span></span></span><span class="line"><span class="cl"><span class="sd">        | gzip &gt; /tmp/backup/{{ mysql_database }}.sql.gz</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">args</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">executable</span><span class="p">:</span><span class="w"> </span><span class="l">/bin/bash</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">changed_when</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl">- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Copy backup to the backup server</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">hosts</span><span class="p">:</span><span class="w"> </span><span class="l">backup</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">gather_facts</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">tasks</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Copy backup to remote server</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">ansible.posix.synchronize</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">src</span><span class="p">:</span><span class="w"> </span><span class="l">/tmp/backup/</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">dest</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ backup_dest }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">private_key</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ ssh_key }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">delegate_to</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ groups[&#39;mysql&#39;][0] }}&#34;</span><span class="w">
</span></span></span></code></pre></div><h3 id="the-dump-task">The dump task <a class="anchor" data-pagefind-ignore href="#the-dump-task" aria-label="Link to this section">#</a></h3>
<ul>
<li><code>mysqldump ... | gzip</code>: dumps one database and compresses it on the fly. Each run overwrites <code>/tmp/backup/&lt;database&gt;.sql.gz</code>.</li>
<li><code>set -o pipefail</code>: makes the task fail when <code>mysqldump</code> fails (for example on a wrong password). Without it the pipe returns gzip&rsquo;s exit code and you silently end up with an empty backup.</li>
<li><code>executable: /bin/bash</code>: <code>pipefail</code> is a bash option; the default <code>/bin/sh</code> may not support it.</li>
<li><code>mysql_password | quote</code>: escapes the password for the shell, so special characters don&rsquo;t break the command.</li>
<li><code>changed_when: true</code>: a new dump is always a change.</li>
</ul>
<h3 id="the-copy-task">The copy task <a class="anchor" data-pagefind-ignore href="#the-copy-task" aria-label="Link to this section">#</a></h3>
<p><code>synchronize</code> always copies from the host it runs on to the inventory host. So the second play targets the backup server and delegates the task to the MySQL server:</p>
<ul>
<li>rsync runs on the MySQL server and pushes <code>/tmp/backup/</code> to the backup server.</li>
<li>It connects as the backup server&rsquo;s <code>ansible_user</code>, with the <code>private_key</code> you specify.</li>
</ul>
<h2 id="variables">Variables <a class="anchor" data-pagefind-ignore href="#variables" aria-label="Link to this section">#</a></h2>
<p>Before running the playbook, you&rsquo;ll need to update the following variables with the appropriate values:</p>
<table>
	<thead>
			<tr>
					<th>Variable</th>
					<th>What it is</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>mysql_user</code></td>
					<td>The MySQL user with permissions to perform the backup.</td>
			</tr>
			<tr>
					<td><code>mysql_password</code></td>
					<td>The password for the MySQL user.</td>
			</tr>
			<tr>
					<td><code>mysql_database</code></td>
					<td>The name of the MySQL database you want to back up.</td>
			</tr>
			<tr>
					<td><code>backup_dest</code></td>
					<td>The path on the remote server where the backup file will be copied.</td>
			</tr>
			<tr>
					<td><code>backup_user</code></td>
					<td>The username for the remote server. Set it as <code>ansible_user</code> on the backup server in your inventory (e.g. <code>backup01 ansible_user=&quot;{{ backup_user }}&quot;</code>); <code>remote_user</code> is a reserved name in Ansible and the <code>synchronize</code> module has no such option.</td>
			</tr>
			<tr>
					<td><code>ssh_key</code></td>
					<td>The path, on the MySQL server, of the SSH key used to connect to the remote server.</td>
			</tr>
	</tbody>
</table>
<blockquote>
<p><strong>Note:</strong> Don&rsquo;t put credentials in a plaintext readable file. Put them in Ansible Vault instead!</p>
</blockquote>
<h2 id="running-the-playbook">Running the Playbook <a class="anchor" data-pagefind-ignore href="#running-the-playbook" aria-label="Link to this section">#</a></h2>
<p>To run the playbook, use the following command:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">ansible-playbook -i &lt;inventory_file&gt; playbook.yml
</span></span></code></pre></div><p>Where <code>&lt;inventory_file&gt;</code> is the file listing your target servers (a <code>mysql</code> group and a <code>backup</code> group), and <code>playbook.yml</code> is the name of your playbook.</p>
<h2 id="security-considerations">Security Considerations <a class="anchor" data-pagefind-ignore href="#security-considerations" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>Vault the credentials</strong>: <code>mysql_password</code> and the backup user belong in Ansible Vault.</li>
<li><strong>Password on the command line</strong>: <code>-p&lt;password&gt;</code> is visible in the process list while the dump runs. A MySQL option file avoids that.</li>
<li><strong>Backup file permissions</strong>: <code>/tmp/backup</code> is mode <code>0755</code>, so local users can read the dump. Tighten it on shared servers.</li>
<li><strong>SSH key scope</strong>: the key on the MySQL server only needs write access to <code>backup_dest</code>.</li>
</ul>
<h2 id="automate-it">Automate it <a class="anchor" data-pagefind-ignore href="#automate-it" aria-label="Link to this section">#</a></h2>
<p>The Ansible roles and Terraform code that build this setup are on GitHub: <a href="https://github.com/pderuiter/blog-mysql-backup" rel="noopener">pderuiter/blog-mysql-backup</a>. It creates a MySQL server and a backup server on Proxmox and runs this playbook as roles; fill in <code>terraform.tfvars</code> and the vault, then run <code>terraform apply</code> and <code>ansible-playbook site.yml</code>.</p>
<h2 id="before-you-rely-on-it">Before You Rely On It <a class="anchor" data-pagefind-ignore href="#before-you-rely-on-it" aria-label="Link to this section">#</a></h2>
<ul>
<li>Test the playbook in a non-production environment first, including a restore: a backup you never restored is not a backup.</li>
<li>Make sure the remote server has enough storage space for the backups.</li>
<li>Make sure you have permission to write to the destination directory.</li>
</ul>
]]></content:encoded></item></channel></rss>