LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2003, 12:05 PM   #1
lhoff
Member
 
Registered: Jun 2001
Location: Chicago
Distribution: Mandrake 10.0 Official
Posts: 181

Rep: Reputation: 30
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!
 
Old 05-28-2003, 12:23 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Take a look at stat:
man stat

You can get the filesize using:
stat -c %s FILENAME
 
Old 05-28-2003, 11:37 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backup script raptorman Programming 4 08-22-2005 09:52 AM
What backup script do you use? buldir Linux - Software 1 04-12-2005 02:57 PM
Need a backup script enygma Linux - General 5 11-04-2004 03:49 PM
help with backup script dennis_89 Linux - Networking 2 06-29-2004 09:47 AM
Backup Script imsajjadali Linux - General 7 01-28-2004 03:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration