FuBackups

From UoWiki

Jump to: navigation, search

Contents

[edit] Backup Basics

[edit] Tar

Backing up stuff is important.

Prof Ian says: "If it's not worth backing up, it's not worth crying over".

Tar is the standard unix backup app. It stands for Tape Archiver.

It's one of the oldest unix commands around, so it's really esoteric. For example, you have to put your commands in a certain order.

eg: to create an archive:

tar -cf ~/etc/tar etc

this means:

use tar, 'c'reate an archive ('f'ile1) of the etc directory.


so running this command will give us a tar file in the user's home directory called etc.tar, which is (surprise surprise), a backup of the home directory.

you could use this:

tar -tvf - which 't'ells you about stuff 'v'erbosely while it's doing it. Which is very polite.

tar -cvf does the same, but only displays to you 'v'erbosely what it's doing, but doesn't 't'ell you anything else. Or something.

Now, the problem is that tar doesn't store the ownership and permissions of files by default. Which is a bit scary. And will screw up your system. You have to use the 'p' option which stores 'p'ermissions too.

handy mnemonic: 'can I have a 'p' please, bob'. If you don't understand this you never watched Blockbusters, so you don't have any education. Sorry.

[edit] Restoring from backup with Tar.

tar -xvf etc.tar - will 'x'tract your archive 'v'erbosely from a 'f'ile. Into your home directory.

This will look similar, but as you will find out from bitter experience, it is not the same thing. Your permissions will be fucked up.


Here's another command to use:

ls -la /usr/local/etc | head -8

this will shows you the first 8 lines (the head command you pipe into does that bit) of a 'l'ist of 'a'll the files in your directory, with lots of details. You'll see that the permissions and the ownership are very different. ie. if you zip files owned by root, you'll unzip them owned by... you. Which will be a big problem if you are trying to restore a system from a tar backup.


This is not very simple problem to solve. It should be as simple as adding a 'p' to the extract command (given that you were careful to add one to the create command. Unfortunately, unix permissions won't allow you to create files as root if you're just a pissy normal user.

The basics are that you must backup as root, or you can't extract as root. If you want to backup your own stuff that's owned by you - fine, you can do that. But if you're doing your whole system, do it as the root user - ie. sudo it.

[edit] Tada!

So, finally, if you don't want to experience problems, to back up your /usr/local/etc directory, do this:

  1. cd /usr/local
  2. sudo tar -cvpf ~/etc.tar etc

line 1 will 'c'hange 'd'irectory into /usr/local (where etc is located). line 2 will run the tar app to 'c'reate 'v'erbosely with 'p'ermissions to a 'f'ile in your '~'home directory called etc.tar and it will make it from /usr/local/etc.

Clear?

Any questions?

No. Right then.


[edit] Compression

Gzip is a good compression tool.

if we're in our home directory with our new etc.tar file. we can run the following command:

gzip -9 etc.tar

This will use gzip to compress a file (etc.tar) to 9/10ths of it's maximum possible compression ratio. Or something.

NB: This will delete etc.tar and replace it with etc.tar.gz which could be a bit problematic. So be careful with gzip.

That's pretty simple. So now, more Tar.

[edit] More Tar.

Don't do this.

tar -cvf ~/etc.tar /usr/local/etc/

If you do this - you are specifying the absolute path (unlike above, where we were simply in the same directory as the directory we wanted to tar up).

This will create an archive with /usr/local in front of everything.

This may not be a problem, unless you have an automated system that relies on tar. When you extract it it will create a directory called usr and one called /local (it strips the first / off for security reasons).


[edit] Another way to fuck up with Tar.

If we change into the directory we want to back up:

cd /usr/local/etc

then do:

tar -cvs ~/etc.tar *

you'll think you're backing up everything in the directory you want to run.

This is fucked. It won't work. It will look like it's worked, but it hasn't. No error messages or anything. But when you untar it you'll notice that all the files spew out everywhere in the directory you untar into. So.

Always back up a directory, not just a bunch of files - you need a container. The container is a directory. Use it.

The other problem is that '*' doesn't match everything. It won't catch dotfiles or files with dashes in them. So basically, the lesson is. When you use tar to pack up a number of files, tar a directory.

If you really want a containerless tar file, use a dot.

tar -cvf .

This does actually back up all the files, but they'll still be a mess when you untar them.


So there you go - don't put in the absolute path because it will be annoying for you, and for machines that are trying to help you.


[edit] Viewing tar files

When you download an archive you really need to make sure it's not going to do something naughty. So look before you leap:

tar -tvf etc.tar | more

You just pipe it into more, and it shows you what you need to see. Look for weird usernames. Are they necessary? are they naughty? or just stupid? 'apache' as a username is probably ok. 'steve' is not so good. It means the person tarring up the file is called steve, and doesn't know how to use tar to pack up a directory meant for distribution. So you probably don't want to use steve's software anyway.

If it's owned by root, and it's setuid root, it could even be dangerous. So be safe, tar -tvf | more first and think about it.



[edit] Notes

1 unless you have a tape device (at /dev/sa0) this will throw an error without the 'f' - which says, write it to a file, not to tape.

Personal tools