View previous topic :: View next topic |
Author |
Message |
stevem
Joined: 15 Mar 2003 Posts: 368
|
Posted: Fri May 16, 2003 10:20 am Post subject: Quick Backup |
|
|
There is no doubt that phpMyAdmin is the best way of working with b2's database, but, as is clear from some of the posts, some users would like a quick way of backing up.
Here is how to do it, using code I have adapted from http://www.phpbuilder.com/mail/php-general/2003022/1611.php
Copy the following code into a new file called b2backup.php and upload to the main b2 directory:
Code: | <?php
require_once ('b2config.php');
$filename = "b2_backup_" . date("y-m-d");
header("Content-Disposition: filename=$filename.sql");
header("Content-Type: application/force-download");
$cmd = "/usr/bin/mysqldump -q -u $dbusername -p$dbpassword -h $dbhost $dbname";
exec($cmd, $sql);
for($i = 0; $i < count($sql); $i++) {
echo $sql[$i] . "\r\n";
}
?> |
Now add this line to b2-include/menutop.txt, just before ***
Code: | 10 b2backup.php Backup |
Please note: You must put a tab (not spaces) between 10 & b2backup.up and before Backup
Upload the files and that's it! You will find a new link called Backup when logging in. Click on it and choose Save this file to disk.
You may wish to improve this by:
1. password protecting the file to stop others downloading the database by just using the URL
2. writing code to use a MySQL query to restore the database
I hope this is of some use.
Last edited by stevem on Sun Feb 22, 2004 1:19 pm; edited 2 times in total |
|
Back to top |
|
 |
stevem
Joined: 15 Mar 2003 Posts: 368
|
Posted: Fri May 16, 2003 11:53 am Post subject: |
|
|
I have added some extra code which helps with security. It checks that the user is logged in and has user level 10 before allowing the backup to take place. Unauthorised users typing in the URL should just get a blank page.
I would very much appreciate improvements to the additional code - it works for me but there are no guarantees!
b2backup.php Code: | <?php
require_once ('b2config.php');
require_once($b2inc."/b2functions.php");
dbconnect();
get_currentuserinfo();
if (!($user_login)) exit;
if ($user_level!=10) exit;
$filename = "b2_backup_" . date("y-m-d");
header("Content-Disposition: filename=$filename.sql");
header("Content-Type: application/force-download");
$cmd = "/usr/bin/mysqldump -q -u $dbusername -p$dbpassword -h $dbhost $dbname";
exec($cmd, $sql);
for($i = 0; $i < count($sql); $i++) {
echo $sql[$i] . "\r\n";
}
?> |
|
|
Back to top |
|
 |
annia
Joined: 09 Apr 2002 Posts: 18
|
Posted: Tue Jun 03, 2003 7:54 am Post subject: |
|
|
my file had 0 bytes.. o_O |
|
Back to top |
|
 |
beachgerl
Joined: 14 Apr 2003 Posts: 37 Location: San Diego
|
Posted: Sat Dec 13, 2003 6:10 am Post subject: |
|
|
I am using your hack. How do I upload this backup to new database on a different web host? I'm transferring all of my entries from old host to the new one. I see that it's zipped as .mysql file so how do I unzip this and set it up on the new database the way it was set up on the old database? Do I need to set up fields first before I unzip the file? I'd appreciate for any feedback on this. Will this work with b2evo?
Thanks! |
|
Back to top |
|
 |
stevem
Joined: 15 Mar 2003 Posts: 368
|
Posted: Sat Dec 13, 2003 11:50 am Post subject: |
|
|
On your new web host you need to create the database and user, but there's no need to create any tables in it. You now need to execute the sql file. How you do this depends on your host. Mine provides a copy of phpMyAdmin. You may be able to install your own copy of phpMyAdmin. It is freely available from http://www.phpmyadmin.net/home_page/ and I recommend it very highly.
In phpMyAdmin you go to the database, click on SQL and run the query by locating the sql file on your machine. Very quickly it will create all the tables and put the data in them. Beautifully simple
It is possible to write a restore program in b2 to do this but any errors would corrupt the database so it's best to do it via phpMyAdmin.
I'm not familiar with b2evo. But the process above is quite independent of the blog and just uses MySQL to recreate the database. If b2evo is then happy to read a b2 database (which would be necessary anyway when upgrading from b2 to b2evo) then there should be no problem.
Does all of this make sense ? |
|
Back to top |
|
 |
beachgerl
Joined: 14 Apr 2003 Posts: 37 Location: San Diego
|
Posted: Sat Dec 13, 2003 4:48 pm Post subject: |
|
|
stevem wrote: | On your new web host you need to create the database and user, but there's no need to create any tables in it. You now need to execute the sql file. How you do this depends on your host. Mine provides a copy of phpMyAdmin. You may be able to install your own copy of phpMyAdmin. It is freely available from http://www.phpmyadmin.net/home_page/ and I recommend it very highly.
In phpMyAdmin you go to the database, click on SQL and run the query by locating the sql file on your machine. Very quickly it will create all the tables and put the data in them. Beautifully simple
It is possible to write a restore program in b2 to do this but any errors would corrupt the database so it's best to do it via phpMyAdmin.
I'm not familiar with b2evo. But the process above is quite independent of the blog and just uses MySQL to recreate the database. If b2evo is then happy to read a b2 database (which would be necessary anyway when upgrading from b2 to b2evo) then there should be no problem.
Does all of this make sense ? |
Thank you very much! I didn't realize it'd be that simple. Yes, my new host has phpMyAdmin so that would make things much easier from what you described. I will worry about b2evo later after I get the b2 up and running because I was told it's easy to upgrade to b2evo anyway. Again, thanks for the explanation. |
|
Back to top |
|
 |
billy
Joined: 05 Jul 2003 Posts: 40
|
Posted: Sat Jan 03, 2004 12:49 am Post subject: |
|
|
this is a awesome hack, i have it installed. it would be much better if someone can make it so we can upload the db through b2 [if possible] woul dmake it better like you said. but i'm glad you thought of this hack in the first place! |
|
Back to top |
|
 |
Smokey.Joe
Joined: 22 Jun 2003 Posts: 3
|
Posted: Fri Jan 23, 2004 10:17 pm Post subject: RE: Great Hack |
|
|
It seems like it works, however it downloaded the file to the computer I was using when I logged in. Is there a simple way to have it default and save it on the server itself? I guess either way it works, and it's very useful. Thanks again.
Smokey.Joe |
|
Back to top |
|
 |
stevem
Joined: 15 Mar 2003 Posts: 368
|
Posted: Fri Jan 23, 2004 10:59 pm Post subject: Re: RE: Great Hack |
|
|
Smokey.Joe wrote: | Is there a simple way to have it default and save it on the server itself? |
I would have thought it is best to save to another computer in case the server gets trashed, but I can see your point if you are logging in on a public computer.
I have only quickly tested this but it should work
Code: | <?php
require_once ('b2config.php');
require_once($b2inc."/b2functions.php");
dbconnect();
get_currentuserinfo();
if (!($user_login)) exit;
if ($user_level!=10) exit;
$path="put path here relative or absolute but must be chmod 777";
$filename = $path."/b2_backup_" . date("y-m-d") . ".sql";
$cmd = "/usr/bin/mysqldump -q -u $dbusername -p$dbpassword -h $dbhost $dbname";
exec($cmd.">".$filename, $sql);
?> |
Put the path to save it to in the line beginning $path above. Do ensure that the directory is writable to ie chmod 777.
This is very basic, so there is no feedback as there is when you download, but the code could be fleshed out with HTML telling you what has happened.
WARNING!
This backup file that you have saved contains all the passwords and details of your database, so, at the very least, save it to a password protected directory. Please be careful not to give anyone the opportunity to ruin your blog. |
|
Back to top |
|
 |
Cyberian75
Joined: 26 Sep 2002 Posts: 1285 Location: Oregon
|
Posted: Mon Jan 26, 2004 2:52 am Post subject: |
|
|
Very useful indeed. Thanks!  _________________ Michael P.
 |
|
Back to top |
|
 |
beachgerl
Joined: 14 Apr 2003 Posts: 37 Location: San Diego
|
Posted: Tue Feb 10, 2004 6:31 am Post subject: Re: trying to unzip backup |
|
|
Hi again. I'm trying to unzip the backup file via phpmyadmin. Here's the error message I got:
Code: |
Error
SQL-query :
CREATE TABLE bgcategories(
cat_ID int( 4 ) NOT NULL AUTO_INCREMENT ,
cat_name tinytext NOT NULL ,
KEY cat_ID( cat_ID )
) TYPE = MYISAM
MySQL said:
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 ' |
What went wrong? I hope I'm not losing my volumes of journal entries.  |
|
Back to top |
|
 |
stevem
Joined: 15 Mar 2003 Posts: 368
|
Posted: Tue Feb 10, 2004 4:31 pm Post subject: |
|
|
My backup says: Code: | CREATE TABLE b2categories (
cat_ID int(4) NOT NULL auto_increment,
cat_name tinytext NOT NULL,
KEY cat_ID (cat_ID)
) TYPE=MyISAM; | and there are some differences between this and yours. Have you copied and pasted the error message or retyped it? I'm not quite sure why yours says bgcategories rather than b2categories and the spaces are different.
But the major problem which could well cause MySQL to give an error is the missing ; at the end of the last line. If that's the problem, re-edit the SQL file and put back the ; and try again. |
|
Back to top |
|
 |
beachgerl
Joined: 14 Apr 2003 Posts: 37 Location: San Diego
|
Posted: Tue Feb 10, 2004 9:38 pm Post subject: |
|
|
stevem wrote: | My backup says: Code: | CREATE TABLE b2categories (
cat_ID int(4) NOT NULL auto_increment,
cat_name tinytext NOT NULL,
KEY cat_ID (cat_ID)
) TYPE=MyISAM; | and there are some differences between this and yours. Have you copied and pasted the error message or retyped it? I'm not quite sure why yours says bgcategories rather than b2categories and the spaces are different.
But the major problem which could well cause MySQL to give an error is the missing ; at the end of the last line. If that's the problem, re-edit the SQL file and put back the ; and try again. |
I didn't retype the error message...I just copied and pasted it and just deleted the "----" part. I set the categories to bgcategories on old db I had. Is that supposed to be b2categories? There was no ; after myISAM. I just copied it directly. How do I edit the mySQL file? In PHPmyadmin or what kind of program that can edit the file ending with *.mysql? Thx for your response! |
|
Back to top |
|
 |
stevem
Joined: 15 Mar 2003 Posts: 368
|
Posted: Tue Feb 10, 2004 9:44 pm Post subject: |
|
|
It only matters if it is bgcategories if your new blog recognises it. But that won't affect restoring the database.
Yes you ought to have the ; there and the fact that it is missing may be why you are getting an error, so it's worth adding it and seeing what happens.
.sql files are just text files so you can edit them in any text editor. |
|
Back to top |
|
 |
beachgerl
Joined: 14 Apr 2003 Posts: 37 Location: San Diego
|
Posted: Wed Feb 11, 2004 7:21 pm Post subject: |
|
|
Okay, I opened the .sql file and I see there is a ; at the end of that line myisam. I'm wondering if it's the 'bgcategories' that's affecting the set up of db because with this new host the databases that are created automatically are added with a prefix ie: bg_b2categories. Is that a possible obstacle? Do I need to modify that? Does that mean the database I created must be the same name as the name I created on other host or it doesn't matter? I'm still trying to understand the error.
I still received same error when I tried to upload it again without any modifications. |
|
Back to top |
|
 |
|