This is my algorithm to backup data on Windows using Bash scripting language. I'm looking forward to any innovations, and better solutions. My task was to create a simple backup algorithm using dry-run and delete. Are there any simpler solutions? Also, adding a GUI would be a pretty nice training for me, but in my case, I'm not too familiar with that.
#!/bin/bash
src=$1
dst=$2
option1=$3
option2=$4
src_files=$(ls $src)
dst_files=$(ls $dst)
files_to_copy () {
files_to_copy=()
for file in $src_files; do
if [[ ! $dst_files =~ $file ]]; then
files_to_copy+=($file)
fi
done
echo ${files_to_copy[@]}
}
files_to_delete () {
to_delete=( )
for file in $dst_files; do
if [[ ! $src_files =~ $file ]]; then
to_delete+=($file)
fi
done
echo ${to_delete[@]}
}
to_copy=$(files_to_copy)
if [[ "$option1" == "del" || "$option2" == "del" ]]; then
to_delete=$(files_to_delete)
fi
dry_run () {
for file in ${to_copy[@]}
do
echo cp $src/$file $dst
done
for file in ${to_delete[@]}
do
echo rm $dst/$file
done
}
run () {
for file in ${to_copy[@]}
do
cp $src/$file $dst
done
for file in ${to_delete[@]}
do
rm $dst/$file
done
}
if [[ "$option1" == "dryrun" || "$option2" == "run" ]]; then
dry_run
else
run
fi
Thank you in advance, and have a nice day.