Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@josephj
Created April 18, 2011 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephj/924791 to your computer and use it in GitHub Desktop.
Save josephj/924791 to your computer and use it in GitHub Desktop.
Export all wikis from one Trac environment to another Trac environment.
#!/usr/bin/php
<?php
// Modify the following constants according to your needs.
define("BACKUP_PATH", "/var/backup/trac/"); // Backup directory.
define("SOURCE_PROJECT", "/var/www/trac/muchiii"); // Source Trac Environment
define("TARGET_PROJECT", "/var/www/trac/router"); // Target Trac Environment
define("REPLACE_FROM", array( // Replace search string in wiki content.
"muchiii.com",
));
define("REPLACE_TO", array( // Replaced string in wiki content.
"miiicasa.com",
));
define("WORK_PATH", "/tmp/trac-wiki-transfer/"); // Working directory,
// your files will be copied here for string replacement.
$timestamp = date("YmdHi");
// Step 1. Backup.
echo "\n";
exec("mkdir -p ". BACKUP_PATH);
$backup_path = BACKUP_PATH . "muchiii_" . $timestamp . "/";
$cmd = "trac-admin " . SOURCE_PROJECT . " hotcopy $backup_path";
echo "\n";
echo "Backup source project files to $backup_path ... ";
exec($cmd);
echo "(DONE)";
$backup_path = BACKUP_PATH . "router_" . $timestamp . "/";
$cmd = "trac-admin " . TARGET_PROJECT . " hotcopy $backup_path";
echo "\n";
echo "Backup target project files to $backup_path ... ";
exec($cmd);
echo "(DONE)";
// Step 2. Create working directory.
echo "\n";
$work_path = WORK_PATH . $timestamp;
echo "Create working directory $work_path ... ";
exec("mkdir -p $work_path");
echo "(DONE)";
// Step 3. Dumping source wiki files.
echo "\n";
$cmd = "trac-admin " . SOURCE_PROJECT . " wiki dump $work_path";
echo "Dumping source wiki files to $work_path ... ";
exec($cmd);
echo "(DONE)";
// Step 4. String replacement.
echo "\n";
require_once "/usr/share/pear/File/SearchReplace.php"; // Batchly find and replace file content.
echo "Making necessary string replacement ... ";
$work_path = $work_path . "/";
$snr = new File_SearchReplace(REPLACE_FROM, REPLACE_TO, array(), $work_path, TRUE);
$snr->setSearchFunction("normal");
$snr->doSearch();
echo "(DONE)";
// Step 5. Copy attachment files to destination. (protect existing files.)
echo "\n";
$cmd = "cp -R --update " . SOURCE_PROJECT . "/attachments " . TARGET_PROJECT . "/";
echo "Coping attachment files to target project " . TARGET_PROJECT . "/attachments ... ";
exec($cmd);
echo "(DONE)";
// Step 6. Target loads wiki files in working directory. (protect existing wikis)
echo "\n";
$cmd = "trac-admin " . TARGET_PROJECT . " wiki load $work_path";
echo "Inserting wiki files to project " . TARGET_PROJECT . " ... ";
exec($cmd);
echo "(DONE)";
// Step 7. Target loads wiki files in working directory. (protect existing wikis)
echo "\n";
$cmd = "rm -rf $work_path";
echo "Removing working directory ($work_path) ... ";
exec($cmd);
echo "(DONE)";
echo "\n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment