Sunday 9 June 2013

MySQL database backup to Email and to FTP Server

Database is very essential and important part of any dynamic system or application. We need to take backup of database time to time. This can be done using the user interface like phpmyadmin on regular basis and stored in a secure location.
The manual process is time consuming and there are change when you forget. We can automate this by making a php application that will backup mysql database and upload it to server using FTP and also mail the backup to user.
Process:
1) Mysql database dump can be taken using command below
mysqldump –opt -h (hostname) -u (database_userbname) –password=(database_password) )(databasename) | gzip > (filename);
This command can be executed using php function passthru() and it will generate backup file of database.

2) Now upload backup to FTP
To upload database to FTP php has some functions like ftp_connect(), ftp_login(), ftp_nb_fput(). using these function backup can be uploaded to FTP.

3) Email backup
Email backup using php function mail().

Click here to download the script.