I want a PHP based solution to backup database (only data and not code) of a remote server and download the file. I know that Shell based solutions are better for doing such
I Think you should install phpmyadmin on your server, this will allow you to access your database from work/school/cafe/etc, MySQL-workbench is more advanced and gives you more features so you can deal with changing the structure and editing any rows/columns, relations, and much more, look at phpmyadmin's features it has most if not all.
I really recommended phpMyAdmin it has many SQL features to help you deal with everything when it comes to the MySQL database if you are using innoDB then you get even more features such as relation-ships between tables.
Everything you listed above is included in phpMyAdmin, if you are running debian or Debian-based system simply run:
root@debian:~ # aptitude install phpmyadmin
root@arch:~ # pacman -S phpmyadmin
BTW: if you are not using Apache or lighttpd for the http-server you will need to read through the conf files for phpmyadmin and then write up the required conf script for phpmyadmin to work with your http-server.
MySQL workbench visurally see what you are doing with your database. http://diariolinux.com/wp-content/uploads/2008/08/wb51linuxpreview2a.png
BTW: Use <ctrl>+<G>
to forward engineer a database, It took me a while to find out for to do this.
use DBI;
my $user = "username"; # MySQL Username
my $pass = "xxxx"; # MySQL Password
my $host = "localhost"; # MySQL Host
my $mydb = "zzzz"; # MySQL Database
my $file = "test.sql"; # Import file
my $sqlServer = "mysql"; # What sql-server are we using, oracle/mysql/etc
# I would use the following method to configure it, though the above works fine too.
($user,$pass,$host,$mydb,$file,sqlServer) = (
"username", # MySQL Username
"password", # MySQL Password
"localhost", # MySQL Host
"myDB", # MySQL Database
"test.sql", # Imported file
"mysql" # What sql-server are we using, oracle/mysql/etc
);
# Now lets connect to the MySQL server.
my $dbh = DBI->connect("DBI:$sqlServer:$mydb:$host",$user,$pass)or die DBI->errstr();
# Lets now open the .sql file.
open(INPUT,$file);
# Now lets run each sql-statement.
while ($line = <INPUT>){
print $line;
$dbh->do($line);
print "Query failed (run manually):\n$line\n\n ". $dbh->errstr()."\n" if $dbh->errstr();
}
# Now close the file.
close(INPUT);
In my own experience, relying on PHP based backup solution is VERY bad idea. If you are dealing with large backups, PHP can easily fail (due to timeouts, memory consumption or something undefined).
We have been successfully using Bacula (http://www.bacula.org/en/) for years now on our un*x servers. Did our own backup scripts which do whatever we want to do. You need to have shell access to server off course.
If you need simple backup php based solution, you are best with writing your own. :)
PHPMyAdmin is a great solution in general but may be overkill if you only ever want to do backups. If you want a universal solution then absolutely don't rely on exec being available. PHP combined with the right SQL queries (as you have noted in your point 3.) can provide you with all the information you need and will always work.