<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wojciech Jamrozy blog &#187; archiver</title>
	<atom:link href="http://www.wojtekrj.net/tag/archiver/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wojtekrj.net</link>
	<description>Blog about computer science, programming and linux</description>
	<lastBuildDate>Sun, 23 Oct 2011 21:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>[Python] Updater/archiver/synchronizer of directories</title>
		<link>http://www.wojtekrj.net/2008/07/python-updater/</link>
		<comments>http://www.wojtekrj.net/2008/07/python-updater/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 08:29:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[archiver]]></category>
		<category><![CDATA[file manipulation]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[updater]]></category>

		<guid isPermaLink="false">http://www.wojtekrj.net/?p=40</guid>
		<description><![CDATA[I have directory with data (music, photos, ebooks etc.) on my two computers. I wanted to have exactly same files in my data directory on both machines, but sometimes I changed content of the directory on one of computers. I&#8217;ve decided to write program, which would help me to update data directory on another machine. [...]]]></description>
			<content:encoded><![CDATA[<p>I have directory with data (music, photos, ebooks etc.) on my two computers. I wanted to have exactly same files in my data directory on both machines, but sometimes I changed content of the directory on one of computers. I&#8217;ve decided to write program, which would help me to update data directory on another machine.<span id="more-40"></span></p>
<p>Firstly, I&#8217;ve installed Samba, a good tool for managing it – gsambad and smbfs (mount and umount commands for the smbfs). You can use NFS instead of samba if you want. Then, I&#8217;ve made my “changed” data directory accessible for another computer.<br />
Lets assume, that <em>/data</em> is “unchanged” data directory on second computer, and <em>/datarem</em> is mounted “changed” data directory from first computer. You can mount this directory using for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #660033;">-t</span> smbfs <span style="color: #660033;">-o</span> <span style="color: #007800;">iocharset</span>=utf8,<span style="color: #007800;">username</span>=<span style="color: #ff0000;">&quot;TYPE_SMB_USER&quot;</span>,<span style="color: #007800;">password</span>=<span style="color: #ff0000;">&quot;TYPE_PASS&quot;</span> <span style="color: #000000; font-weight: bold;">//</span>192.168.2.15<span style="color: #000000; font-weight: bold;">/</span>data <span style="color: #000000; font-weight: bold;">/</span>datarem</pre></div></div>

<p>Then you can use my script mov.py with following usage:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>mov.py changed_directory directory_to_update</pre></div></div>

<p>for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>mov.py <span style="color: #000000; font-weight: bold;">/</span>datarem <span style="color: #000000; font-weight: bold;">/</span>data</pre></div></div>

<p>The script will scan <em>/datarem</em> recursively for files and directories. If corresponding file or directory doesn&#8217;t exist in <em>/data</em>, it will be created. If size of file in <em>/datarem</em> is different to a size of corresponding file in <em>/data</em>, the file in<em> /data</em> will be replaced. If sizes are equal, file in <em>/data</em> is keep unchanged (it doesn&#8217;t matter for me, but for some usages it may be a problem).</p>
<p>Here is code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># Created by Wojtek Jamrozy (www.wojtekrj.net)</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">shutil</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span>start, end<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">for</span> f <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">listdir</span><span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span>:
			s_ = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>start, f<span style="color: black;">&#41;</span>
			e_ = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>end, f<span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>s_<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>e_<span style="color: black;">&#41;</span>:
				<span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>e_<span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Directory &quot;</span> +e_+<span style="color: #483d8b;">&quot; has been created<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
			update<span style="color: black;">&#40;</span>s_,e_<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>end<span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span>end<span style="color: black;">&#41;</span>:
				<span style="color: #dc143c;">shutil</span>.<span style="color: black;">copyfile</span><span style="color: black;">&#40;</span>start,end<span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;File &quot;</span> +end+<span style="color: #483d8b;">&quot; has been updated<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #dc143c;">shutil</span>.<span style="color: black;">copyfile</span><span style="color: black;">&#40;</span>start,end<span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;File &quot;</span> +end+<span style="color: #483d8b;">&quot; has been created<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
st = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
en = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>st<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Source directory does not exist!&quot;</span>
	<span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>st<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>en<span style="color: black;">&#41;</span>:
	<span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>en<span style="color: black;">&#41;</span>
update<span style="color: black;">&#40;</span>st,en<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>You can download it here:<br />
Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>Most functions used in this script are easy to understand. You can find further information here:<br />
<a href="http://docs.python.org/lib/module-os.path.html" target="_blank">http://docs.python.org/lib/module-os.path.html</a><br />
<a href="http://www.python.org/doc/current/lib/module-shutil.html" target="_blank">http://www.python.org/doc/current/lib/module-shutil.html</a><br />
<a href="http://docs.python.org/lib/module-sys.html" target="_blank">http://docs.python.org/lib/module-sys.html</a></p>
<p><a class="a2a_button_wykop" href="http://www.addtoany.com/add_to/wykop?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Wykop" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/wykop.png" width="16" height="16" alt="Wykop"/></a><a class="a2a_button_delicious" href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a><a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a><a class="a2a_button_technorati_favorites" href="http://www.addtoany.com/add_to/technorati_favorites?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Technorati Favorites" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technorati.png" width="16" height="16" alt="Technorati Favorites"/></a><a class="a2a_button_digg" href="http://www.addtoany.com/add_to/digg?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Digg" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/digg.png" width="16" height="16" alt="Digg"/></a><a class="a2a_button_google_bookmarks" href="http://www.addtoany.com/add_to/google_bookmarks?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Google Bookmarks" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/google.png" width="16" height="16" alt="Google Bookmarks"/></a><a class="a2a_button_msdn" href="http://www.addtoany.com/add_to/msdn?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="MSDN" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/msdn.png" width="16" height="16" alt="MSDN"/></a><a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a><a class="a2a_button_technotizie" href="http://www.addtoany.com/add_to/technotizie?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="Technotizie" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technotizie.png" width="16" height="16" alt="Technotizie"/></a><a class="a2a_button_technet" href="http://www.addtoany.com/add_to/technet?linkurl=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;linkname=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" title="TechNet" rel="nofollow" target="_blank"><img src="http://www.wojtekrj.net/wp-content/plugins/add-to-any/icons/technet.png" width="16" height="16" alt="TechNet"/></a><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.wojtekrj.net%2F2008%2F07%2Fpython-updater%2F&amp;title=%5BPython%5D%20Updater%2Farchiver%2Fsynchronizer%20of%20directories" id="wpa2a_2">Share/Save</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.wojtekrj.net/2008/07/python-updater/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

