Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
- overwrite_files="yes"
- copying_mode="move"
- check_period_in_seconds=60
- # server PC:
- folder_to_monitor="/media/ramdisk/"
- # client PC, label of external sd card:
- monitor_skip_strings=(extsd)
- # my computer location:
- backup_root_folder="/media/$(whoami)/"
- search_for_folder_to_backup_to="recordings"
- file_extensions_to_include="mp3\|flac\|mp4"
- function countdown_timer() {
- time_sec=$1
- while [[ 0 -ne $time_sec ]]; do
- echo "$time_sec, waiting... to start"
- time_sec=$[$time_sec-1]
- sleep 1
- done
- }
- function find_copy_files() {
- src_dir="$1"
- out_dir_root="$2"
- backup_dir="$3"
- search_ext="$4"
- copy_detection=0
- client_detection=0
- out_dir=""
- readarray -t arr_d < <(find "$out_dir_root" -mindepth 1 -maxdepth 1 -type d | sed "s/.*\///" | sort)
- # detect server or client
- for i in "${arr_d[@]}"
- do
- out_dir_loc="$out_dir_root$i/$backup_dir/"
- if [[ -d "$out_dir_loc" ]]; then
- # client detection:
- if [[ $client_detection -eq 0 ]]; then
- for j in "${monitor_skip_strings[@]}"
- do
- if echo $out_dir_loc | grep -q "$j" ; then
- client_detection=1
- break
- fi
- done
- fi
- if [[ $client_detection -eq 1 ]]; then
- # only one input:
- for j in "${monitor_skip_strings[@]}"
- do
- if echo $out_dir_loc | grep -q "$j" ; then
- echo "skipping" > /dev/null
- else
- src_dir=$out_dir_loc
- break
- fi
- done
- # only one output:
- for j in "${monitor_skip_strings[@]}"
- do
- if echo $out_dir_loc | grep -q "$j" ; then
- out_dir=$out_dir_loc
- break
- fi
- done
- elif [[ $client_detection -eq 0 ]]; then
- # only one output, first one found:
- for j in "${monitor_skip_strings[@]}"
- do
- if echo $out_dir_loc | grep -q "$j" ; then
- echo "skipping" > /dev/null
- else
- out_dir=$out_dir_loc
- break
- fi
- done
- # input is original, unchanged
- fi
- fi
- done
- echo "source directory: $src_dir"
- echo "backup directory: $out_dir"
- readarray -t arr_f < <(find "$src_dir" -maxdepth 1 -type f -regex '.*\.\('$search_ext'\)$' | sed "s/.*\///" | sort)
- for i in "${arr_f[@]}"
- do
- file_src_loc=$src_dir$i
- file_out_loc=$out_dir$i
- if [[ "$copying_mode" == "copy" ]]; then
- if [[ ! -f "$file_out_loc" ]]; then
- echo "coping file: $file_src_loc"
- cp "$file_src_loc" "$out_dir"
- copy_detection=1
- elif [[ "$overwrite_files" == "yes" && $file_src_loc -nt $file_out_loc ]]; then
- echo "overwriting file: $file_src_loc"
- cp "$file_src_loc" "$out_dir"
- copy_detection=1
- else
- echo "file exists, skipping: $file_out_loc"
- fi
- elif [[ "$copying_mode" == "move" ]]; then
- if [[ ! -f "$file_out_loc" ]]; then
- echo "moving file: $file_src_loc"
- mv -n "$file_src_loc" "$file_out_loc"
- copy_detection=1
- elif [[ "$overwrite_files" == "yes" && $file_src_loc -nt $file_out_loc ]]; then
- echo "moving and overwriting file: $file_src_loc"
- mv -f "$file_src_loc" "$file_out_loc"
- copy_detection=1
- else
- echo "file exists, skipping: $file_out_loc"
- fi
- fi
- done
- }
- while :
- do
- countdown_timer $check_period_in_seconds
- find_copy_files "$folder_to_monitor" "$backup_root_folder" "$search_for_folder_to_backup_to" "$file_extensions_to_include"
- if [[ copy_detection -eq 1 ]]; then
- # sudo apt install speech-dispatcher
- spd-say -t female2 "your grace, backup is done, please praise me..."
- copy_detection=0
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement