LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Backup script to tar (https://www.linuxquestions.org/questions/programming-9/backup-script-to-tar-182699/)

untoldone 05-17-2004 10:19 PM

Backup script to tar
 
Hi, I was attempting to write my first 'real' script today and had an issue with it. The script is suposed to take a directory, go through all the files in it and place the files into several 1 Gig sized tar files. It seems to divide the files up ok, but when it gets to where it runs the tar ... tar gives me errors about not being able to find the files. When i echo the tar command the script creates and then just copy the command into the console, the tar runs without a problem. Thanks for your time.

Code:

#!/bin/bash

check=$1                # directory to check
backdir=$2              # directory for backupfiles
maxsize=1000000        # size in KB of one tar

tarlist=""              # will contain space delimited file names
comsize=0              # total (complete) size of all files
i=1                    # file count

for files in $check/*
do
        size_w=`du -s "$files"`
        cursize=${size_w%*$files*}      # find the size of the current file
        if [ `expr $comsize + $cursize` -ge $maxsize ]
        then
                tarlist="$tarlist $files"
                totar="$backdir/$i.tar $tarlist"
                cmd="tar -cf $totar"
                echo $cmd
                $cmd
                tarlist=""
                comsize=0
                i=`expr $i + 1`
        else
                comsize=`expr $comsize + $cursize`
                tarlist="$tarlist \"$files\""
                i=`expr $i + 1`
        fi
done

totar="$backdir/$i.tar $tarlist"
cmd="tar -cf $totar"
echo $cmd
$cmd


jlliagre 05-18-2004 05:38 PM

just replace your last line by:
eval $cmd
and it will work better.

untoldone 05-20-2004 03:03 PM

thanks much, that did it


All times are GMT -5. The time now is 02:21 AM.