anon
anon

Reputation:

Compressing single file instead of whole directory

Hi guys i've been trying for hours to get my script working, the issue is that instead of compressing a single file, it compresses the whole directory structure.

    #!/bin/sh

# Where to backup to.
mkdir -p /home/knoppix/backups --verbose
dest="/home/knoppix/backups"

# What to backup.
cat /etc/passwd >> /home/knoppix/backups/dbusers.txt
backupdb="/home/knoppix/backups/dbusers.txt"

# Create archive filename.
archive_file="DB$(date +%d%b%Y_%H%M).tgz"

# Print start status message.
echo "Backing up $backupdb to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backupdb

Upvotes: 1

Views: 2935

Answers (2)

GargantuChet
GargantuChet

Reputation: 5779

Try using tar -C to move into the directory:

#!/bin/sh

# Where to backup to.
mkdir -p /home/knoppix/backups --verbose
dest="/home/knoppix/backups"

# What to backup.
cat /etc/passwd >> /home/knoppix/backups/dbusers.txt
backupdir="/home/knoppix/backups"
backupfile="dbusers.txt"

# Create archive filename.
archive_file="DB$(date +%d%b%Y_%H%M).tgz"

# Print start status message.
echo "Backing up $backupdb to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file -C $backupdir $backupfile

GNU tar also provides a --transform option, which can be used to tweak the name for archive creation or extraction. Another way to change your original script would be to do something like:

#!/bin/sh

# Where to backup to.
mkdir -p /home/knoppix/backups --verbose
dest="/home/knoppix/backups"

# What to backup.
cat /etc/passwd >> /home/knoppix/backups/dbusers.txt
backupdir="/home/knoppix/backups"
backupfile="dbusers.txt"

# Create archive filename.
archive_base_name="DB$(date +%d%b%Y_%H%M)"
archive_file="$archive_base_name.tgz"

# Print start status message.
echo "Backing up $backupdb to $dest/$archive_file"
date
echo

# Backup the files using tar.
tar --transform="s#^$base#$archive_base_name#" czPf $dest/$archive_file $backupdb

The argument to --transform says to substitute one string of text for another, like s#old#new#. The old portion is actually a pattern called a regular expression, and ^ will cause the regex to match only at the beginning of a line. And the P (in czPf) tells tar not to remove the '/' from the beginning of paths. So in this example, the tar will be created with something like:

DB11Nov2011_0555/dbusers.txt

If you have an existing archive containing a file named like "dbusers.txt", you can use --transform to extract it to another name or directory:

tar --transform="s#dbusers#original#" xzf example.tgz # creates original.txt
tar --transform="s#^#output/#" xzf example.tgz        # creates output/dbusers.txt

Try adding v to the tar options (czPvf) to get it to display the names of files as they are being added or extracted. When using --transform, it might also help to use --show-transformed-names so that the v flag shows the results of the substitution instead of the original names.

Finally, for a bit more troubleshooting help, try adding set -x at the top of the script (or just before the tar command). It will cause the shell to print a command before executing it. The useful part is that it will show the values used, after variables and wildcards are evaluated.

You can even try this on the command line. Use set +x to turn if back off:

$ set -x
$ archive_file="DB$(date +%d%b%Y_%H%M).tgz"
++ date +%d%b%Y_%H%M
+ archive_file=DB11Nov2011_0605.tgz
$ echo $archive_file
+ echo DB11Nov2011_0605.tgz
DB11Nov2011_0605.tgz
$ set +x
+ set +x
$ echo $archive_file
DB11Nov2011_0605.tgz

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409136

How about:

# Backup the files using tar.
(cd /home/knoppix/backups/; tar czf $dest/$archive_file dbusers.txt)

Upvotes: 1

Related Questions