<?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>Information Retrieval Blog &#187; sync</title>
	<atom:link href="http://blog.zye.me/tag/sync/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.zye.me</link>
	<description>REAL TIME DATA PROCESSING, DISTRIBUTED COMPUTING, PATTERN DISCOVERY</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:33:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>backup and sync with lftp</title>
		<link>http://blog.zye.me/2010/02/55529.html</link>
		<comments>http://blog.zye.me/2010/02/55529.html#comments</comments>
		<pubDate>Sun, 21 Feb 2010 07:07:20 +0000</pubDate>
		<dc:creator>yezheng</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[lftp]]></category>
		<category><![CDATA[linuex]]></category>
		<category><![CDATA[sync]]></category>

		<guid isPermaLink="false">http://blog.so8848.com/?p=55529</guid>
		<description><![CDATA[lftp is an amazing  command line ftp tool, which lets you operate remote files just like in a local filesystem in a terminal (bash).  If you work a lot with command line, I bet you would like it.  A frequently used functionality with lftp is backup or sync a remote directory with a local one. Here <a href='http://blog.zye.me/2010/02/55529.html'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>lftp is an amazing  command line ftp tool, which lets you operate remote files just like in a local filesystem in a terminal (bash).  If you work a lot with command line, I bet you would like it.  A frequently used functionality with lftp is backup or sync a remote directory with a local one.</p>
<p>Here is simple synchronization script using lftp.</p>
<p>﻿﻿</p>
<div id="_mcePaste"><span style="color: #33cccc;">#/bin/bash</span></div>
<div id="_mcePaste"><span style="color: #33cccc;">lftp -u user,password publish.163.com &lt;&lt;EOF</span></div>
<div id="_mcePaste"><span style="color: #33cccc;">set ftp:ssl-allow no</span></div>
<div id="_mcePaste"><span style="color: #33cccc;">#a slash &#8220;/&#8221; in local dir is used to keep the dir structure</span></div>
<div><span style="color: #33cccc;">mirror   print/ ~/tmp_install/print/</span></div>
<div id="_mcePaste"><span style="color: #33cccc;">mirror -R  ~/tmp_install/print/ print/</span></div>
<div id="_mcePaste"><span style="color: #33cccc;">quit 0</span></div>
<div id="_mcePaste"><span style="color: #33cccc;">EOF</span></div>
<div>
<p><span style="color: #33cccc;"><span style="color: #000000; font-family: 'Lucida Grande', Verdana, Lucida, Helvetica, Arial, sans-serif; line-height: normal; font-size: 13px;"> </span></span></p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">To write the script, you need to know how to use lftp to connect to an <acronym style="cursor: help; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #000000; padding: 0px; margin: 0px;" title="File Transfer Protocol">FTP</acronym> server and synchronize a remote directory with a local one. If your <acronym style="cursor: help; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #000000; padding: 0px; margin: 0px;" title="File Transfer Protocol">FTP</acronym> server supports anonymous connections, you can connect to it using the simple command:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">lftp ftpsite</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">If the server requires a user name and password, the connection command would look like:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">lftp -u username,password ftpsite.</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">To synchronize a remote directory with a folder on your hard disk, lftp utilizes the <code style="font-size: 12px; padding: 0px; margin: 0px;">mirror</code> command. Used without switches, this command syncs the current local and remote directories. You can also specify explicitly the source and target directories:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">mirror path/to/source_directory path/to/target_directory</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">The <code style="font-size: 12px; padding: 0px; margin: 0px;">mirror</code> command offers a comprehensive set of switches, which you can use to control the synchronization process. For example, used with the <code style="font-size: 12px; padding: 0px; margin: 0px;">–delete</code> switch, the <code style="font-size: 12px; padding: 0px; margin: 0px;">mirror</code>command deletes the files in the local folder that are not present in the remote directory, while the <code style="font-size: 12px; padding: 0px; margin: 0px;">–only-newer</code> option forces lftp to download only newer files. Another handy switch is <code style="font-size: 12px; padding: 0px; margin: 0px;">–exclude</code>; it allows you to specify which files and directories to skip during synchronization. And if you prefer to keep an eye on the syncing process, you can use the <code style="font-size: 12px; padding: 0px; margin: 0px;">–verbose</code>switch.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">Typing all those switches every time you want to synchronize two directories can be a bit of a bother. Fortunately, lftp understands complex commands that can perform several actions in one fell swoop. All you have to do is to use the <code style="font-size: 12px; padding: 0px; margin: 0px;">-e</code> switch, so lftp stays connected and runs the specified commands:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">lftp -u username,password -e "mirror --delete --only-newer --verbose path/to/source_directory path/to/target_directory" ftpsite</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">Using this command, lftp connects to the <acronym style="cursor: help; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #000000; padding: 0px; margin: 0px;" title="File Transfer Protocol">FTP</acronym> server using the provided credentials, and then runs the command(s) in the quotes. You can save the entire command in a text file, then run it by pointing lftp to it using the <code style="font-size: 12px; padding: 0px; margin: 0px;">-f</code> switch:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">lftp -f /home/user/ftpscript.txt</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">lftp has a few other clever tricks up its sleeve. The <code style="font-size: 12px; padding: 0px; margin: 0px;">at</code> switch can come in handy when you want to run the backup at a specific time. The following command, for example, runs at midnight:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">lftp at 00:00 -u username,password -e "mirror --delete --only-newer --verbose path/to/source_directory path/to/target_directory" ftpsite &amp;</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">Notice the ampersand, which sends the command to the background so you don’t have to keep the terminal window open.</p>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">Now you know how to create local backup of files and directories stored on an <acronym style="cursor: help; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #000000; padding: 0px; margin: 0px;" title="File Transfer Protocol">FTP</acronym> server. But how do you restore the data if disaster strikes? Quite easily, actually. All you have to do is to add the <code style="font-size: 12px; padding: 0px; margin: 0px;">–reverse</code> switch to the <code style="font-size: 12px; padding: 0px; margin: 0px;">mirror</code> command:</p>
<pre class="code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; font-size: 12px; color: #000000; overflow-x: auto; overflow-y: auto; background-color: #f7f9fa; padding: 0.5em; border: 1px dashed #8cacbb;">lftp -u username,password -e "mirror --reverse --delete --only-newer --verbose path/to/source_directory path/to/target_directory" ftpsite</pre>
<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding: 0px;">As the name suggests, the switch reverses the source and target directories, so lftp uploads files from the local directory to the remote <acronym style="cursor: help; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #000000; padding: 0px; margin: 0px;" title="File Transfer Protocol">FTP</acronym> server.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.zye.me/2010/02/55529.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

