Tar: Difference between revisions

adds documentation section
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight"
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
GNU Tar is everybody's favorite archive file format.
== Use Cases ==
=== Get just one file ===
<code>drush archive-backup (arb)</code> and <code>drush archive-restore (arr)</code> are great commands for making a full site backup prior to changes; that you can use to restore from.  Your entire Drupal instance (code, settings, modules, plus database) are all tar'd into an archive file, and gzip compressed <ref>The example refers to a .tar file.  <code>drush archive-backup</code> creates a .tar.gz file -- meaning it's compressed with gzip.  So, to work with the .tar file, first you would uncompress the .tar.gz file with <code>gunzip file.tar.gz</code></ref>.  Again, one of the things that <code>drush arb</code> does is create a SQL dump file.  What if you want just that dump file from the archive?  The long way would be to extract the whole thing, find and move the bits you want, and then discard the rest.  Or, you could just tell tar to give you the file you want explicitly:
<syntaxhighlight lang="bash">
# look at what's in the archive
tar -tf ~/drush-backups/archive-dump/20150327030955/freephile_drupal.20150327_030955.tar
# request just the SQL dump file (puts it into the current working directory)
tar -xf ~/drush-backups/archive-dump/20150327030955/freephile_drupal.20150327_030955.tar freephile_drupal.sql
</syntaxhighlight>
=== Strip top level of the archive and don't extract a sub-folder ===
The magic tar one-line command to unpack a Drupal distribution over an
The magic tar one-line command to unpack a Drupal distribution over an
existing Drupal directory (without nuking sites) - but keep reading!
existing Drupal directory (without clobbering your "sites" directory)
<source lang="bash">
<syntaxhighlight lang="bash">
tar x --show-omitted-dirs --exclude sites --directory ../work/gr/iic-drupal/ \
tar x --show-omitted-dirs --exclude sites --directory ../work/gr/iic-drupal/ \
--strip 1 -zf drupal-6.11.tar.gz
--strip 1 -zf drupal-6.11.tar.gz
</source>
</syntaxhighlight>


--
Drupal ships a tarball with the first object being a directory named
Drupal ships a tarball with the first object being a directory named
after the release (e.g. drupal-6.11/)
after the release (e.g. drupal-6.11/)
Line 23: Line 35:
But, if you want to do it all in one pass, you can use the combination
But, if you want to do it all in one pass, you can use the combination
of '''strip''' and '''directory''' options:
of '''strip''' and '''directory''' options:
<source lang="bash">
<syntaxhighlight lang="bash">
tar x --directory ~/existing-drupal --strip 1 -zf drupal-6.11.tar.gz
tar x --directory ~/existing-drupal --strip 1 -zf drupal-6.11.tar.gz
</source>
</syntaxhighlight>
But wait!! that command will still overwrite your existing "sites"
 
directory which is where you are supposed to store all your
;<code>--strip 1</code> : removes the first path component of objects in the tar archive
customizations to Drupal -- and you don't want to do that.  The
;<code>--directory</code> : tells tar to change to a particular directory prior to extracting the contents of the archive
recommended procedure is to make backups of your "sites" directory and
 
then copy it back into the distribution that you unpack. I think a
If you're upgrading a Drupal instance, then you already have a "sites" directory.   You can skip over this directory in the tar archive with the '''exclude''' option.  In
better way is to skip over the sites directory.  Don't even extract it
from the tar archive.  You can do this with the '''exclude''' option.  In
fact, you can even have tar show you what you have skipped over with
fact, you can even have tar show you what you have skipped over with
the '''show-omitted-dirs''' option.  So, the best way that I know how to
the '''show-omitted-dirs''' option.  So, the best way that I know how to
unpack a Drupal archive over an existing installation - all in one
unpack a Drupal archive over an existing installation - all in one
easy command - is
easy command - is
<source lang="bash">
<syntaxhighlight lang="bash">
tar x --show-omitted-dirs --exclude sites --directory ../work/gr/iic-drupal/ --strip 1 -zf drupal-6.11.tar.gz
tar x --show-omitted-dirs --exclude sites --directory ../work/gr/iic-drupal/ --strip 1 -zf drupal-6.11.tar.gz
</source>
</syntaxhighlight>
This didn't mess with my .git directory, .gitignore file and another
This didn't mess with my .git directory, .gitignore file and another
top-level file I had called "crossdomain.xml" but it still nuked my
top-level file I had called "crossdomain.xml"
"sites" directory :-(
 
== Warning ==
{{Messagebox |
|type=warning
|text = Although I could not reproduce this behavior, I did once get my "sites" directory deleted.  So, you do have [[backups]] and use [[version control]] right?
}}


I restored it with
I restored it with
<source lang="bash">
<syntaxhighlight lang="bash">
git checkout sites
git checkout sites
</source>
</syntaxhighlight>
and then I did a
and then I did a
<source lang="bash">
<syntaxhighlight lang="bash">
git status
git status
</source>
</syntaxhighlight>
followed by
followed by
<source lang="bash">
<syntaxhighlight lang="bash">
git commit -a -m 'adds changes in Drupal 6.11 security release'
git commit -a -m 'adds changes in Drupal 6.11 security release'
</source>
</syntaxhighlight>


== Documentation ==
== Documentation ==
There is a '''lot''' more option information in the <code>tar --help</code> output
There is a lot more option information in the '''<code>tar --help</code>''' output
than in the man page.  The "info" manual is also online at http://www.gnu.org/software/tar/manual/tar.html providing full documentation for tar.
than in the man page.  Of course if you <code>sudo apt-get install tar-doc</code> then you can view the info manual.  The "info" manual is also online at http://www.gnu.org/software/tar/manual/tar.html providing full documentation for tar.
 
I'll have to test  --keep-newer-files to see if that works.
 
 


== Alternatives ==
== Alternatives ==
Line 73: Line 84:
from
from
http://www.cs.toronto.edu/~murray/code/hacks/untar
http://www.cs.toronto.edu/~murray/code/hacks/untar
<source lang="bash">
<syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash


Line 336: Line 347:
fi
fi
done
done
</source>
</syntaxhighlight>
 
== See Also ==
Sometimes you end up with a directory nested in it's intended destination (like drush arb can leave you with /var/www/drupal/drupal). See the [[Bash]] article for moving a nested directory up a level.
 
{{References}}