Should you be taking backups of more things than you currently are? Well now there is no excuse. Just use this 50 odd line script and receive your encrypted backups attached via email.
Download:
The script is available at Github - https://github.com/splitice/encrypted-email-backup
Features:
Backup a MySQL Database
mysqldump -u [username] -p[password] [database_name] | bash /opt/backup-script/backup-sql-encrypt.sh backup /opt/backup-script/backup.key "backup.sql" "[email protected]" "Database Backup"
Decrypt the MySQL Database backup to file
cat backup.sql.gz.enc | ./backup.sh decrypt /opt/backup-script/backup.key > backup.sql
Decrypt the MySQL Database backup to a MySQL Database
cat backup.sql.gz.enc | ./backup.sh decrypt /opt/backup-script/backup.key | mysql -u root -p[root_password] [database_name]
Backup a folder on the filesystem
Restore a folder on the filesystem
Additional Information
Why encrypt your backups?
Do you trust your email provider (Google, Microsoft etc)? Even if you do (damn!) just having your backups in your email provides another possible avenue of data-leakage (if your email is compromised, so are your site backups). Encrypted backups are useless without a copy of your encryption key which should not be available in your email.
Why receive your backups via email?
Most people check their emails frequently, hell - for most of us it is our primarily channel of communication in the connected world. By receiving your backups via email you are forced to check them daily, and will be quick to notice if they stop arriving.
Why send an email on failure?
Why not, its important you know when something changes and the backup was unable to be run. Often these scripts are the kind you run for years without modification. Its important to know if they stop running, and hence your system ceases to be backed up regularly.
How frequently should I back up?
This depends on your system. Think about it in terms of how much can you data can you afford to loose.
What about my mail server disk space?
Delete old backups. We do this via a cusomized version of the automation script found here: http://www.labnol.org/internet/gmail-auto-purge/27605/
What if my backup is already compressed?
Remove the gzip executions from the script, or else there will be a small overhead for double compression.
I want to do something slightly differently...
You are welcome to... the script is Open Source. I encourage you to fork it on Github. I want no rights. If your changes are high quality and useful for others, submit a pull request and I'll take a look and see about merging.
Download:
The script is available at Github - https://github.com/splitice/encrypted-email-backup
Features:
- Supports any backup format, just pipe it into the backup script. Use one script for all your backups.
- Backups are stored in a remote location, a remote mail server. This is unlikely to fail at the same time as your server being backed up, and should be secured with a different password to prevent simultaneous compromise.
- Backups are encrypted with AES-256 encryption with a key that is not transmitted to the remote location. This encryption is
- An email is sent on backup failure, successful and failed backups are easily distinguishable through the subject prefix. These can also be easily used in automatic filing/filtering system's to ensure it comes to an administrators attention.
- All the software used in the process is Opensource, this means it is unlikely to be backdoored (atleast not by me), that it is an open format and that it is likely available in the package manager of your distribution.
- Create the backup encryption key. Create a file at an appropriate location, this key can be of any length (longer better) and should be backed up in a different location in case of data-loss. If you loose this key you will be unable to decrypt your backups. You can use your favorite text editor to create this key file.
nano /opt/backup-script/backup.key
- From your backup user (i.e www-data) open the crontab to add a cron job
Code:crontab -e
- Add the backup cron job, to run it daily use @daily, to run it hourly @hourly etc. This script will be similar to the following, you will need to pipe your own backup script (tar of files, mysqldump etc) into the backup script. Be sure to supply your own email address, and your own encryption key.
Code:@daily /opt/backup-script/take_backup | bash /opt/backup-script/backup-sql-encrypt.sh backup /opt/backup-script/backup.key "backup" "[email protected]" "Backup"
- Download Backup Email attachment on to a linux server with a copy of the backup.sh script, and the key file.
- Execute the backup script in decryption mode, pipe in the file, pipe out the backup
Code:cat backup.enc | backup.sh decrypt /opt/backup-script/backup.key > backup
- Restore the backup as appropriate for your software. See the examples section for examples using MySQL.
Backup a MySQL Database
mysqldump -u [username] -p[password] [database_name] | bash /opt/backup-script/backup-sql-encrypt.sh backup /opt/backup-script/backup.key "backup.sql" "[email protected]" "Database Backup"
Decrypt the MySQL Database backup to file
cat backup.sql.gz.enc | ./backup.sh decrypt /opt/backup-script/backup.key > backup.sql
Decrypt the MySQL Database backup to a MySQL Database
cat backup.sql.gz.enc | ./backup.sh decrypt /opt/backup-script/backup.key | mysql -u root -p[root_password] [database_name]
Backup a folder on the filesystem
Code:
cd /var/www && tar c test-site-folder.com | bash /opt/backup-script/backup-sql-encrypt.sh backup /opt/backup-script/backup.key "backup.tar" "[email protected]" "Site Data Backup"
Code:
cd /var/www && cat backup.tar.enc > bash /opt/backup-script/backup-sql-encrypt.sh restore /opt/backup-script/backup.key | tar xc
Why encrypt your backups?
Do you trust your email provider (Google, Microsoft etc)? Even if you do (damn!) just having your backups in your email provides another possible avenue of data-leakage (if your email is compromised, so are your site backups). Encrypted backups are useless without a copy of your encryption key which should not be available in your email.
Why receive your backups via email?
Most people check their emails frequently, hell - for most of us it is our primarily channel of communication in the connected world. By receiving your backups via email you are forced to check them daily, and will be quick to notice if they stop arriving.
Why send an email on failure?
Why not, its important you know when something changes and the backup was unable to be run. Often these scripts are the kind you run for years without modification. Its important to know if they stop running, and hence your system ceases to be backed up regularly.
How frequently should I back up?
This depends on your system. Think about it in terms of how much can you data can you afford to loose.
What about my mail server disk space?
Delete old backups. We do this via a cusomized version of the automation script found here: http://www.labnol.org/internet/gmail-auto-purge/27605/
What if my backup is already compressed?
Remove the gzip executions from the script, or else there will be a small overhead for double compression.
I want to do something slightly differently...
You are welcome to... the script is Open Source. I encourage you to fork it on Github. I want no rights. If your changes are high quality and useful for others, submit a pull request and I'll take a look and see about merging.