Backup file and send to email

Richard G

Verified User
Joined
Jul 6, 2008
Messages
12,744
Location
Maastricht
On the SMF forum I just found somebody with an interesting question.
He would like to make a backup of his database and send it to his email address.

Personally this makes me curious. I'm glad I god admin rights, because with reseller backup, you can make automatic backups to your NAS.
As a user, I guess you have to make an FTP script and run this via a cronjob to get your backups to your NAS for example.

But now I became curious on how to send a database backup to an email address.
Probably also via a script with a "mailto" command. But is this possible or would a php script be needed to make use of the php mail() command or something?
 
I suppose it have to be done in BASH from console (well console not really needed i think) using the mail function.

Not sure about the command to use for attach a file in an e-mail from command line, but definitly can be done.. it may be not suggested on big size database, but for little ones it should work without many issue.

For sure the mysql backup line would be:

Code:
mysqldump -uUSERNAME -pPASSWORD DATABASE_NAME > export.sql

With this file you need just to attach to an email and send it.

Googling for 5 seconds gave me some infos, the command should be:

Code:
mysqldump -uUSERNAME -pPASSWORD DATABASE_NAME > export.sql && gzip -c export.sql && mail -s "MySQL DB" -a export.sql.gz EMAIL_ADDRESS

I've not tested this, is 4:30 am here and i'm just back home from a party.. so.. try it (it should not cause issue)... :)

Regards
 
Back
Top