corz.org uses cookies to remember that you've seen this notice explaining that corz.org uses cookies, okay!
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/inc/init.php';//distro
/* v0.8.1 (php4+)
site synchroniser tool
- synchronise a website with its development mirror -
- single machine mac version -
this is a self-contained auto-site-synch-tutorial-cum-conversion-tool thing
in the beginning there was no backup, I would manually go through my whole
site with an ftp client, uploading any updated files to the live website.
sometimes I'd miss some, and always it was a pain-in-the-ass.
then came the backup script
it logs its actions to a file, which *this* tool converts into a set of
valid ftp commands that can be used to synchronise a real live website
with new and updated files from a development mirror at home/wherever.
the whole sync process is now reduced to a cut-n-paste and a click, plus a
couple of shell commands; one ("corzbax") for doing the backup (which
creates the all-important log entries), the other ("up") to upload the
*changed* files to the website via ftp. the backup script part is best run
regularly and automatically, a la cron. What took hours, now takes
seconds, the whole point.
for all this to work you need; a website, a local mirror, and space to
backup the local mirror. apart from the website, this will need to be on a
*nix machine; Linux, OS X, BSD, Unix, whatever, though I definitely
recommend these operating systems for your web server too! It wouldn't be
too difficult this get running on windows, I just don't desire that.
there's a link on the generated page which will take you to a "shell
scripts" page. you can simply copy and paste the code directly from that
page to create your two scripts. (there may also be some "I prepared
earlier" inside the zip distribution of this script) the backup script runs
twice a day as a cron job. As well as the twice-daily auto-backup, I have a
lite version ("cb"), without the gzipping, much faster, which I run manually
after long sessions.
the other shell command is "up", the upload script (also available from
this page). this lives in my OS X home folder. handy.
I prefer to "up" manually. If the whole thing was automatic, there's a high
chance that it would go off while you were working on something, and
"in-progress" pages would be uploaded to the live website. ouch! not very
smart. the "lite" (quick) backup script, and then "up" are called at the
end of development sessions, when I'm *fairly* certain the local copy is
functioning as expected, ie. without bugs.
to install:
* drop this script into your local webserver and load the tool in your browser.
* navigate to the "shell scripts" page and copy-n-paste them into real scripts.
(there will likely be some already prepared in the zip distribution)
* drop the scripts into your PATH (say, /usr/local/bin) and make excecutable..
cd /usr/local/bin
chmod +x up cb cbi corzbax
to use:
* run the "corzbax" (or "up") script. (corzbax: cron up: manually)
* copy (cut) the contents of the the mirror.log file into *this* tool's text area.
(I usually delete the contents and re-save it blank)
* paste the returned ftp commands into "up"
* save "up", and run it!** TADA!
(I can do the whole lot in under six seconds!)
I use this tool every day, sometimes "a lot".
have fun!
;o)
(c) 2004 >tomorrow! ~ cor + corz.org ;o)
Please view the license for this free software, here:
https://corz.org/free-scripts-licence.nfo
** to run a shell command, you simply open a terminal (/Applications/Utilities/Terminal)
and type the commands directly into the window. if you need help, mail me.
*/
// prefs..
$devsite_dir = "/Volumes/mac/webserver/cor/"; // location of home mirror
$web_dir = '/var/www/html/'; // location of files on server (ftp path)
// do special commands for these files..
$special_file = '.blog';
// what to do?
$special_command = 'chmod 777';
// your header here? it's just eye-candy, folks. (use a relative or FULL absolute path)
$site_header = $site_config['header'];//distro $site_header = $_SERVER['DOCUMENT_ROOT'].'/inc/header.php';
// style sheets, separate paths to multiple style sheets with commas..
$site_styles = $site_config['sync_styles'];//distro $site_styles = '/inc/css/main.css,/inc/css/sync.css';
// and footer.. (use a relative or FULL absolute path)
$site_footer = $site_config['footer'];//distro $site_footer = $_SERVER['DOCUMENT_ROOT'].'/inc/footer.php';
// end prefs
if (stristr($_SERVER['HTTP_HOST'],'corz')) { $topsize = 44; } else { $topsize = 5; }
// see inside prep() function itself for the file filters (stuff to remove from ftp uploads)
// it's easy to customise with your own no-no files and log lines..
// get the name of the devsite dir, alone. "cor", for me.
$paths = explode('/',$devsite_dir); $my_folder = $paths[count($paths)-2];
// do "shell scripts" page..
if (isset($_GET['showscript'])) {
do_header();
if (file_exists($site_header)) { include_once $site_header; }
show_scripts();
die();
}
/*
or else run the conversion tool.. */
$example_string = '
example backup script log output
/. the lines with .DS_Store and .ht_refs will be stripped.
/. also, a new "new" folder will be created first, click "do it"..
cor/.DS_Store
cor/blog/edit.php
cor/.ht_refs
cor/blog/blogz.blog
cor/new/
/Volumes/mac/webserver/cor/new/new-index.php
/. the last line demonstrates how this tool handles log output from other programs, too';
// converter tool..
if (isset($_POST['cvt_area'])) { $text = ($_POST['cvt_area']); } else { $text = ''; }
if (get_magic_quotes_gpc()) $text = stripslashes($text);
if (isset($_POST['prep'])) { $converted = prep($text); } else { $converted = $example_string; }
do_header();
if (file_exists($site_header)) { include_once $site_header; }
echo '
<div class="content tool">
<form method="post" action="',$_SERVER['REQUEST_URI'],'">
<div class="form"><br />
<label><strong>enter the raw log data..</strong>
<textarea name="cvt_area" id="cvt-area" rows="20" onclick="this.focus();this.select()" spellcheck="false"
title="If you click, all text is selected (for copying) - if you want to edit, use the keyboard!">',$converted,'</textarea>
</label>
<div class="clear"></div>
<div id="sftp-up">
<input type="submit" name="prep" value="make sftp commands!" />
</div>
<div id="select-all">
<input title="javascript select all function" type="button"
value="select all" onclick="javascript:this.form.cvt_area.focus();this.form.cvt_area.select();" />
</div>
<div class="clear"></div>
<!--site synchroniser tool (c) 2004 -> cor @ corz.org-->
<h2>The Old Mac Website Sync Tool..</h2>
..takes output from a *nix shell command like this..<br />
<br />
<code><span style="color:#CE9C20">
# archive any updated files, and log..<br />
tar -uvf /backup/corz.tar cor
</span></code><br />
<br />
and converts it into valid sftp commands we can use to update our live web site
with all the new and changed files from its development mirror. i.e synchronize<br />
<br />
If you have a *nix box kicking around, you\'ll probably prefer <a href="https://corz.org/machine/source/php/website-sync-linux.php" title="using two hard drives is more secure, if you\'ve got a linux box around, try this">this version</a> which uses <b>cp</b>, and (optionally) two machines, a cleaner and more secure backup strategy.
</div>
</form>
</div>';
toplinks_out(true);
@include $_SERVER['DOCUMENT_ROOT'].'/inc/footer-mac.php';
if (file_exists($site_footer)) { include_once $site_footer; }
echo '</body></html>';
/*
function prep($log)
convert mirror.log (created from corzbax script) into valid ftp commands (for auto-upload)
*/
function prep($log_data) {
global $backup_dir, $my_folder, $devsite_dir, $web_dir, $special_file, $special_command;
$dirs = array();
$special = array();
$log_file = explode("\n",$log_data);
while (list($key, $value) = each($log_file)) {
$log_file[$key] = trim($log_file[$key]);
/* filters..
add any files you want stripped out.. */
if (trim($value) == '') unset($log_file[$key]); // empty lines
if (stristr($value, "/.")) unset($log_file[$key]); // always do invisible files manually
if (stristr($value, "backup script")) unset($log_file[$key]); // logging information
if (stristr($value, "creating archive")) unset($log_file[$key]); // more log info from our shell-script
if (stristr($value, ".bak")) unset($log_file[$key]); // backup files
if (stristr($value, ".out")) unset($log_file[$key]); // debug files
if (stristr($value, ".comment")) unset($log_file[$key]); // do not replace online user comments!
if (stristr($value, "email_list.txt")) unset($log_file[$key]); // ARRGGHH! don't replace this! (again)
if (stristr($value, "corzoogle.php")) unset($log_file[$key]); // I'll do this myself, by hand
if (stristr($value, "thumbs.db")) unset($log_file[$key]); // pesky windows machines!
if (stristr($value, "desktop.ini")) unset($log_file[$key]); // pesky windows machines!
if (stristr($value, ".log")) unset($log_file[$key]); // no need to up these
if (stristr($value, "_arc/")) unset($log_file[$key]); // no need to up these
/* while it seems like it would be better to put these into some prefs array,
I'm not so sure; it's real easy to edit things down here, gets you inside the script, too. */
}
// special commands for certain files..
// best waiting until after we have filtered out the unwanted files
reset($log_file);
while (list($key, $value) = each($log_file)) {
if (stristr($value, $special_file)) {
array_push($special, $log_file[$key]);
}
}
$log_file = array_unique($log_file); // remove duplicates
sort($log_file); // sort alphabetically
// folders..
$i=0;
foreach($log_file as $line) {
// last character is forward slash (this is a folder)
if (substr($line,-1) == "/") {
$line = substr($line,strpos($line,$my_folder),-1); // apart from the root folder, we will create mkdir
if ($line != $my_folder) { //statements for all new/changed dirs. with tar, this can be simply because an
$dirs[$i] = $line; // enclosed files was deleted. but the ftp mkdir command just fails, no problem
}
unset($log_file[$i]);
}
$i++;
}
// the magic..
$i=0; $commands = array();
foreach($log_file as $line) {
$commands[$i] = preg_replace("/(.*)$my_folder\/(.*)/",
"\rdelete \"$web_dir\\2\"\rput \"$devsite_dir\\2\" \"$web_dir\\2\"",
$line);
$i++;
}
rsort($dirs); // need to sort these here, or else subdirs try to get created before their parents.
// add the mkdir commands to the commands list, at the start, of course.
foreach($dirs as $line) {
$new_dir = preg_replace("/(.*)$my_folder\/(.*)/", "\nmkdir \"$web_dir\\2\"", $line);
array_unshift($commands,"\n$new_dir\n");
}
/*
special actions */
$special_commands = array();
foreach($special as $line) {
$commands[$i] = preg_replace("/(.*)$my_folder\/(.*)/",
"\r$special_command \"$web_dir\\2\"",
$line);
$i++;
}
return implode("\n",$commands);
}
///*
//function do_header() */
//function old_do_header() {
//global $topsize,$HTML5_shiv;
// echo '<!DOCTYPE html>
//<html lang="en">
//<head>
//<meta charset="utf-8">
//<meta name="viewport" content="initial-scale=1.0, width=device-width" />
//<title>website sync tool for Mac (web site synchroniser with built-in tutorial!)</title><meta name="description" content="website synchronizer (synchroniser) tool, converts shell backup log (cp) output to valid ftp commands, comes with all the required shell scripts, and built-in tutorial, sort of">
//<style media="screen">
///*<![CDATA[*/
//@import "/inc/css/osx.css";
//@import "/inc/css/footer.css";
///*]]>*/
//</style>
//<style media="screen"><!--
// body {
// font-family: Tahoma, Lucida Grande, Helvetica, Verdana, sans-serif;
// color: #000;
// font-size: 11pt;
// }
// a:link { color:#869C20; text-decoration:none; }
// a:active { color:#869C20; text-decoration:none; }
// a:visited { color:#869C20; text-decoration:none; }
// a:hover { color:#869C20; text-decoration:none; }
//
// .links {
// font-size: 10pt;
// position: absolute;
// top: ',$topsize,'px; /* I leave room for my toobar! */
// right: 7px;
// }
//
// //--></style></head>
//<body leftmargin=0 rightmargin=0 topmargin=0 bottommargin=0 marginwidth=0 marginheight=0>';
//@include $_SERVER['DOCUMENT_ROOT'].'/public/mac/inc/osxheader.php';
//}/* end function do_header()
//*/
/*
function do_header() */
function do_header() {
global $site_styles;
echo '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, width=device-width" />
<title>website sync tool for Mac (web site synchroniser with built-in tutorial!)</title>
<meta name="description" content="website synchronizer (synchroniser) tool, converts shell backup log (cp) output to valid sftp commands, comes with all the required shell scripts, and built-in tutorial, sort of" />';
$style_sheets = explode(',', $site_styles);
foreach ($style_sheets as $my_sheet) {
echo '
<link rel="stylesheet" href="',$my_sheet,'" type="text/css" media="screen" />';
}
echo '
</head>
<body>';
}/* end function do_header()
*/
/*
function show_scripts() */
function show_scripts() {
echo '
<div class="content tool">
<h2>The shell scripts..</h2>
<p>Here are the actual scripts that were once used to sync corz.org and my mac dev mirror, using only my mac. my friendly local linux box was offline for a few days (hardware), so I needed a mac-only version, and <i>then</i> I realised how poor the OS X version of <b>cp</b> is. This one uses <b>tar</b> to do the backup, and create the log. I couldn\'t survive even a couple of days without my sync script..
<p>Why? well, I don\'t have to keep tabs on which files I\'ve edited, for starters; and I tend to edit lots of different site files in one session; keeping track would be an enormous task; the backup script does this for me. Only new and changed files are backed up, thereby indicating <i>which</i> files are new or changed!
<p>The first time you run this script, it creates a new <b>tar</b> archive of your entire site. this will take a few moments and used as your <i>baseline</i> mirror, after which we can really <i>use</i> the script. Now, whenever you run the it, you will have only the <i>updated</i> files in the output log.
<p>The few seconds it takes to edit your prefs here will save you hours and hours and hours in the future. <b>Mac users Note:</b> If your local site folder has custom icons, the resultant tar and tar.gz archives will probably be unusable on a PC, possibly other platforms. Yeah, it\'s a resource fork thing.
<p>okay, here goes..
<p><code><span style="color:#586858"> file: /usr/local/bin/cb</span></code>
<pre class="small">
<span style="color:#a8b8a8">#!/bin/sh</span>
<span style="color:#a8b8a8"># corz.org mirror backup script (single-mac version)..</span>
<span style="color:#a8b8a8"># an interactive version is also included in the zip archive</span>
<span style="color:#a8b8a8"># note: this script is still very immature.</span>
<span style="color:#a8b8a8"># please <a href="',$GLOBALS['site_config']['email_address_protected'],'?subject=website-sync"
title="email me about this script">report</a> any bugs/problems. ta</span>
<span style="color:#a8b8a8"># prefs..</span>
devroot="/Volumes/mac/webserv"
sitefolder="cor"
backupdir="/Users/corz/bax"
arcname="corz"
<span style="color:#a8b8a8"># this is the file who\'s content we will convert into ftp commands</span>
logfile="/Volumes/mac/webserver/itest/mirror.log"
<span style="color:#a8b8a8"># end prefs</span>
cd $devroot
echo >> $logfile
echo "corz backup script: "`date "+%Y.%m.%d-%H.%M"`.. >> $logfile
echo "corz backup script - "`date "+%Y.%m.%d-%H.%M"`..
if ! [ -e $backupdir/$arcname.tar ];then
echo "creating initial \'base\' archive.."
echo "creating archive (base - no logging for this) .." >> $logfile
tar --verify -cSf $backupdir/$arcname.tar $sitefolder > /dev/null
fi
echo "syncing backup archive.."
echo "creating archive (sync) .." >> $logfile
echo >> $logfile
tar -uvSf $backupdir/$arcname.tar $sitefolder >> $logfile
echo "gzipping backup archive.."
echo "creating archive (gzipping) .." >> $logfile
gzip -c9 $backupdir/$arcname.tar > $backupdir/`date "+%Y.%m.%d-%H.%M"`_$arcname.org.tar.gz
echo "backup script completes .." >> $logfile
echo >> $logfile
echo "corz backup script completed"
exit 0
<span style="color:#a8b8a8"># fin</span>
</pre>
<p>The output from the converter would go into a script something like this..<br />
(after a quick manual check, of course)<br />
<br />
<br />
<code><span style="color:#586858"> file: ~/up</span></code>
<pre>
<span style="color:#a8b8a8">#!/bin/sh</span>
<span style="color:#a8b8a8"># upload new files to website..</span>
HOST=\'ftp.mysite.com\'
USER=\'username\'
PASSWD=\'password\'
ftp -n $HOST <span style="color:#D1A919"><<FTP_QUIT
quote USER $USER
quote PASS $PASSWD
binary
<span style="color:#a8b8a8"># paste converted ftp commands right here</span>
quit
FTP_QUIT</span>
exit 0
<span style="color:#a8b8a8"># fin</span></pre>
<p>That\'s it! your wesite and its mirror are one!
</div>
<div class="clear-half"></div>';
toplinks_out();
if (file_exists($GLOBALS['site_footer'])) { include_once $GLOBALS['site_footer']; }
}/* end function show_scripts()
*/
// my_function..
function toplinks_out($at_tool=false) {
$href_add = '';
$alink_str = 'test conversion tool';
if ($at_tool) {
$href_add = '?showscript=true';
$alink_str = 'view the scripts';
}
echo '<span class="toplinks"><a href="',$_SERVER['PHP_SELF'],$href_add,'" title="',$alink_str,' page">',$alink_str,'</a> • <a href="https://corz.org/engine?section=php&download=website-sync.zip" title="download the zip of the website-sync">download the zip</a> • <a href="website-sync-linux.php" title="check out the linux version">linux version</a></span>';
}
?>