How do I tar/gzip and then delete directories somewhat safely?

3

2

How should I tar/gzip and delete files somewhat safely?

Background: We're running a little low on disk space on a Ubuntu computer (Hardy Heron), and I want to tar/gzip, verify that it worked, and then delete, some directories I'm no longer using. I'm planning on making a tar gzip for each directory I'm getting rid of. (The gzips will be to the hard drive, not a magnetic tape)

I came across a solution that ought to be safe but isn't at unix.com. It isn't safe because in current versions of tar, --remove-files can delete files even if the archive isn't created. (Acknowledged as a bug in this email)

I've also noticed a --compare option, but it says that it "ignores files in the file system that do not have corresponding members in the archive".

Also, does gzipping interfere with tar's ability to check against the existing files?

Andrew Grimm

Posted 2009-11-10T23:14:37.340

Reputation: 2 230

Answers

3

You can configure rsync to --delete-after transferring. It also includes many other options - i.e. you could avoid the tar / gzip step altogether. Use rsync to backup your stuff and delete the local copy. For some detailed instructions see scrounge.org and mikerubel.org and this.

DaveParillo

Posted 2009-11-10T23:14:37.340

Reputation: 13 402

I'm not using multiple computers. – Andrew Grimm – 2009-11-10T23:47:33.357

rsync does not require 2 computers. You can define the source & destination to whatever you want. A local rsync is perfectly legal. – DaveParillo – 2009-11-11T02:06:08.453

1

I use this script for my own use:

#!/bin/ksh
#
# @(#)$Id: save.sh,v 2.2 2007/09/01 23:41:23 jleffler Exp $"
#
# Compress directories to save space

case $(id | sed 's/^uid=\([0-9][0-9]*\)(.*/\1/') in
0)  echo "$(basename $0 .sh): cannot run safely as root!" 1>&2
    exit 1;;
esac

for d in ${*:-`pwd`}
do
    (
    set -e
    echo $d
    cd $d
    b=`basename $d`
    chmod 755 .
    [ ! -d Safe ] && mkdir Safe
    chmod 755 Safe
    # GNU Tar Only!
    find . -type f -print | grep -v '^./Safe' | tar -czf Safe/$b.tgz -T -
    chmod 444 Safe/$b.tgz
    chmod 555 Safe
    (set +e; rm -fr `pwd` 2>/dev/null ; exit 0)
    chmod 755 Safe
    mv Safe/* .
    rmdir Safe
    chmod 555 .
    )
done

Note that 'root' can delete directories even when the owner can't - so it refuses to let 'root' run it. Try it out on a copy of your directory before risking it on anything important. I'm happy with it - you may prefer something else.

One day, I'll make it work with 'bzip2' or other compressors.

Jonathan Leffler

Posted 2009-11-10T23:14:37.340

Reputation: 4 526