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 | existing Drupal directory (without clobbering your "sites" directory) | ||
< | <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 | ||
</ | </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: | ||
< | <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 | ||
</ | </syntaxhighlight> | ||
;<code>--strip 1</code> : removes the first path component of objects in the tar archive | |||
;<code>--directory</code> : tells tar to change to a particular directory prior to extracting the contents of the archive | |||
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 | |||
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 | ||
< | <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 | ||
</ | </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" | 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 | ||
< | <syntaxhighlight lang="bash"> | ||
git checkout sites | git checkout sites | ||
</ | </syntaxhighlight> | ||
and then I did a | and then I did a | ||
< | <syntaxhighlight lang="bash"> | ||
git status | git status | ||
</ | </syntaxhighlight> | ||
followed by | followed by | ||
< | <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' | ||
</ | </syntaxhighlight> | ||
== Documentation == | == Documentation == | ||
There is a | 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. | ||
== 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 | ||
< | <syntaxhighlight lang="bash"> | ||
#!/bin/bash | #!/bin/bash | ||
| Line 336: | Line 347: | ||
fi | fi | ||
done | done | ||
</ | </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}} | |||