<?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>Backup &#8211; Phocean.net</title>
	<atom:link href="/tag/backup/feed" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Computer Security Blog</description>
	<lastBuildDate>Fri, 24 Feb 2017 21:17:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.10</generator>
	<item>
		<title>Automatic backup when inserting a drive</title>
		<link>/2009/09/28/automatic-backup-when-inserting-a-drive.html</link>
		<comments>/2009/09/28/automatic-backup-when-inserting-a-drive.html#comments</comments>
		<pubDate>Mon, 28 Sep 2009 09:34:11 +0000</pubDate>
		<dc:creator><![CDATA[phocean]]></dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[System]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[hal]]></category>
		<category><![CDATA[openSUSE]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[udev]]></category>

		<guid isPermaLink="false">http://www.phocean.net/?p=419</guid>
		<guid isPermaLink="false">http://www.phocean.net/?p=419</guid>
		<description><![CDATA[I bought a 500 GB 2.5&#8243; external disk drive to backup the data of my laptop. It is small, quiet, easy to move and far enough for the important data I want to backup, mostly documents, e-mails or script from work. Being lazy, it happened that I did not backup my data. Yes, it is...<br><i class="icon-right-hand"></i> <span class="read-more"><a href="/2009/09/28/automatic-backup-when-inserting-a-drive.html">Continue Reading</a></span>]]></description>
				<content:encoded><![CDATA[<p>I bought a 500 GB 2.5&#8243; external disk drive to backup the data of my laptop. It is small, quiet, easy to move and far enough for the important data I want to backup, mostly documents, e-mails or script from work.</p>
<p>Being lazy, it happened that I did not backup my data. Yes, it is a shame, but inserting a drive and launching the commands to rsync the discs was preventing me from this best practice.</p>
<p>So, I decided to make it automatic. The goal was that the only thing I would have to do would be to insert the drive, and then remove it when it is done.</p>
<p>Thanks to the magic of Gnu/Linux, it had been very easy. I will show below how I did it, thought they are many things that could be improved (but I haven&#8217;t felt the need so far).</p>
<h2><span style="text-decoration: underline;">Udev</span></h2>
<p><strong>Udev</strong> not only allows to create /dev entries dynamically, but offers a lot of triggers to perfom all kind of actions when some hardware is inserted.</p>
<p>The <strong><em>udevinfo</em></strong> command will show you a lot of output concerning your drive. What we want is a unique way to differenciate the backup drive from any other drive that will be inserted in the future.</p>
<p>What would be better than the manufacturer serial ?</p>
<p>So let&#8217;s look for it :</p>
<pre>$ udevinfo -a -p /sys/block/sdc | grep serial</pre>
<p><em><strong>*UPDATE 06/2011* </strong> It seems that on recent versions, the syntax of this command slightly changed into this :</em></p>
<pre>$ udevadm info -a -p /sys/block/sdc | grep serial</pre>
<p>Copy the serial.</p>
<p>Now we have to create a rule file, that will tell to udev what to do when this particular drive is inserted.</p>
<p>This is done in the <em><strong>/etc/udev/rules.d</strong></em> folder. Let&#8217;s create a file <em><strong>30-mnt.rules</strong></em> or anything you like.</p>
<p>We edit this file so that it contains :</p>
<pre>ACTION=="add",KERNEL=="sd*",SUBSYSTEMS=="usb", ATTRS{serial}=="57442D57584E3430394C5A38", RUN+="/home/jc/bin/backup/bckp-home.sh %k"</pre>
<p><em><strong>ACTION==&#8221;add&#8221;</strong></em> will tell udev that this action must be triggered when the drive is inserted.<br />
<em><strong>SUBSYSTEMS</strong></em> could be changed according to the drive you are using (scsi, usb, &#8230;).<br />
<em><strong>ATTRS{serial} </strong></em>must contain the serial you just grabbed.<br />
<em><strong>RUN+=&#8221;/path/to/bin/backup.sh %k&#8221;</strong></em> tells udev to launch the backup script. %k, which contains the device name, sdc, is passed as an argument.</p>
<p>Optionally, it is quite convenient, you may want to make a symlink to the <em><strong>/dev/sd?</strong></em> device, with :</p>
<pre>KERNEL=="sd*",SUBSYSTEMS=="scsi", ATTRS{model}=="GJ0250EAGSQ     ", SYMLINK+="ultrabay%n"</pre>
<h2><span style="text-decoration: underline;">The shell script</span></h2>
<p>Now, the script itself :</p>
<pre>#!/bin/sh
LOGFILE=/PATH/TO/bckp.log
echo "--- BCKP - INFO : \$1=_${1}_" &gt;&gt; $LOGFILE
[[ $1 ]] || { echo "ERROR : missing parameter"&gt;&gt;$LOGFILE; exit 1; }

# give time for the user, if needed to kill the process
sleep 6
MOUNT_PATH=$(grep $(echo $1) /etc/mtab | awk '{print $2}')
[[ $MOUNT_PATH ]] || {
  echo "ERROR fretching mount point"&gt;&gt;$LOGFILE;
  exit 1;
}
echo " Synchronizing $MOUNT_PATH)"&gt;&gt;$LOG

# add here all you rsync commands
rsync -av --delete /PATH/TO/DATA $MOUNT_PATH/backup/
...
exit 0</pre>
<h2><span style="text-decoration: underline;">Testing it</span></h2>
<p>Now, let&#8217;s reload udev :</p>
<pre>$ sudo udevadm control --reload-rules</pre>
<p>To test if it works :</p>
<pre>$ sudo udevadm trigger</pre>
<p>or maybe :</p>
<pre>$ /etc/init.d/boot.udev restart</pre>
<p>Plug off/in your drive, and the script should be executed as expected.</p>
<h2><span style="text-decoration: underline;">Optional : setting more options with Hal<strong><br />
</strong></span></h2>
<p>It is not necessary at all for the backup script to work, but it would be very practical to have  a fixed mount point for a drive.<br />
For instance, I use a second drive (in the untrabay slot of my thinkpad) that contains all my virtual machines.</p>
<p>The benefice is to prevent a performance drain of the system when many virtual machines are doing I/O like swapping or anything else.</p>
<p>Create a file like <strong><em>/etc/hal/fdi/policy/15-static-mount.fdi</em></strong>, containing :</p>
<pre>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;deviceinfo version="0.2"&gt;
  &lt;device&gt;
    &lt;match key="volume.uuid" string="aa0019ef-86e0-4011-b996-31ef3e7174c8"&gt;
      &lt;merge key="volume.policy.should_mount" type="bool"&gt;true&lt;/merge&gt;
      &lt;merge key="volume.fstype" type="string"&gt;ext4&lt;/merge&gt;
<span style="color: #ff0000;">      &lt;merge key="volume.policy.desired_mount_point" type="string"&gt;ultrabay&lt;/merge&gt;</span>
      &lt;merge key="volume.label" type="string"&gt;Fuji&lt;/merge&gt;
      &lt;merge key="volume.policy.mount_option.noatime" type="bool"&gt;true&lt;/merge&gt;
      &lt;merge key="volume.policy.mount_option.acl" type="bool"&gt;true&lt;/merge&gt;
    &lt;/match&gt;
  &lt;/device&gt;
&lt;/deviceinfo&gt;</pre>
<p>The drive is matched by it uuid. You can get the uuid of your disk with :</p>
<pre>$ ls -la /dev/disk/by-uuid/</pre>
<p>You can, if you want, set the volume label and specify several options of the file system.</p>
<p>However, the most interesting option is the &#8220;desired_mount_point&#8221; one which allow you to fix the mount point. In the example, the disk will always be mounted in </p>
<pre>/media/ultrabay</pre>
<p>, and not the system disk, or disk_1, etc.</p>
<h2><span style="text-decoration: underline;">Coming next</span> !</h2>
<p>That&#8217;s all for today folks. Let me know if there are some things not clear or that can be optimized.</p>
<p>Next time, we will see how to run the same script from <strong>Hal</strong> instead. We will also use Zenity to get a nice GUI prompt when the disk is inserted.</p>
]]></content:encoded>
			<wfw:commentRss>/2009/09/28/automatic-backup-when-inserting-a-drive.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Back-up your disk with DD</title>
		<link>/2006/12/04/back-up-your-disk-with-dd.html</link>
		<pubDate>Mon, 04 Dec 2006 15:32:00 +0000</pubDate>
		<dc:creator><![CDATA[phocean]]></dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[System]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Hard drive]]></category>

		<guid isPermaLink="false">http://192.168.1.10/wordpress/?p=8</guid>
		<description><![CDATA[<p>I had several bad experiences losing my data in my life. Sometimes because of the hardware, sometimes because of my stupidness or inexperience. At least, it learned to be careful. I backup my data regularly, and on several support concerning the important ones.</p>]]></description>
				<content:encoded><![CDATA[<p>I had several bad experiences losing my data in my life. Sometimes because of the hardware, sometimes because of my stupidness or inexperience. At least, it learned to be careful. I backup my data regularly, and on several support concerning the important ones.</p>
<p><span id="more-8"></span></p>
<p>I sometimes back up my primary partition, where the system is set. If my drive crashed, I want to recover quickly a system without having to reinstall hundreds of applications. I have used several times Partimage and was satisfied. But now I tend to use dd, which basically do the same thing in just one line. The advantage is that it is by default on any Linux distribution.</p>
<p>To back it up on an usb disk :</p>
<pre>$ sudo dd   if=/dev/hda1   |   gzip   -v   |   dd   of=/media/usbdisk/backup_hda1.gz</pre>
<p>To backup my home and other data partitions, I just copy the files manually, it is faster and there is no need to backup such a thing as the boot sector. But of course you could use the same command above.</p>
<p>When you wish to restore the partition to a new hard drive, just :</p>
<pre>$ zcat   /mnt/hdb5/sauvegarde.gz   |   dd   of=/dev/hda1</pre>
<p>To do that, you will have probably booted on a live CD Linux as Knoppix to restore the crashed system. In any case, don&#8217;t overwrite the system you just booted on !</p>
<p>One tip if you want to back up only the boot sector :</p>
<pre>$ sudo dd   if=/dev/hda   of=/home/secteur_boot.dd   bs=512   count=1</pre>
<p>And to restore :</p>
<pre>$ sudo dd   if=/home/secteur_boot.dd   of=/dev/hda   bs=512   count=1</pre>
]]></content:encoded>
			</item>
	</channel>
</rss>
