Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Posts
    6

    Question 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?

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    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
    done
    Last edited by barriehie; 11-18-2010 at 05:11 AM. Reason: typo

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Posts
    6
    Quote Originally Posted by barriehie View Post
    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
    done
    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?

  4. #4
    Linux 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.

    Code:
    /root> df -hm
    Filesystem           1M-blocks      Used Available Use% Mounted on
    /dev/mapper/VolGroup00-LogVol01
                             38747      3397     33350  10% /
    Try adding the -P option.

    Code:
    /root> df -hmP
    Filesystem         1048576-blocks      Used Available Capacity Mounted on
    /dev/mapper/VolGroup00-LogVol01     38747      3397     33350      10% /

  5. #5
    Just 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

  6. #6
    Just Joined!
    Join Date
    Nov 2010
    Posts
    6
    Quote Originally Posted by lomcevak View Post
    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.

    Code:
    /root> df -hm
    Filesystem           1M-blocks      Used Available Use% Mounted on
    /dev/mapper/VolGroup00-LogVol01
                             38747      3397     33350  10% /
    Try adding the -P option.

    Code:
    /root> df -hmP
    Filesystem         1048576-blocks      Used Available Capacity Mounted on
    /dev/mapper/VolGroup00-LogVol01     38747      3397     33350      10% /

    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.

  7. #7
    Linux 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •