views:

749

answers:

1

I want to setup a cron job to run so that it will automatically backup my MySQL database, while the database is running, and then FTP that backup to my backup server.

I assume I can do this using a bash script.

Anyone know of a good way to accomplish this?

Thanks in advance.

+2  A: 

This is a very simple approach using the lftp command line ftp client:

backup.sh:

mysqldump -f [database] | gzip > /backup/[database].dump.gz
lftp -f /backup/lftp.script

lftp.script:

open backup.ftp.example.com
user [username] [password]
cd /backup
mv webflag.dump.gz.8 webflag.dump.gz.9
mv webflag.dump.gz.7 webflag.dump.gz.8
mv webflag.dump.gz.6 webflag.dump.gz.7
mv webflag.dump.gz.5 webflag.dump.gz.6
mv webflag.dump.gz.4 webflag.dump.gz.5
mv webflag.dump.gz.3 webflag.dump.gz.4
mv webflag.dump.gz.2 webflag.dump.gz.3
mv webflag.dump.gz.1 webflag.dump.gz.2
mv webflag.dump.gz webflag.dump.gz.1

Note: This approach has a number of issues:

  • ftp is unencryped, so anyone who is able to sniff the network, is able to see both the password and the database data. Piping it through gpg -e [key] can be used to encrypt the dump but the ftp passwords stays unencrypted (sftp, scp are better alternatives)
  • if someone hacks the database server, he can use the user information in this script to access the ftp server and depending on rights delete the backups (this has happened in the real world: http://seclists.org/fulldisclosure/2009/Jun/0048.html)
nhnb
+1 Though you might need to supply server/user/password flags with mysqldump as well.
Tinister
You shouldn't ftp backups from the live webserver to the backup server. Reason: Your live webserver is very public, and therefore a greater target for getting compromised. If this happens, you lose all your backups as well. Instead, your backup server should ftp into the live webserver for database dumps and download them. The backup server should not be on a publicly accessible net, so even if someone totally owns your webserver, backups are still secure.
davethegr8
I am aware of that issue, that's why I pointed it out in my posting. The two largest companies which offer server hosting in Germany, however, only provide a backup facility in this way.
nhnb