473,378 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Mysql db restore

I am trying to set up one server with multiple mysql dbs. This script will get the mysqldump files from the ftp server and based on the name of the server in the file, it will create the corresponding db on the server.

this is the code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. #ftp the most recent file to the ftp server
  4. $dir1="/patrol_media/patadmin/sqlbak";
  5. $dir2="d:\\cacti\\backup";
  6.  
  7. $ftp_server = "logsvr2.dowjones.net"; // Address of FTP server.
  8. $ftp_user_name = "username";
  9. $ftp_user_pass = "password";
  10. $destination_file = "/patrol_media/patadmin/sqlbak";
  11.  
  12. $conn_id = ftp_connect($ftp_server);
  13.  
  14. $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
  15.  
  16. $contents = ftp_nlist($conn_id, $dir1);
  17.  
  18. foreach($contents as $key => $file)
  19. {
  20.     $extension = substr(strrchr($file, $dir1), 1);
  21.     echo "KEY $key => $file $extension";
  22.     // try to download $server_file and save to $local_file
  23.     if (ftp_get($conn_id, "$dir2\\$extension", $file, FTP_BINARY))
  24.         {
  25.             echo "Successfully written to $dir2\\$extension";
  26.             $BackupFile="$dir2\\$extension";
  27.             // move file into OLD folder
  28.  
  29.         }
  30. }
  31. $username = "cactiuser";
  32. $password = "cactiuser";
  33. $dc = opendir($dir2);
  34. while ($file = readdir($dc)) {
  35.     $server_name = strtok($file, "_");
  36.     $database=$server_name;
  37.  
  38.     $con = mysql_connect("localhost","cactiuser","cactiuser");
  39.     if (!$con)
  40.       {
  41.       die('Could not connect: ' . mysql_error());
  42.       }if (mysql_query("DROP DATABASE $database",$con))
  43.           {
  44.               echo "Database dropped";
  45.           }
  46.       else
  47.           {
  48.               echo "Error dropping database: " . mysql_error();
  49.           }
  50.       if (mysql_query("CREATE DATABASE $database",$con))
  51.           {
  52.                       echo "Database created";
  53.         }
  54.              else
  55.         {
  56.                       echo "Error creating database: " . mysql_error();
  57.           }
  58.  
  59.       mysql_close($con);
  60.  
  61.  
  62.     echo $restoreCommand = 'mysql -u {$username} -p{$password} {$database} < {$file}';
  63.  
  64.     echo "{$file}\n";
  65.  
  66.     if (exec($restoreCommand)) {return true;}
  67.         else {return false;}
  68.  
  69. }
  70.  
  71. ?>
  72.  
when I ran this, I am getting this error:
Expand|Select|Wrap|Line Numbers
  1. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.' at line 1
  2. Error creating database: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
  3.  to use near '.' at line 1
  4. mysql -u {$username} -p{$password} {$database} < {$file}.
  5.  
Can anybody help?
May 8 '08 #1
5 1756
I found one problem where $fn variable was not defined. I changed it to $file and I am getting a more meaningfull error now:
Expand|Select|Wrap|Line Numbers
  1. Access denied for user 'cactiuser'@'localhost' to database 'sbkesmgendev01'
  2. Error creating database: Access denied for user 'cactiuser'@'localhost' to database 'sbkesmgendev01'
  3. mysql -u {$username} -p{$password} {$database} < {$file}SBKESMGENDEV01_20080508.sql
  4.  
May 8 '08 #2
the part that ftp gets all files into d:\cacti\backup works. I need to go throught each file and create a mysqldb based on the file name. I would like to do something like this in php. This piece of code works in perl. Can any body help?
Expand|Select|Wrap|Line Numbers
  1. opendir(DIR, "$dir2");
  2. @files = readdir(DIR);
  3. closedir(DIR);
  4.  
  5. foreach $file (@files) {
  6.  
May 8 '08 #3
when I do:

Expand|Select|Wrap|Line Numbers
  1. $dir = opendir("$dir2");//List files in images directory
  2. while (($file = readdir($dir)) !== false){
  3.  
  4. echo "file name is $file\n";
  5. closedir($dir);
  6.  
I am not getting all of the files in that directory. I have two actual files and it is only displaying the first one.

this is the output:

file name is .
file name is ..
file name is SBKESMGENDEV01_20080508.sql

I am missing SBKW2K3ESMPRD4_20080429.sql.

Can anybody tell me why?
May 8 '08 #4
TheServant
1,168 Expert 1GB
Use one post at a time. You have four! You can edit your posts so next to the reply button under each post you will see Edit/Delete. Please use it!
May 8 '08 #5
Atli
5,058 Expert 4TB
Hi.
In your $restoreCommand variable, I assume you are trying to insert the values of those variables into the string?
If so, you should use double-quotes, not single-quotes. When used like this, the variable names will be used, not their values.
Expand|Select|Wrap|Line Numbers
  1. # This will not work...
  2. echo $restoreCommand = 'mysql -u {$username} -p{$password} {$database} < {$file}';
  3.  
  4. # It should be
  5. echo $restoreCommand = "mysql -u {$username} -p{$password} {$database} < {$file}";
  6.  
The code you posted in your last post shouldn't work. You should get a parse-error, due to the fact that there is a single { bracket with no matching } bracket.

After fixing that, all my tests work perfectly.
If it is not showing the second file, it must not be there.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $dir = opendir(".");
  3. while ($file = readdir($dir)) {
  4.     echo "file name is $file\n";
  5. }
  6. closedir($dir);
  7. ?>
  8.  
May 9 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
1
by: Jerry T | last post by:
How can I backup and restore a MySQL DB WITHOUT USING EXEC() MYSQL MYSQLDUMP ?? If I try running exec() and mysqldump, I get Fork warning errors ..
2
by: Thomas Bartkus | last post by:
Is it safe to say that restoring the directory from a backup would that restore all database data and structure? All usr/pwd and GRANT info? Thomas Bartkus
5
by: KC | last post by:
Hello I installed MySQL ver 4.0.20d on a Windows XP Professional PC. I created a user and database that I would like to delete, and at this point I would just like to uninstall MySQL and do a...
7
by: phillip.s.powell | last post by:
We're looking at a GUI interface for our MySQL DB and I am interested in MySQL Administrator, however, one of our requirements is to be able to import/export databases. Is this possible or do I...
0
by: newman | last post by:
Dear all, I have mysql 4.1.11 on my current server, i need my database restore another server.. (another server mysql version is 4.1.11 same.) And now... I just created new my database to new...
5
by: linuxlover992000 | last post by:
I am a newbie in the world of MySQL. In fact I enabled it in my Linux box only because it is required to run WordPress (the blogging software). I was trying to plan ahead and figure out a way to...
3
by: deepstar | last post by:
Hello I have a rather large database (about 102 megabytes when dumped to an SQL file) and need advice on backup software for MySql databases. So far I've only tried Navicat and it takes about 30...
7
by: Paul | last post by:
I recently installed php 4.4.4 using windows binaries on Windows XP Pro. I also installed MySQL 4.1. I usually use Pear DB but I tried MDB2 and it worked fine until a client uses a different...
4
by: Bootstrap Bill | last post by:
I'm looking for a PHP program to backup and restore a mysql database. I'm using Godaddy to host a forum. Their mysql control panel will only restore a database of two megabytes or less. My...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.