2017-09-27 08:21:45 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2017-09-27 08:23:22 +02:00
|
|
|
# Create a key pair on the backup system or on your computer:
|
2017-09-27 08:21:45 +02:00
|
|
|
# $ openssl req -x509 -sha256 -nodes -newkey rsa:4096 -keyout mysqldump.priv.pem -out mysqldump.pub.pem
|
|
|
|
# Do not transport your private key to any other systems.
|
|
|
|
#
|
|
|
|
# And copy the public key to your server:
|
|
|
|
# $ scp mysqldump.pub.pem mysqldump@your.server.tld:/home/mysqldump/key/
|
|
|
|
#
|
2017-09-27 08:23:22 +02:00
|
|
|
# If needed, decryption is done like this:
|
2017-09-27 08:21:45 +02:00
|
|
|
# $ openssl smime -decrypt -in dump-cli.sql.enc -binary -inform DEM -inkey mysqldump.priv.pem -out dump-cli.sql
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2017-09-27 09:07:27 +02:00
|
|
|
CLIENT=$1
|
2017-09-27 08:21:45 +02:00
|
|
|
MYSQL_CREDENTIALS='/var/www/'$CLIENT'/credentialsmysql.cnf'
|
|
|
|
BACKUP_DIR='/var/www/'$CLIENT'/backup/'
|
|
|
|
BACKUP_DIR=$BACKUP_DIR$(date +"%Y%m%d_%H%M%S")
|
2017-09-27 09:07:27 +02:00
|
|
|
PUCLIC_KEY=$2
|
2017-09-27 08:21:45 +02:00
|
|
|
|
|
|
|
if [ -e $MYSQL_CREDENTIALS ]; then
|
|
|
|
mkdir $BACKUP_DIR
|
|
|
|
echo -e "\e[32mDumping database to $BACKUP_DIR...\e[0m"
|
2017-09-27 09:34:06 +02:00
|
|
|
mysqldump --defaults-file=$MYSQL_CREDENTIALS --databases $CLIENT > $BACKUP_DIR/$CLIENT.sql
|
2017-09-27 08:21:45 +02:00
|
|
|
|
|
|
|
echo -e "\e[32mEncrypting database...\e[0m"
|
2017-09-27 10:31:27 +02:00
|
|
|
openssl smime -encrypt -binary -text -aes256 -in $BACKUP_DIR/$CLIENT.sql -out $BACKUP_DIR/$CLIENT.sql.enc -outform DER $PUCLIC_KEY
|
2017-09-27 08:21:45 +02:00
|
|
|
|
2017-09-27 09:34:06 +02:00
|
|
|
rm $BACKUP_DIR/$CLIENT.sql
|
2017-09-27 08:21:45 +02:00
|
|
|
else
|
|
|
|
echo -e "\e[93mDatabase backup not configured. Skipping.\e[0m"
|
|
|
|
fi
|