Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split array into groups of three instead of groups of two

Currently I have this backup script working just fine in Unraid.
I can add as many backup jobs to the array below as I like and the script will loop through these one after the other until all are done.

#!/bin/bash

# backup source to destination
backup_jobs=(
# source                         # destination
"/mnt/user/isos"                 "/mnt/disks/Backup"
"/mnt/user/data"               "/mnt/disks/Backup"
)

# loop through all backup jobs
for i in "${!backup_jobs[@]}"; do

    # get source path and skip to next element
    ! (( i % 2 )) && src_path="${backup_jobs[i]}" && continue

    # get destination path
    dst_path="${backup_jobs[i]}"
    
    #run backup
    rsync -av --delete --log-file=/mnt/disks/Backup/rsync-logs/log.`date '+%Y_%m_%d__%H_%M_%S'`.log --progress --exclude '.Recycle.Bin' "$src_path" "$dst_path"

Now I would like to also have a subfolder for the rsync-logs per backup job.

For that I’d like to add a name to the array for each job – but I cannot figure out how to get the loop to load that 3rd variable per line per loop like the other 2 variables.
🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

backup_jobs=(
    # source                    # destination         # jobname
    "/mnt/user/isos"            "/mnt/disks/Backup"   "isos"
    "/mnt/user/data"            "/mnt/disks/Backup"   "backup"
    )

Anyone able to help? 🙂

>Solution :

Use i % 3 instead of i % 2. This will be 0 for the source path, 1 for the destination path, and 2 for the job name.

for i in "${!backup_jobs[@]}"; do
    case $(($i % 3)) in
        0) src_path="${backup_jobs[i]}"; continue ;;
        1) dst_path="${backup_jobs[i]}"; continue ;;
        2) job_name="${backup_jobs[i]}" ;;
    esac
    
    #run backup
    rsync -av --delete --log-file=/mnt/disks/Backup/rsync-logs/"$job_name"/log.`date '+%Y_%m_%d__%H_%M_%S'`.log --progress --exclude '.Recycle.Bin' "$src_path" "$dst_path"
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading