Watael
This is my adaptation of your script and now works OK.
Code:
1 #!/bin/bash
2 # Thanks to Watael at bashscripts.org: Sandbox
3 # set shell option
4 shopt -s nullglob
5 # because if /Archive/Logs is empty, "${LogFiles[@]}" would expand to
6 # literally '/Archive/Logs/*', instead of nothing.
7 #set Logs directory
8 logs="/Archive/Logs"
9 # set log file name to date
10 fn="$logs/$(date +%Y%m%d-%H%M).log"
11 # set maximum number of log files to keep
12 maxLogs=30
13 # list of directories to backup
14 # Dirlist is an array of directory names
15 DirList=( bash bin C C++ CDIndexes Documents Linux .thunderbird UsefulFiles )
16 # scan through the list of directory names
17 for dir in "${DirList[@]}"
18 do
19 echo -e "\n\n***** From Directory: $HOME/$dir" >> $fn
20 rsync -auv --progress -s "$HOME/$dir" "/Archive" >> "$fn"
21 done
22 # LogFiles is an array of log file names
23 LogFiles=( "$logs"/* )
24 # scan through the list of log file names
25 for (( x=0; x<${#LogFiles[@]}-maxLogs; x++))
26 do
27 # toDeleteFiles is an array of log files to be deleted
28 toDeleteFiles+=( "${LogFiles[x]}" )
29 done
30 # delete all files listed in the array toDeleteFiles if the list is non-zero
31 if [ ${#toDeleteFiles[@]} -gt 0 ] ; then
32 rm "${toDeleteFiles[@]}"
33 fi
34 echo -e "\n\n***** End of Backup" >> $fn
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.
Thanks for your help. I have a lot to learn!
Alan