Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:12:19+00:00 2026-05-27T10:12:19+00:00

I have a share backup bash script that works as is, but it uses

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:12:19+00:00Added an answer on May 27, 2026 at 10:12 am

    This approach should work as long as your paths don’t have spaces in them:

    for pair in "share1_var log1_var" "share2_var log2_var"
    do
        set -- $pair
        share_var = $1
        log_var = $2
        create_log_dir "$share_var"
        create_log "$share_var" "$log_var"
        create_rsync_cmd "$log_var"
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several divs that share a common class. If one of these divs
I have a faulty hard drive that works intermittently. After cold booting, I can
Say you have 2 share libraries, lib1.so and lib2.so, that both have libcommon.a statically
I have two controllers which share most of their code (but must be, nonetheless,
I have multiple threads that share use of a semaphore. Thread A holds the
I am developing application that uses shared preferences and content provider. But everytime I
I want to have a share button in my app that will post to
I'm building a music events website and want to have a 'share this event'
I'm writing a Windows Service in C#. I have a samba share on a
I have four classes which share some arrangement of four properties. I have currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.