Maybe this is too far off, but I'd love it:

A great security enhancement would be to store the backed up database in a GnuPG encrypted file. Since there is the GnuPG API, maybe that would be possible.

Comments

Anonymous’s picture

I use PGP since many years for secure mailing and encrypted data storage. Currently I use cron with a script to email me a backup of the database to an external mailbox. It would be very helpful to manage this with the GnuPG API.

GnuPG is a free implementation of the OpenPGP standard and can be freely used, modified and distributed under the terms of the GNU General Public License.

yan’s picture

Currently I use cron with a script to email me a backup of the database to an external mailbox.

Actually that's exactly what I am looking for.

Anonymous’s picture

Dependencies: cron, GnuPG (default install: /usr/bin/gpg, default keyrings/public key: /home/username/.gnupg*1

Filename: backup_mysql.php

$datestamp = date("Y-m-d"); // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING VARIABLES TO MATCH YOUR SETUP */
$dbuser = "username"; // Database username
$dbpwd = "password"; // Database password
$dbname = "database"; // Database name. Use --all-databases if you have more than one
$filename= "filename_$datestamp.sql.pgp"; // The name (and optionally path) of the pgp file
$to = "mail@example.com"; // Email address to send file to
$from = "mail@example.com"; // Email address message will show as coming from.
$subject = "[SITENAME] MySQL backup $datestamp"; // Subject of email
$recipient = "0x1234567890ABCDEF"; // PGP Key ID

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gpg --no-secmem-warning --recipient $recipient --always-trust --encrypt --output $filename"; // Generate the dump and encrypt it
$result = passthru($command);

$attachmentname = array_pop(explode("/", $filename)); // If a path was included, strip it out for the attachment name

$message = "Encrypted MySQL database backup file $attachmentname attached.";
$mime_boundary = "<<<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));

$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"\r\n";

$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";

mail($to, $subject, $content, $headers);

unlink($filename); //delete the pgp file from the server

Cron command: php -q /home/username/etc/backup_mysql.php*2

*1 You have to import at least one public key ($recipient) into your public keyring, e.g. with cPanel.
*2 A path outside your webroot.

namzezam’s picture

For to make secure sending of the db backup by mail, how to to use the public key of (user=1) admin generated via gnupg_user from gnupg module and by which public key to encrypt the $file (by gnupg_encrypt or gnupg_exec?) before compressing $file in the function backup_migrate_file_compress which is in modules/backup_migrate/includes/files.inc ?

This should end up only with few lines of code but result in full testing with nice functionality for both modules: the gnupg and the backup_migrate.

namzezam’s picture

When considering for security issue to put in php.ini
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

then the above solution are not relevant!!!

Instead we can use pecl.php.net/package/gnupg

For more please see these:
http://devzone.zend.com/article/3753-Using-GnuPG-with-PHP
http://www.php-editors.com/php_manual/ref.gnupg.html

ronan’s picture

I have added support for the AES encryption API (http://drupal.org/project/aes). I have also left open the interface to allow GPG API integration in the future. I did not implement that API as the release notes claim that decryption is not yet supported in that module and I do not want to allow people to create backups that they cannot restore.

I'm no crypto expert, so I'd love it of some of the smart people on this thread could take a look at the AES implementation and see if it is meaningful at all (it exports, it imports and in between the files look garbled, that's about all I can tell). And maybe give me a quick primer on GnuPG: how it works, how I would implement it, what are it's advantages, etc. I would like an encryption solution that does not depend on Backup and Migrate to decrypt (so that backups are not totally useless if your site isn't running). Are there standard tools/techniques to decrypt GnuPG encrypted files?

Also, to namzezam's concern about shell_exec, passthru etc. I agree in principle. I've tried to make this module not dependent on command line utilities and the like (how much shorter would this thing be if I could just use `sqldump | gzip > $file` and be done :)) but I'm willing to compromise that for advanced functionality like this. I figure the people who are most interested in encryption are savvy enough to modify php.ini and are smart enough to know the implications.

Anonymous’s picture

I would like an encryption solution that does not depend on Backup and Migrate to decrypt (so that backups are not totally useless if your site isn't running).

This solution is OpenPGP/GnuPG because I read in the module: Encrypted files can only be restored by Backup and Migrate and only on sites with the same encryption key.

Are there standard tools/techniques to decrypt GnuPG encrypted files?

GnuPG or any other OpenPGP program/implementation. OpenPGP is the standard. I use PGP for Windows (PGP Desktop).

If you haven't installed an OpenPGP compliant product you can not decrypt the backup files and in this case you don't need the GnuPG API at all.

As long as the GnuPG module isn't implemented in Backup and Migrate I prefer the slim version 1.x of the module and the solution posted in comment #3. It works perfectly for me.

ronan’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

I think after 5 years I have to come to the conclusion that I don't have the bandwidth to add and maintain this task. It should be doable as a 3rd party module though so if anyone wants to step up to the plate I'd be happy to lend guidance.