I have written this to backup selected directories from my /home directory to a separate partition mounted at /Archive. The backup script runs every day at 12 midday and 8 pm. There is a log of each run kept up to a maximum of 30 log files.

############################################################### Remove earlier log files so we hold only a specified maximum#

# Go to the Logs directorycd /Archive/Logs

# Maximum number of logs to keepmax=30

# Get the number of files in the directoryx=`ls -u1 | wc -l`

# do while the number of logs is greater than the maximumwhile [[ $x > $max ]]do # Get the name of the oldest file and remove it y=`ls -u1 | tail -1` rm $y # Get the new count of files in the directory x=`ls -u1 | wc -l`done

############################################################## End of Script#############################################################

I have added several comments to remind myself of what is going on. I am not used to the fact that a variable can be used as an array without specifically defining it as such as in C, C++, BASIC and so on.

In your code "Archive/${dir%/*}" produced a new directory within the destination directory (eg /Archive/bin/bin) instead of just copying the files to the existing directory (/Archive/bin). So I changed it as in the code above in line 20.

Also if the number of logs was less than the maximum then toDeleteFiles became "" which the command rm disliked. So I added the test in line 31.

Who is online

Users browsing this forum: No registered users and 15 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum