Wednesday, August 10, 2005

tar in pieces

Ever needed to create a tar file that has multiple pieces ?
tar has some support for it, but it's pretty cumbersome.

Essentially, tar supports the "-L" switch which allows you to specify the maximum length before changing tapes. But you can use that to change files.
We will also need the "--new-volume-script" switch which will run a script every time we reached the max size. Finally, "--volno-file" is useful to track what volume you're on.
The basic trick is to rename/prepare the next file between volumes using the script tar will call.

The whole thing can be solved with 4 small scripts :
backup.sh - runs the backup command itself (and takes care of the last file).
pack.sh - gets called from backup.sh to handle each volume
restore.sh - used for restore
unpack.sh - gets called from restore.sh to handle volumes.

First backup :

backup.sh
========================
#!/bin/bash
if [ ! $# -eq "3" ]
then
echo "Usage $0 archive_name max_size_in_KB target_dir"
exit
fi
# remove volume counter
rm -rf volno
#create tar file
tar cvf $1 -L $2 --volno-file volno --new-volume-script "pack.sh $1" $3
# handle the last file
last_file_name=$1__`cat volno`.tar.gz
gzip -c $1 > $last_file_name
rm -rf $1
# END OF backup.sh


pack.sh
============
#!/bin/bash
#
if [ ! $# -eq "1" ]
then
echo "Usage: $0 file_name"
exit
fi
let "archive_no=`cat volno`-1"
echo Processing Archive $archive_no...
gzip -c $1 > $1__$archive_no.tar.gz
#END of pack.sh


and to restore it:

restore.sh
===========
#!/bin/bash
if [ ! $# -eq "1" ]
then
echo "Usage $0 first_archive_name_(without __X.tar.gz)"
exit
fi
# remove volume counter
rm -rf volno
#unzip the first file
gunzip -c $1__1.tar.gz > $1
#restore tar file
tar xvf $1 --volno-file volno --new-volume-script "/root/drm_backup/unpack.sh $1"
# remove temp file
rm -rf $1
# End of restore.sh

unpack.sh
==========
#!/bin/bash
#
if [ ! $# -eq "1" ]
then
echo "Usage: $0 file_name"
fi
let "archive_no=`cat volno`"
echo Processing Archive $archive_no...
gunzip -c $1__$archive_no.tar.gz > $1
# End of unpack.sh

SSH keys - automatic login failure (still getting password prompt)

One of the coolest features of SSH, is its support for secure keys. You can create public and private keys, and then no password is required for login from a certain host, while still maintaining a secure connection.
This is very useful for scripts.

There are a million guides on the web how to do it .. (just Google "how to setup ssh keys") but they many fail to mention one thing! What to do if you created/updated the "authorized_keys" file and the authentication still fails ??

Well, it turns out that SSHd is very picky when it comes to permissions on the file. It will FAIL (!!) if the authorized_keys permissions are not 600, or the directory is not 755. So if you updated the authorized_keys file, and you still get a password prompt. Ensure that your permissions are set as follows:

$ chmod 755 $HOME/.ssh (or better yet, 700)
$ chmod 600 $HOME/.ssh/authorized_keys

And try again ..
Good luck!