<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://illuminatedcomputing.com/">
  <id>https://illuminatedcomputing.com/</id>
  <title>Illuminated Computing</title>
  <updated>2010-03-20T00:00:00Z</updated>
  <link rel="alternate" href="https://illuminatedcomputing.com/" type="text/html"/>
  <link rel="self" href="https://illuminatedcomputing.com/tags/backups/atom.xml" type="application/atom+xml"/>
  <author>
    <name>Paul A. Jungwirth</name>
    <uri>https://illuminatedcommputing.com/</uri>
  </author>
  <entry>
    <id>tag:illuminatedcomputing.com,2010-03-20:/posts/2010/03/rsync-and-exclude-from/</id>
    <title type="html">rsync and --exclude-from</title>
    <published>2010-03-20T00:00:00Z</published>
    <updated>2010-03-20T00:00:00Z</updated>
    <link rel="alternate" href="https://illuminatedcomputing.com/posts/2010/03/rsync-and-exclude-from/" type="text/html"/>
    <content type="html">
&lt;p&gt;Lately I’ve been looking for a better backup solution, and I settled on rsync. If you want a good tutorial, you can find one easily enough by googling. But I had a lot of trouble finding good documentation about the &lt;code&gt;--exclude-from&lt;/code&gt; option. What I found was either too cursory or just plain wrong. So I did some tests, and here are my results. I’m pleased to say that rsync’s &lt;code&gt;--exclude-from&lt;/code&gt; option has very full functionality!&lt;/p&gt;

&lt;p&gt;This option names a file, as in &lt;code&gt;--exclude-from=my-excludes.txt&lt;/code&gt;. That file contains a list of patterns, and if one of the patterns matches a given file or directory, rsync leaves that file/directory out of the backup.&lt;/p&gt;

&lt;p&gt;These patterns may be full filenames, as in &lt;code&gt;log&lt;/code&gt;, or they may use wildcards, as in &lt;code&gt;*.bak&lt;/code&gt;. If a directory is excluded, then all the files within it are excluded, too. (This sounds obvious, but I’ve seen other programs with exclude files that just leave out the directory!)&lt;/p&gt;

&lt;p&gt;You can even include bits of a path in your exclude file to further limit what gets excluded. For instance, if you say &lt;code&gt;photos/thumb&lt;/code&gt;, then the &lt;code&gt;thumb&lt;/code&gt; directory will be excluded whenever it appears inside a folder called &lt;code&gt;photos&lt;/code&gt;. It will not be excluded if it appears in, say, &lt;code&gt;body-parts&lt;/code&gt;. Also, rsync will still include your &lt;code&gt;photos&lt;/code&gt; folders; just their &lt;code&gt;thumbs&lt;/code&gt; folders will be left out.&lt;/p&gt;

&lt;p&gt;Finally (and this is the part I was really interested in and found wrongly-documented elsewhere), you can include specific paths. You do this by starting the path with a slash: &lt;code&gt;/root/tmp&lt;/code&gt;. This forward slash does &lt;em&gt;not&lt;/em&gt; indicate a file at the root of your filesystem, like a normal unix path. Rather it tells rsync to anchor this path to the base of the top-level source folder.&lt;/p&gt;

&lt;p&gt;Note that if you’re using this feature, you must use it consistently with how you specify your source folder. Remember that in rsync, if you name your source folder without a trailing slash, rsync copies that folder and everything in it, whereas if you give it a trailing slash, rsync copies just the folders’ contents. Well, anchored paths in the &lt;code&gt;--exclude-from&lt;/code&gt; file must take account of this. Suppose your source folder is &lt;code&gt;root&lt;/code&gt;. Then your excludes file would have a line like &lt;code&gt;/root/tmp&lt;/code&gt;. If if your source folder is &lt;code&gt;root/&lt;/code&gt;, then your excludes file needs a line like &lt;code&gt;/tmp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To put this all into an example, suppose you have this file structure and you want to exclude all the red files:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;root/
     important.txt
     &lt;span style="color:red;"&gt;important.txt.bak&lt;/span&gt;
     mysql/
           ecom.db
           &lt;span style="color:red;"&gt;log/mysql.log&lt;/span&gt;
     &lt;span style="color:red;"&gt;photos/thumb/&lt;/span&gt;
     &lt;span style="color:red;"&gt;tmp/note.txt&lt;/span&gt;
     www/
         index.html
         &lt;span style="color:red;"&gt;log/www.log&lt;/span&gt;
         &lt;span style="color:red;"&gt;photos/thumb/&lt;/span&gt;
         tmp/index-ver2.html
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In other words, you want to exclude: * All &lt;code&gt;*.bak&lt;/code&gt; files. * All &lt;code&gt;log&lt;/code&gt; directories. * All &lt;code&gt;photo/thumb&lt;/code&gt; directories. * The top-level &lt;code&gt;tmp&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Then you could either call rsync like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rsync -avz --exclude-from=excludes.txt root backups&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with &lt;code&gt;excludes.txt&lt;/code&gt; as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;*.bak
log
photos/thumb
/root/tmp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or you could call rsync like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rsync -avz --exclude-from=excludes.txt root/ backups&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;with an &lt;code&gt;excludes.txt&lt;/code&gt; like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;*.bak
log
photos/thumb
/tmp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; One more note: You might think that ending a line in the excludes file with a trailing slash would work analogously to naming a source directory with a trailing slash: “include this directory but not its contents.” That way if you were backing up your whole filesystem, you could have a line like &lt;code&gt;/mnt/&lt;/code&gt; that would create a &lt;code&gt;/mnt&lt;/code&gt; directory but ignore all its contents. But in fact, rsync just seems to ignore trailing slashes in the excludes file. If you want to exclude everything in the &lt;code&gt;/mnt&lt;/code&gt; directory, you need a line like this instead: &lt;code&gt;/mnt/*&lt;/code&gt;.&lt;/p&gt;
</content>
  </entry>
</feed>

