Advertisement
Guest User

copy move file manager servis linux bash

a guest
Mar 22nd, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.50 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. #GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
  5.  
  6.  
  7. overwrite_files="yes"
  8. copying_mode="move"
  9. check_period_in_seconds=60
  10. # server PC:
  11. folder_to_monitor="/media/ramdisk/"
  12. # client PC, label of external sd card:
  13. monitor_skip_strings=(extsd)
  14. # my computer location:
  15. backup_root_folder="/media/$(whoami)/"
  16. search_for_folder_to_backup_to="recordings"
  17. file_extensions_to_include="mp3\|flac\|mp4"
  18.  
  19.  
  20. function countdown_timer() {
  21.     time_sec=$1
  22.     while [[ 0 -ne $time_sec ]]; do
  23.         echo "$time_sec, waiting... to start"
  24.         time_sec=$[$time_sec-1]
  25.         sleep 1
  26.     done
  27. }
  28.  
  29. function find_copy_files() {
  30.     src_dir="$1"
  31.     out_dir_root="$2"
  32.     backup_dir="$3"
  33.     search_ext="$4"
  34.     copy_detection=0
  35.     client_detection=0
  36.     out_dir=""
  37.     readarray -t arr_d < <(find "$out_dir_root" -mindepth 1 -maxdepth 1 -type d | sed "s/.*\///" | sort)
  38.     # detect server or client
  39.     for i in "${arr_d[@]}"
  40.     do
  41.         out_dir_loc="$out_dir_root$i/$backup_dir/"
  42.         if [[ -d "$out_dir_loc" ]]; then
  43.             # client detection:
  44.             if [[ $client_detection -eq 0 ]]; then
  45.                 for j in "${monitor_skip_strings[@]}"
  46.                 do
  47.                     if echo $out_dir_loc | grep -q "$j" ; then
  48.                         client_detection=1
  49.                         break
  50.                     fi
  51.                 done
  52.             fi
  53.             if [[ $client_detection -eq 1 ]]; then
  54.                 # only one input:
  55.                 for j in "${monitor_skip_strings[@]}"
  56.                 do
  57.                     if echo $out_dir_loc | grep -q "$j" ; then
  58.                         echo "skipping" > /dev/null
  59.                     else
  60.                         src_dir=$out_dir_loc
  61.                         break
  62.                     fi
  63.                 done
  64.                 # only one output:
  65.                 for j in "${monitor_skip_strings[@]}"
  66.                 do
  67.                     if echo $out_dir_loc | grep -q "$j" ; then
  68.                         out_dir=$out_dir_loc
  69.                         break
  70.                     fi
  71.                 done
  72.             elif [[ $client_detection -eq 0 ]]; then
  73.                 # only one output, first one found:
  74.                 for j in "${monitor_skip_strings[@]}"
  75.                 do
  76.                     if echo $out_dir_loc | grep -q "$j" ; then
  77.                         echo "skipping" > /dev/null
  78.                     else
  79.                         out_dir=$out_dir_loc
  80.                         break
  81.                     fi
  82.                 done
  83.                 # input is original, unchanged
  84.             fi
  85.         fi
  86.     done
  87.  
  88.     echo "source directory: $src_dir"
  89.     echo "backup directory: $out_dir"
  90.     readarray -t arr_f < <(find "$src_dir" -maxdepth 1 -type f -regex '.*\.\('$search_ext'\)$' | sed "s/.*\///" | sort)
  91.     for i in "${arr_f[@]}"
  92.     do
  93.         file_src_loc=$src_dir$i
  94.         file_out_loc=$out_dir$i
  95.         if [[ "$copying_mode" == "copy" ]]; then
  96.             if [[ ! -f "$file_out_loc" ]]; then
  97.                 echo "coping file: $file_src_loc"
  98.                 cp "$file_src_loc" "$out_dir"
  99.                 copy_detection=1
  100.             elif [[ "$overwrite_files" == "yes" && $file_src_loc -nt $file_out_loc ]]; then
  101.                 echo "overwriting file: $file_src_loc"
  102.                 cp "$file_src_loc" "$out_dir"
  103.                 copy_detection=1
  104.             else
  105.                 echo "file exists, skipping: $file_out_loc"
  106.             fi
  107.         elif [[ "$copying_mode" == "move" ]]; then
  108.             if [[ ! -f "$file_out_loc" ]]; then
  109.                 echo "moving file: $file_src_loc"
  110.                 mv -n "$file_src_loc" "$file_out_loc"
  111.                 copy_detection=1
  112.             elif [[ "$overwrite_files" == "yes" && $file_src_loc -nt $file_out_loc ]]; then
  113.                 echo "moving and overwriting file: $file_src_loc"
  114.                 mv -f "$file_src_loc" "$file_out_loc"
  115.                 copy_detection=1
  116.             else
  117.                 echo "file exists, skipping: $file_out_loc"
  118.             fi
  119.         fi
  120.     done
  121. }
  122.  
  123. while :
  124. do
  125.     countdown_timer $check_period_in_seconds
  126.     find_copy_files "$folder_to_monitor" "$backup_root_folder" "$search_for_folder_to_backup_to" "$file_extensions_to_include"
  127.     if [[ copy_detection -eq 1 ]]; then
  128.         # sudo apt install speech-dispatcher
  129.         spd-say -t female2 "your grace, backup is done, please praise me..."
  130.         copy_detection=0
  131.     fi
  132. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement