How to Create Bash Menu for Listed Folders

I have created a simple bash script in ubuntu to backup my projects:

#!/bin/bash

save_backup_path=~/Downloads/my_backup
web_projects_path=~/Downloads/my_backup/projects

cd $web_projects_path || return

for f in *; do
    if [ -d "$f" ]; then
        echo "backup project $f";
        tar -zcf $save_backup_path/"$f"-$(date +%d-%m-%Y).tar.gz --exclude="$f"/node_modules --exclude="$f"/vendor --exclude="$f"/storage/framework/views/*.php --exclude="$f"/public/uploaded "$f"
    fi
done

it will create a tar.gz for each folder (project name).

What I want is to create a menu and select which project I want to create backup ex: (1, 2, 3 ... or * for all)

I don't know how to handle the select and do the job

I have used this for storing all the projects names (folders names)

for f in *; do
  if [ -d "$f" ]; then
    readarray -t project_names < <(echo "$f")
  fi
done

but when listing the select

echo "Select project for backup:"
select choice in "${project_names[@]}"; do
  [[ -n $choice ]] || { echo "Invalid choice. Please try again." >&2; continue; }
  break
done

it shows only the last one (the last folder name).

How can I do this to list all projects and create the tar.gz for selected one or all if selected is *

*** UPDATE ****

#!/bin/bash

### backup the projects

save_backup_path=~/Downloads/my_backup
web_projects_path=~/Downloads/my_backup/projects

cd $web_projects_path || return

declare -a project_names
i=1
for d in */
do
    project_names[i++]="${d%/}"
done

echo "Select project for backup:"
select choice in "${project_names[@]}"; do
  [[ -n $choice ]] || { echo "Invalid choice. Please try again." >&2; continue; }
  break
done

echo "backup project $choice ..."
tar -zcf $save_backup_path/"$choice"-$(date +%d-%m-%Y).tar.gz --exclude="$choice"/node_modules --exclude="$choice"/vendor --exclude="$choice"/storage/framework/views/*.php --exclude="$choice"/public/uploaded "$choice"

I have created the selected with choices and now it works for the selected project only. How can I handle for all projects ? If the user select * to make them all (separately)

1

1 Answer

I figured out how to backup all projects. Adding * to the existing array

Here is the final version:

#!/bin/bash

save_backup_path=~/Downloads/my_backup
web_projects_path=~/Downloads/my_backup/projects

cd $web_projects_path || return

declare -a project_names
i=1
for d in */
do
    project_names[i++]="${d%/}"
done
project_names[i++]="*"

echo "Select project for backup:"
select choice in "${project_names[@]}"; do
  [[ -n $choice ]] || { echo "Invalid choice. Please try again." >&2; continue; }
  break
done

backup_project() {
  echo "backup project $1 ..."
  tar -zcf $save_backup_path/"$1"-$(date +%d-%m-%Y).tar.gz --exclude="$1"/node_modules --exclude="$1"/vendor --exclude="$1"/storage/framework/views/*.php --exclude="$1"/public/uploaded "$1"
}

if [ "$choice" == "*" ]; then
    unset project_names[-1]
    for project in "${project_names[@]}"; do
      backup_project "$project"
    done
  else
    backup_project "$choice"
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest