Results 1 to 7 of 7
Hi. I have a script that uses rsync to create several daily backups of selected folders. I have an external Firewire drive that's partitioned into two volumes---with one volume for ...
- 11-17-2010 #1Just Joined!
- Join Date
- Nov 2010
- Posts
- 6
Seeking help w/BASH script
Hi. I have a script that uses rsync to create several daily backups of selected folders. I have an external Firewire drive that's partitioned into two volumes---with one volume for the rsync backup. In the script, before it runs rsync, it tars the previous backup into a .tgz file stored on the second volume, thus leaving me a number of backup sets. I use several launchd agents to run the backup at pre-defined times. Everything in this script works fine.
As the second volume fills, I now want to add to my script so that when available space is under 20GB, it begins deleting the oldest backup sets until there's at least 20GB free. This is where I'm stuck. I've figured out that I can use this command that will find out how much free space is left:
FREESPACE = `df -hm | grep '/Volumes/backup_02' | awk '{print $4}'`
but I'm not sure how to structure a while, until, or for loop to just delete enough of the oldest .tgz backups to leave me with 20GB of available space.
Suggestions? Ideas?
- 11-17-2010 #2
Something like: (pseudocode going on here!)
Code:declare -i FREESPACE FREESPACE = `df -hm | grep '/Volumes/backup_02' | awk '{print $4}'` while [ $FREESPACE -lt 20GB ] do ls -1tr | head --lines=1 | read line file=$line rm /path_to_file/$file doneLast edited by barriehie; 11-18-2010 at 05:11 AM. Reason: typo
- 11-19-2010 #3Just Joined!
- Join Date
- Nov 2010
- Posts
- 6
This isn't working yet. I've got:
cd /Volumes/backup_02
FREESPACE=`df -hm | grep '/Volumes/backup_02' | awk '{print $4}'`
while [ $FREESPACE -lt 10240 ]
do
ls -1 | read FILETODELETE
rm -f $FILETODELETE
FREESPACE=`df -hm | grep '/Volumes/backup_02' | awk '{print $4}'`
done
More ideas and suggested code?
- 11-19-2010 #4Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
hmm, the manpage for df doesn't list the -m option but it still works. Anyway your problem may be that the Filesystem is so long that the rest of the info is listed the another line.
Try adding the -P option.Code:/root> df -hm Filesystem 1M-blocks Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol01 38747 3397 33350 10% /
Code:/root> df -hmP Filesystem 1048576-blocks Used Available Capacity Mounted on /dev/mapper/VolGroup00-LogVol01 38747 3397 33350 10% /
- 11-19-2010 #5Just Joined!
- Join Date
- Nov 2010
- Posts
- 6
OK, I've just tried the following. It at least deletes files, BUT it deletes all files and doesn't break out of the loop as I think it should.
for i in *.tgz
do
rm -f $i
FREESPACE=`df -hm | grep '/Volumes/backup_02' | awk '{print $4}'`
if ($FREESPACE -lt 10240); then
break
fi
done
- 11-19-2010 #6Just Joined!
- Join Date
- Nov 2010
- Posts
- 6
I don't think the problem is with my FREESPACE command:
FREESPACE=`df -hm | grep '/Volumes/backup_02' | awk '{print $4}'`
but is with the loop processing the files. If I use "echo $FREESPACE" just after the above command, it correctly shows the amount of free megabytes.
- 11-19-2010 #7Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
OK. Your code looks correct. I'd suggest running the script in verbose mode so you can see what it's doing. Redirect the output to a file because you'll get a lot.
Code:sh -vx /path/to/script > /tmp/log 2>&1


Reply With Quote
