<?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>Ninetynine.be &#187; usb drive</title>
	<atom:link href="http://ninetynine.be/blog/tag/usb-drive/feed/" rel="self" type="application/rss+xml" />
	<link>http://ninetynine.be/blog</link>
	<description>Just another blog with no useful content on it at all.</description>
	<lastBuildDate>Sun, 08 Jan 2012 10:39:54 +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>Ubuntu: backup to USB drive on mount</title>
		<link>http://ninetynine.be/blog/2009/03/ubuntu-backup-to-usb-drive-on-mount/</link>
		<comments>http://ninetynine.be/blog/2009/03/ubuntu-backup-to-usb-drive-on-mount/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 16:37:14 +0000</pubDate>
		<dc:creator>ruben</dc:creator>
				<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[usb drive]]></category>

		<guid isPermaLink="false">http://ninetynine.be/blog/?p=17</guid>
		<description><![CDATA[Hi everyone, I always was sloppy with my backups and I really needed to do something about it. I bought a new Freecom HDD 500GB and what I wanted to do is quickly backup my files every time I plug the device in, and it should give me a proper notice when the backup is [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone,</p>
<p>I always was sloppy with my backups and I really needed to do something about it. I bought a new Freecom HDD 500GB and what I wanted to do is quickly backup my files every time I plug the device in, and it should give me a proper notice when the backup is done.</p>
<p>Doesn&#8217;t sound that hard, or does it?</p>
<p>It became clear that I needed a little thingy called &#8220;udev&#8221;. And with big help from unutbu from <a href="http://ubuntuforums.org">ubuntuforums.org</a> I managed to get a very nice script up and running.</p>
<p>First thing you have to do is to know how your system recognizes your USB drive. </p>
<p>I followed <a href="http://ubuntuforums.org/showthread.php?t=168221">this great tutorial here</a>. (Don&#8217;t bother the fstab!)</p>
<p>My rules look like these.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #007800;">SUBSYSTEMS</span>==<span style="color: #ff0000;">&quot;usb&quot;</span>, <span style="color: #007800;">KERNEL</span>==<span style="color: #ff0000;">&quot;sd?1&quot;</span>, ATTRS<span style="color: #7a0874; font-weight: bold;">&#123;</span>product<span style="color: #7a0874; font-weight: bold;">&#125;</span>==<span style="color: #ff0000;">&quot;Freecom Network Drive&quot;</span>, <span style="color: #007800;">NAME</span>=<span style="color: #ff0000;">&quot;freecomHD&quot;</span>, RUN+=<span style="color: #ff0000;">&quot;/usr/bin/usb_backup.sh&quot;</span></pre></td></tr></table></div>

<p>Note that I have saved them under &#8220;/etc/udev/rules.d/81-local.rules&#8221;, because /etc/udev/rules.d/README says. It would also be safer I you select your device using UUID. </p>
<blockquote><p>
Files should be named xx-descriptive-name.rules, the xx should be<br />
chosen first according to the following sequence points:</p>
<p>80 rules that run programs (but do not load modules)</p></blockquote>
<p>After adding your rule, you need to restart udev.</p>

<div class="wp_syntax"><div class="code"><pre class="unix" style="font-family:monospace;">$ sudo /etc/init.d/udev restart</pre></div></div>

<p>Next install libnotify-bin through synaptic. This will make it possible for us to show notifies as if it were GNOME itself <img src='http://ninetynine.be/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Next we&#8217;ll need to hack the script a little bit:</p>
<p><span id="more-17"></span><br />
Place this in &#8220;/usr/local/bin/alt-notify-send&#8221;</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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">user</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">whoami</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">pids</span>=<span style="color: #000000; font-weight: bold;">`</span>pgrep <span style="color: #660033;">-u</span> <span style="color: #007800;">$user</span> gnome-panel<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">title</span>=$<span style="color: #000000;">1</span>
<span style="color: #007800;">text</span>=$<span style="color: #000000;">2</span>
<span style="color: #007800;">timeout</span>=$<span style="color: #000000;">3</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$title</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> You need to give me a title <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$text</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">text</span>=<span style="color: #007800;">$title</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$timeout</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">timeout</span>=<span style="color: #000000;">60000</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> pid <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$pids</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #666666; font-style: italic;"># find DBUS session bus for this session</span>
        <span style="color: #007800;">DBUS_SESSION_BUS_ADDRESS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-z</span> DBUS_SESSION_BUS_ADDRESS \
                <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$pid</span><span style="color: #000000; font-weight: bold;">/</span>environ <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/DBUS_SESSION_BUS_ADDRESS=//'</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #666666; font-style: italic;"># use it</span>
        <span style="color: #007800;">DBUS_SESSION_BUS_ADDRESS</span>=<span style="color: #007800;">$DBUS_SESSION_BUS_ADDRESS</span> \
        notify-send <span style="color: #660033;">-u</span> low <span style="color: #660033;">-t</span> <span style="color: #007800;">$timeout</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$title</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$text</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>This is because</p>
<blockquote><p>The reason why notify-send was not working was because notify-send uses the DBUS_SESSION_BUS_ADDRESS environment variable and that variable was not set in the shell that runs your script. This environment variable is set when you start a gnome session, but is not set when root runs a udev rule.</p>
<p>Moreover, one user can not send messages to another user using notify-send. So we use &#8220;su&#8221; to change root to your normal user, and we use the alt-notify-send script to find and set the DBUS_SESSION_BUS_ADDRESS before launching notify-send.</p></blockquote>
<p>(<a href="http://ubuntuforums.org/showthread.php?t=1094342&#038;page=2">unutbu</a>)</p>
<p>Then I created a file /usr/bin/usb_backup.sh.</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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #007800;">mount_point</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">grep</span> freecomHD <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mtab<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">attempts</span>=<span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mount_point</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$attempts</span>&quot;</span> <span style="color: #660033;">-le</span> <span style="color: #000000;">50</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #666666; font-style: italic;"># $mount_point has not been found</span>
    <span style="color: #666666; font-style: italic;"># quit if this fails more than 50 times. </span>
    <span style="color: #666666; font-style: italic;"># This should not be necessary, but it better to be safe</span>
    <span style="color: #666666; font-style: italic;"># than have the script trapped in this loop forever for</span>
    <span style="color: #666666; font-style: italic;"># some unforeseen reason.</span>
    <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">1</span>
    <span style="color: #007800;">mount_point</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">grep</span> freecomHD <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>mtab<span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #007800;">attempts</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$attempts</span>+<span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$mount_point</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #c20cb9; font-weight: bold;">su</span> ruben alt-notify-send <span style="color: #ff0000;">&quot;Backup Message&quot;</span> <span style="color: #ff0000;">&quot;USB Backup device detected&quot;</span> <span style="color: #000000;">0</span>
&nbsp;
    rsync <span style="color: #660033;">-a</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>ruben<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>FREECOM\ HDD<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">su</span> ruben alt-notify-send <span style="color: #ff0000;">&quot;Backup Message&quot;</span> <span style="color: #ff0000;">&quot;Your USB Backup has completed&quot;</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">&amp;</span></pre></td></tr></table></div>

<p>Note that my user is named &#8220;ruben&#8221; and that you should replace it by your own. Also replace the the name in mount_point to the name you have given in your udev-rules. </p>
<p>Also note that the whole process is run in a new process (due to the &#8220;&#038;&#8221; character). Otherwise ubuntu will try to do the backup before the mount. But it needs to be mounted before backup. This requires the little hack on top of the script <img src='http://ninetynine.be/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Don&#8217;t forget to make the scripts executable!</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="unix" style="font-family:monospace;">$ sudo chmod 755 /usr/bin/usb_backup.sh
$ sudo chmod 755 /usr/local/bin/alt-notify-send</pre></td></tr></table></div>

<p>It may be a good thing to tweak your rsync a little bit, so you don&#8217;t lose too much time every time you plug in.</p>
<p>And that&#8217;s it! This was a little harder than I thought. But then again, it shows that you&#8217;re absolutely free to do whatever you want on ubuntu. This is why we all love our tux.</p>
<p>Hope this helped somebody. And stick around to read more about all sorts of stuff!</p>
<p>Happy coding!</p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://ninetynine.be/blog/2009/03/ubuntu-backup-to-usb-drive-on-mount/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

