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)
I figured out how to backup all projects. Adding * to the existing array
Here is the final version: