LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   backup script (https://www.linuxquestions.org/questions/programming-9/backup-script-62309/)

lhoff 05-28-2003 12:05 PM

backup script
 
I'm interested in writing a script (I've done PHP, ActionScript and JavaScript before, but never Perl or Shell scripts...) to backup files from a directory name that I supply as an argument.

What I want to achieve is this:
1) Script should examine every file in the directory (and it's children) and get the filesize of that file.
2) Add the filesize to a total filesize.
3) Stop at 650MB total and write the list of files examined to a file. That ilst needs to be fed to tar to create an archive of those files.
4) If there are more files in the path to do, continue the 3 steps above and save a 2nd, 3rd, 4th, etc file of filenames, until the path has been thoroughly examined.

I'm using bash. How do I start?

PLEASE, tell me if I need to correct my thinking before I begin. Thanks!

david_ross 05-28-2003 12:23 PM

Take a look at stat:
man stat

You can get the filesize using:
stat -c %s FILENAME

crabboy 05-28-2003 11:37 PM

Not the fastest script around, but it will get you started. If you have a lot of large files you may get more more onto the CD if you use a best fit type of algorithim.

Code:

#!/bin/sh

MAX_SIZE=650000000
IFS='
'

TEMP_NAME=`mktemp tarlist.XXXXXX`
SIZE=0
TAR_LIST=""
TAR_NUM=1

mktar()
{
  TAR_FILENAME="backup-${TAR_NUM}.tar"
  echo "Creating tarfile $TAR_FILENAME size $SIZE"
  tar -cf $TAR_FILENAME -T $TEMP_NAME
  SIZE=0
  TAR_NUM=`expr $TAR_NUM + 1`
  rm -f $TEMP_NAME
}

for FILE in `find . -type f \( ! -name 'backup*.tar' \)  -printf '%p %s\n'`; do

  CURRENT_SIZE=`echo $FILE | cut -d' ' -f2`
  SIZE=`expr $SIZE + $CURRENT_SIZE`

  if [ $SIZE -lt $MAX_SIZE ]; then
      echo $FILE | cut -d' ' -f1 >> $TEMP_NAME
  else
      mktar
  fi
done

if [ $SIZE -gt 0 ]; then
  mktar
fi



All times are GMT -5. The time now is 08:43 PM.