{"id":11188,"date":"2012-04-06T14:31:42","date_gmt":"2012-04-06T12:31:42","guid":{"rendered":"https:\/\/www.saasweb.agency\/blog\/how-to-create-automatically-encrypted-mysql-dumps\/"},"modified":"2026-10-27T08:15:54","modified_gmt":"2026-10-27T07:15:54","slug":"how-to-create-automatically-encrypted-mysql-dumps","status":"publish","type":"post","link":"https:\/\/www.saasweb.agency\/en\/blog\/how-to-create-automatically-encrypted-mysql-dumps\/","title":{"rendered":"How to Create Automatically Encrypted MySQL Dumps"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.saasweb.agency\/wp-content\/uploads\/2012\/04\/logo-mysql-110x57-1.png\" alt=\"\" title=\"logo-mysql-110x57\" width=\"110\" height=\"57\" class=\"alignleft size-full wp-image-975\" srcset=\"https:\/\/www.saasweb.agency\/wp-content\/uploads\/2012\/04\/logo-mysql-110x57-1.png 110w, https:\/\/www.saasweb.agency\/wp-content\/uploads\/2012\/04\/logo-mysql-110x57-1-50x26.png 50w, https:\/\/www.saasweb.agency\/wp-content\/uploads\/2012\/04\/logo-mysql-110x57-1-100x52.png 100w\" sizes=\"(max-width: 110px) 100vw, 110px\" \/>Everyone knows that backups are important\u2014whether it is a system failure or you simply want to revert to an older state because an error was made.<br \/>\nBackups are often handled by cron jobs. In most cases, however, a simple MySQL dump is generated without encrypting the content.<br \/>\nIn this article, I describe how to create backups using GPG without using a fixed password. Since there may be multiple database administrators who need to restore backups, every authorized admin should be able to restore the backup using their own passphrase. <\/p>\n<div class=\"code-snippet\">\n#!\/bin\/bash<\/p>\n<p>BACKUP_DIR=\/var\/backup\/auto\/mysql<br \/>\nDATE=$(date -I)<br \/>\nTARGET_DIR=$BACKUP_DIR\/$DATE<br \/>\nmkdir -p $TARGET_DIR<\/p>\n<p>DATABASES=$(mysql -uadmin -B -s -e&#8221;show databases;&#8221;)<\/p>\n<p>for db in $DATABASES; do<br \/>\nTARGET=$TARGET_DIR\/$db.sql.gz.gpg<br \/>\necho -n dumping $db to $TARGET &#8230;<br \/>\nmysqldump -uadmin $db | gzip | gpg -rCAFEBABE -rAFFE4711 -rAFFEFACE -e &#8211; > $TARGET<br \/>\nif [ $PIPESTATUS -eq 0 ]; then echo OK; else echo ERROR; fi<br \/>\ndone\n<\/p><\/div>\n<p>The variable <code>BACKUP_DIR<\/code> specifies where the backups should be stored. Below that, a directory is created with the date in the format YYYY-MM-DD (<code>date -I<\/code>), where the dumps of individual databases are placed. If you want to generate more than one backup per day, you can adjust this command accordingly.  <\/p>\n<p>Example output:<\/p>\n<div class=\"code-snippet\">&#8230;<br \/>\ndumping mysql to \/var\/backup\/auto\/mysql\/2012-04-06\/mysql.sql.gz.gpg &#8230;OK<br \/>\ndumping test to \/var\/backup\/auto\/mysql\/2012-04-06\/test.sql.gz.gpg &#8230;OK<br \/>\n&#8230;<\/div>\n<p>What needs to be adjusted in this script are the KEY\/IDs of the recipients\u2014i.e., who is allowed to decrypt the data later. In this example, three recipients are specified: <\/p>\n<div class=\"code-snippet\">\ngpg -rCAFEBABE -rAFFE4711 -rAFFEFACE\n<\/div>\n<p>Each recipient has the ability to decrypt the backup files. GPG automatically determines the key for the &#8220;decryptor&#8221;: <\/p>\n<div class=\"code-snippet\">\ngpg -d mysql\/2012-04-06\/test.sql.gz.gpg | gunzip | head -n 5<\/p>\n<p>You need a passphrase to unlock the secret key for<br \/>\nuser: &#8220;XZY&#8221;<br \/>\n2048-bit ELG-E key, ID AFFEFACE, created&#8230;<br \/>\nEnter passphrase:<\/p>\n<p>gpg: encrypted with 2048-bit RSA key, ID CAFEBABE&#8230;<br \/>\ngpg: encrypted with 2048-bit ELG-E key, ID AFFE4711&#8230;<br \/>\ngpg: encrypted with 2048-bit ELG-E key, ID AFFEFACE&#8230;\n<\/p><\/div>\n<p>The actual output then appears in STDOUT:<\/p>\n<div class=\"code-snippet\"><code>-- MySQL dump 10.13  Distrib 5.1.56, for redhat-linux-gnu (x86_64)<br \/>\n--<br \/>\n-- Host: localhost    Database: test<br \/>\n-- ------------------------------------------------------<br \/>\n-- Server version       5.1.56<br \/>\n<\/code><\/div>\n<p>If you have not had much experience with GnuPG and the MySQL CLI, you should consider the following points before running the example:<br \/>\n&#8211; Keys of all recipients should be signed\/trusted beforehand so that the backup script does not enter interactive mode.<br \/>\n&#8211; The password for database access is defined by default in the ~\/.my.cnf file in the [client] section.<\/p>\n<p>Have fun testing and implementing it,<br \/>\nC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everyone knows that backups are important\u2014whether it is a system failure or you simply want to revert to an older state because an error was made. Backups are often handled by cron jobs. In most cases, however, a simple MySQL dump is generated without encrypting the content. In this article, I describe how to create \u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[981,983],"tags":[988,1008,1145,1179,1102,1353,1003,1855,1036,1662,1058],"class_list":["post-11188","post","type-post","status-publish","format-standard","hentry","category-programming","category-tutorial","tag-administration","tag-backup","tag-backup-2","tag-database","tag-dump","tag-encryption","tag-gpg","tag-mysql","tag-mysqldump","tag-mysqldump-encrypt","tag-security-2"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/posts\/11188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/comments?post=11188"}],"version-history":[{"count":0,"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/posts\/11188\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/media?parent=11188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/categories?post=11188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.saasweb.agency\/en\/wp-json\/wp\/v2\/tags?post=11188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}