PHP mirror script - server to server copying
these scripts will make an exact copy of an entire
directory with all subfolders and files on a remote
server.
it's the perfect tool
if you move sites or if you have a site template somewhere which you want to copy over.
having to download a whole site to your desktop just to upload it again on the next server is,
especially if you consider the difference between the bandwith of your server and your desktop, not the best option.
this file will copy from one server directly to the other. without FTP.
of course you have to have access to both servers, it won't get to the files remotely.
you upload the file mirror_agent.php into the directory you want to copy, and upload the file mirror_client.php to the directory where you want the mirror.
just change a single variable in mirror_client.php and load it in your browser.
and that's it basically...
mirror_agent.php - the script which lists all files
download mirror_agent.php | downloaded 2282 times | last changed 22.11.2004 14:18
01 <?php
02
03 /*|----------------------------------------------------------|
04 | |
05 | lixlpixel mirror_magic |
06 | |
07 | mirror_agent.php part 1 of 2 |
08 | |
09 | copyleft 2004 lixlpixel.com |
10 | |
11 |----------------------------------------------------------|
12 |-------------------- what it does ------------------------|
13 | |
14 | this script(s) will make an exact copy of an entire |
15 | directory with all subfolders and files on a remote |
16 | server. |
17 | |
18 | once its sister script mirror_client.php connects to |
19 | this script it will list all files as a hex string. |
20 | the mirror_agent.php converts this hex data to binary |
21 | and writes the files to disk on the server it runs on. |
22 | |
23 | after a sucessful run the mirror_client.php and this |
24 | script will delete themselves to protect from abuse. |
25 | |
26 |----------------------------------------------------------|
27 | |
28 | ================================================= |
29 | WARNING ! DON'T USE ORIGINAL ! WILL DELETE ITSELF |
30 | ================================================= |
31 | |
32 |----------------------------------------------------------|
33 |----------------------- usage ----------------------------|
34 | |
35 | put a COPY of this file here INSIDE the directory |
36 | you want to mirror. |
37 | |
38 | change the variable in mirror_client.php |
39 | to the URL of this file. |
40 | |
41 |----------------------------------------------------------|
42 | |
43 | no need to edit below this line |
44 | |
45 |----------------------------------------------------------|*/
46
47
48 /* --------------------- functions -------------------------- */
49
50 // disable errors ( files can be unreadable by PHP due to permissions )
51 error_reporting(0);
52
53 // disable time limit ( you never know how big this site is )
54 set_time_limit(0);
55
56 // convert all files to hex
57 function recur_dir($dir)
58 {
59 $dirlist = opendir($dir);
60 while ($file = readdir ($dirlist))
61 {
62 if ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'mirror_agent.php')
63 {
64 $newpath = $dir.'/'.$file;
65 $permissions = substr(decoct(fileperms($newpath)),-4);
66 if (is_dir($newpath))
67 {
68 echo '
69 dir|'.$newpath.'|'.$permissions;
70 recur_dir($newpath);
71 }else{
72 echo '
73 file|'.$newpath.'|'.$permissions.'
74 '.bin2hex(file_get_contents($newpath));
75 }
76 }
77 }
78 closedir($dirlist);
79 return $mod_array;
80 }
81
82 header('Content-type text/plain');
83 echo 'mirror_agent here';
84 recur_dir('.');
85
86 unlink(__file__);
87
88 ?>
mirror_client.php - the script which writes the files to the server
download mirror_client.php | downloaded 789 times | last changed 22.11.2004 14:17
01 <?php
02
03 /*|----------------------------------------------------------|
04 | |
05 | lixlpixel mirror_magic |
06 | |
07 | mirror_client.php part 2 of 2 |
08 | |
09 | copyleft 2004 lixlpixel.com |
10 | |
11 |----------------------------------------------------------|
12 |-------------------- what it does ------------------------|
13 | |
14 | this script(s) will make an exact copy of an entire |
15 | directory with all subfolders and files on a remote |
16 | server. |
17 | |
18 | it connects to its sister script mirror_agent.php |
19 | on the remote server and receives the data as a hex |
20 | string. it converts this hex data to binary and |
21 | writes the files to disk. |
22 | |
23 | after a sucessful run the mirror_agent.php and this |
24 | script will delete themselves to protect from abuse. |
25 | |
26 |----------------------------------------------------------|
27 | |
28 | ================================================= |
29 | WARNING ! DON'T USE ORIGINAL ! WILL DELETE ITSELF |
30 | ================================================= |
31 | |
32 |----------------------------------------------------------|
33 |----------------------- usage ----------------------------|
34 | |
35 | put a COPY of this file here INSIDE a fresh new |
36 | directory which must be WRITEABLE by PHP. |
37 | |
38 | make sure you UPLOADED a copy of the mirror_agent.php |
39 | to the directory on the remote server which |
40 | you want to mirror. |
41 | |
42 | CHANGE the $mirror_agent variable below to the |
43 | URL of the mirror_agent.php on the REMOTE SERVER. |
44 | |
45 | LOAD this file in your browser - done |
46 | |
47 |----------------------------------------------------------|*/
48
49
50 /* --------------------- configuration ---------------------- */
51
52 // set this to the URL of the mirror_agent.php on the server you want to mirror
53 $mirror_agent = 'http://www.remote.host/the_directory_to_mirror/mirror_agent.php';
54
55 /*|----------------------------------------------------------|
56 | |
57 | no need to edit below this line |
58 | |
59 |----------------------------------------------------------|*/
60
61
62 /* --------------------- functions -------------------------- */
63
64 // convert hex to binary
65 function hex2asc($temp)
66 {
67 for ($i = 0; $i < strlen($temp); $i += 2) $data .= chr(hexdec(substr($temp,$i,2)));
68 return $data;
69 }
70
71 // write file to disk
72 function file_write($filename, $filecontent, $mode='wb')
73 {
74 if($fp = fopen($filename,$mode))
75 {
76 fwrite($fp, $filecontent);
77 fclose($fp);
78 return true;
79 }else{
80 return false;
81 }
82 }
83
84 /* --------------------- the code --------------------------- */
85
86 // disable errors ( just in case you try this script before you read the usage above )
87 error_reporting(0);
88
89 // disable time limit ( you never know how big this site is )
90 set_time_limit(0);
91
92 // get the hex data from mirror
93 $cont = file_get_contents($mirror_agent);
94 $files = explode("\n",$cont);
95
96 // check if it's really the mirror_agent and if we have write permission
97 if(trim($files[0]) == 'mirror_agent here' && is_writeable(dirname(__file__)))
98 {
99 // yes - start copying
100 for($i = 1; $i < count($files); $i++)
101 {
102 if(trim($files[$i]) != '')
103 {
104 $parts = explode('|',trim($files[$i]));
105 $permissions = intval($parts[2], 8);
106 if($parts[0] == 'dir')
107 {
108 // it's a directory - make it
109 mkdir($parts[1],$permissions);
110 $done[] = $permissions.'|'.$parts[1];
111 }elseif($parts[0] == 'file')
112 {
113 // it's a file - write it down
114 file_write($parts[1], hex2asc($files[$i+1]));
115 chmod($parts[1],$permissions);
116 $done[] = $permissions.'|'.$parts[1];
117 $i++;
118 }
119 }
120 }
121 // output status
122 echo '
123 <pre>
124 following files were copied:';
125
126 for($i = 0; $i < count($done); $i++)
127 {
128 echo '
129 '.$done[$i];
130 }
131
132 echo '
133 </pre>';
134
135 unlink(__file__);
136 }else{
137 // error - it's not the mirror_agent, or you didn't chmod this directory
138 echo '
139 <pre>
140 ERROR:
141 '.$mirror_agent.'
142 does not respond like a mirror_agent should or
143 the directory '.dirname(__file__).' is not writeable.
144 </pre>';
145 }
146
147 ?>
