I have a share backup bash script that works as is, but it uses several lines to do something that can be looped (I'm hoping). I'm thinking it can be much better, and the first step, in my mind, would be to use a loop to cycle through the shares.
In it scurrent state, the script I have calls three bash functions and a python script (that sends email with stderr and stdout) for each share, of which there are several (4 now, and maybe up to 6-8), and it looks like this:
create_log_dir "$share1_backup"
create_log "$share1_backup" "$log_file"
create_rsync_cmd "$log_file"
python backup.py $email_address \
"$rsync_command $rsync_args $share1 $share1_backup"
So the code above would be something like 20-30 lines for all the shares. I was hoping to make a loop that goes through these steps for each share that looks something like this (pseudocode-like example):
for var in list (each $share_var and $log_var)
do
create_log_dir "$share_var"
create_log "$share_var" "$log_var"
create_rsync_cmd "$log_var"
done
I tried this format with some bash options and couldn't get it to work. Still new to bash, so I'm hoping to pick up some cool tips with this one.
Thanks in advance.
UPDATE EDIT
Here are the path and log variables and the for loop I'm using:
#variables
share1="/path/to/share/"
share1_backup="/path/to/share/backup"
# log creation
# If the 'rsync_logs' directory in destination doesn't exist, create it.
create_log_dir()
{
backup_dest=$1
if [ ! -d "$backup_dest/rsync_logs" ]
then
mkdir $backup_dest/rsync_logs
fi
}
# Check for log_file existence; append '_alt' if exists.
create_log()
{
backup_dest=$1
log_file=$2
log_file="$backup_dest/rsync_logs/$(date +"%Y-%m%d")_rsync_output.log"
if [ -f $log_file ]
then
log_file="${log_file}_alt"
fi
}
#loop
for pair in "share1_backup log_file"
do
set -- $pair
share=$1
log=$2
create_log_dir "$share"
create_log "$share" "$log"
create_rsync_cmd "$log"
done
So I tried it with the original answer:
for pair in "share1_backup log_file"
and... with the variable method (neither seemed to work - first one gave error because the directory, share1_backup/rsync_logs didn't exist - it didn't use the variable):
for pair in "${share1_backup} ${log_file}"
This second one resulted in empty log file.
UPDATE EDIT 2
I think the better approach to this, so that the two answers below will both work, is to build the logs somewhere else. I originally thought it'd be nice to save them in the backup destination, but then again, it might be good to save the logs on the machine on which all the shares are mounted, for consistency. Then I can write a script that copies them over periodically, if I must have them on the destination drives. This way, I can build the log_file variable ahead of time, and then use it in the loop, as David K. Hess pointed out.
Thanks again.
list?