Around last year, I started backing up the SQL databases on my accounts weekly just so if anything happened, I would have a recent copy. This has served me pretty well a few times (ie. saved my a__). However, the backup files soon started to clutter up server space so I had to delete them from time to time.
Now, this is all fine and all except that I’m lazy, and I want an archive of all my backup files. Okay, okay, the other reason is that I want to fill up a Gmail account at least once in my lifetime. So why not? Let’s send the backup files to Gmail!
So I wrote two scripts: A filesplitter that took the backup files and split them into a specified size (10MB attachment is the limit for Gmail) and a PHP emailer script that encoded the attachment in base64 and generated the correct MIME type emails. It’s easy to send mail with PHP using the mail() function, but it’s a lot more difficult trying to send attachments.
I combined this with my original shell script (.sh) that was linked to a crontab that made backups of my databases every week.
It’s working pretty well right now so I thought other people might be interested in the source:
Download (3KB): BackupToEmail_1.0.zip. Written in PHP and 1 shell (SH) script.
Purpose
The included three files work together to back up a database every so often (specified by a crontab), tar and gzips the .sql dump, and sends the tgz to a email address for backup. If the tgz file is too large, a file splitter is employed to keep the file within attachment size limits.
Usage
- Place all files in a specified directory (ie. /home/yourname/www/backup)
- Chmod that directory 777.
- Chmod backup.sh 777 (give it executing permissions +x).
- Edit backup.sh to reflect the database you wish to backup.
- Edit backupToEmail.php to reflect the email address you wish to send the files to. A gmail account is a good choice since it has over 2GB of storage Also change the from address to reflect the domain you are sending emails from.
- Set a crontab to execute backup.sh every period of time. This can beweekly, monthly, or even daily. You can do this in cpanel is you haveaccess to one or issue the command ‘crontab -e’ at command line and addto the crontab file something like:
0 3 * * 0 /home/yourname/www/backup/backup.sh
For the above example, that is set to execute at 3AM every week.
No support is offered for this script. Use at your own risk!
Credits: PHP Manual
License: GPL