Quantcast
Channel: Cranky Bit » Open Source
Viewing all articles
Browse latest Browse all 19

Simple Backup and Restore With Tar

$
0
0

I've got a Linux server that acts as a fileserver, among other things. I have hundreds of gigabytes of data on it, accessible to any of my computers but in a simplified fashion because I'm not duplicating the data on each computer, plus backup is more straightforward because I just need to backup the server. And Linux is great for automation, and for raw processing of files in bulk. ;-)

So, how to back up the data?

I have installed Webmin, which makes backing up directories in the filesystem trivial. With a web form, you can specify everything about your backup: What directories to backup, where to save the backup file, whether you want to just tar it or also bzip the archive; it even handles scheduling it as a cron job, notifying you the results via email, and more.

If you were do to this manually, though, your tar command might look something like this:

DOS:
  1. tar -c -f /path/of/archive.tar.bz2 --bzip /path/to/backup

You're telling tar, "Please -create a -file at /path/of/archive.tar.bz2, and along the way, compress it using --bzip. The path to archive is /path/to/backup."

I was backing up a 15GB directory and didn't want to make the server spend the extra time compressing all that data, so to save time I did not use the --bzip argument, and so instead it just creates a tar file which I'd name archive.tar.

Great. We're backing up on a regular basis now. I feel much better.

Oh no, I need to retrieve a single file from my backup! How do I do it?

It would be overkill to extract the whole archive, especially when it's really big like my example, when all you need is a single file, perhaps just a few megabytes in size.

Well, if you were to extract the whole archive, you might type:

DOS:
  1. tar -xvf archive.tar

You're telling tar to extract from the file archive.tar, and be verbose, showing all the files you extract.

Again, that's a waste of time and hard drive space. Instead, --list the archive to find the file you need, and then --extract the exact file by specifying it to tar.

You could just list all the files and try to find your file in the list:

DOS:
  1. tar --list -f archive.tar

Again, a waste of time. Make Linux do the work. I know the file I need to restore is foobar.txt, so tell Linux to return just that file by piping the archive list into grep:

DOS:
  1. tar --list -f archive.tar | grep foobar.txt

It crunches through the archive, looking for that file. Finally, it returns, say, "my/path/to/foobar.txt". Excellent! Now ask tar to extract just that single file from the archive:

DOS:
  1. tar -x -f archive.tar my/path/to/foobar.txt

Now, tar will wade through all the files in the archive, and only extract the single file.


Viewing all articles
Browse latest Browse all 19

Trending Articles