tips and tricks for movable type

output MT templates with MySQL and PHP
November 28, 2002

I've been meaning to post Brenna's script to output your MT blog templates using the MySQL backend and PHP. So here it is.

First, create your connect file (to access your MySQL database):

<?
// name of your database
$database = "databasename";

// connect to database
$db = mysql_connect("localhost", "dbuser", "dbpassword");
mysql_select_db("$database",$db);
?>

Be sure to change databasename, dbuser, and dbpassword to your specific info.

Save and upload to your server.

Then create the backup script:

<?php
//connection info
include ("/path/to/your/connection/file/connect.php");

echo "<html><head><title>Template Backup</title></head><body>";
echo "<hr /><p>Backup created on: ", date("d-M-Y G:i:s"), "</p><hr /><br /><br />";

$grab = "SELECT template_name, template_outfile, template_text FROM mt_template WHERE (template_blog_id=11)";
$entry = mysql_query($grab);
while ($row = mysql_fetch_array($entry))
{
$row = str_replace(array(">", "<"), array("&gt;", "&lt;"), $row);
echo "<p><b>Name: ", ($row['template_name']), "</b><br /><b>Outfile: ", ($row['template_outfile']), "</b><br /><br /><b>TEMPLATE TEXT:</b></p><pre>", ($row['template_text']), "</pre><hr />";
}

echo "</body></html>";
?>

and update /path/to/your/connection/file/connect.php to point to your connect file, and 11 to the ID of the blog you want to backup.

Save and upload to your server.

Point your browser to the URL for your backup script:

http://www.yourdomain.com/blog/backup.php

The results will generate in your browser window - you can save the output to a text file for safekeeping. Now, if you ever have to recreate your blog, you can copy and paste the templates as needed.

Comments

I appreciate this tip. I can see how it could be usefull, but i was wondering about the modules. My modules are not included in the backup and it would be nice to include those too since I use a lot of modules for my templates

by scott | 01.28.03 11:06 AM

I'm not really sure why your modules aren't being included - when I run the script, it displays all of my template modules along with the other templates. =(

It looks like it sorts the templates alphabetically; are you sure you didn't just overlook your template modules in the list because of how they're sorted?

by girlie | 01.28.03 06:54 PM

hi Girlie,

I am getting this error and dont understand why:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/breaking/public_html/blog/backup.php on line 8

by Ken Edwards | 03.22.03 01:26 PM

I think that indicates a problem with the query (the $grab line) - like it's not producing any results. Did you put the right blog ID in there?

by girlie | 03.22.03 03:59 PM

hi girlie,

i'm a new mtblogger and am finding your site very useful. thanks!

something about mt has been puzzling me for a while now, and that's the redundant archive (individual) file generation.

i haven't been able to find a blog with say... mt/archives/one.php?entry_id=000001 everyone just seems to be generating a file for each entry.

am i totally missing something here?

thanks!

by kathy | 08.10.03 09:23 AM

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

TrackBack: 4
(URL: http://www.thegirliematters.com/sf/mt-track.cgi/113)

» Aww Sh*t
Excerpt: Brilliant J, brilliant. I backup my templates whenever I change something. I backed it up when I added the comments...
Weblog: J : Da Blog
Tracked: 03.17.03 04:19 AM

» Go to sleep, go to sleep, close your big bloodshot eyes...
Excerpt: Damn I'm tired. I can't even make backups, but I will. *plugs in Memory Key* Tried to get this script...
Weblog: J : Da Blog
Tracked: 03.18.03 12:55 AM

» Export = Important
Excerpt: After what happened to Dan I made sure to back-up my blog entries (I alrea...
Weblog: Design-A-Blog
Tracked: 09.29.03 08:36 PM

» Backing Up Templates
Excerpt: As part of my back-up plan for this semester's ENGL classes, I needed to include the templates I'd prepared. I'd already downloaded 145 mb of files from the server as well as another 45 mb of files and emails from...
Weblog: Mama Write
Tracked: 05.20.04 07:37 PM