Advertisement
atkuzmanov

rclone sync and check bash script 2

Aug 14th, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.23 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Source directories
  4. SRC_DIRS=(
  5.   "localsrc1:/mnt/tns-google-drive-smb"
  6.   "localsrc2:/mnt/tns-maindatads-smb"
  7.   "localsrc3:/mnt/tns-app-configs-smb"
  8.   "localsrc4:/mnt/tns-proxbkps-ds-1"
  9. )
  10.  
  11. # Destination directories for single storage units SSD and NVME
  12. DEST_DIRS_SINGLE_STORE_SSD_NVME=(
  13.   "localdest4:/mnt/sd25-4tb-bkp4-mnt4"
  14. )
  15.  
  16. TEMP_UPLOAD_PATH="/home/usertemp/rclone_cache_temp/tmp_upload"
  17. CHUNK_PATH="/home/usertemp/rclone_cache_temp/chunks"
  18.  
  19. # Transfers and checkers configuration
  20. declare -A TRANSFERS_CONFIG
  21. declare -A CHECKERS_CONFIG
  22. # Populate TRANSFERS_CONFIG and CHECKERS_CONFIG for single storage units SSD and NVME
  23. for dest in "${DEST_DIRS_SINGLE_STORE_SSD_NVME[@]}"; do
  24.   TRANSFERS_CONFIG["$dest"]=4
  25.   CHECKERS_CONFIG["$dest"]=4
  26. done
  27.  
  28. # Function to get the current timestamp for logging
  29. get_timestamp() {
  30.   date '+%Y-%m-%d %H:%M:%S'
  31. }
  32.  
  33. # Function to get the current timestamp for filenames
  34. get_filename_timestamp() {
  35.   date '+%Y%m%d_%H%M%S'
  36. }
  37.  
  38. # Log file setup
  39. LOG_DIR="./logs2"
  40. mkdir -p "$LOG_DIR"
  41. LOG_FILE="${LOG_DIR}/rclone_sync_$(get_filename_timestamp).log"
  42.  
  43. # Function to log messages with a timestamp
  44. log() {
  45.   local message="$1"
  46.   echo "$(get_timestamp) - $message" >> "$LOG_FILE"
  47. }
  48.  
  49. # Function to check files between source and destination
  50. check_files_md5() {
  51.   local src="$1"
  52.   local dest="$2"
  53.  
  54.   # Generate file list for source in parallel
  55.   log "<<<INFO>>> Generating file list for source..."
  56.   find "$src" -type f -print0 | xargs -0 -P 4 -I {} md5sum {} > source_files.md5
  57.  
  58.   # Generate file list for destination in parallel
  59.   log "<<<INFO>>> Generating file list for destination..."
  60.   find "$dest" -type f -print0 | xargs -0 -P 4 -I {} md5sum {} > dest_files.md5
  61.  
  62.   # Compare the file lists
  63.   log "<<<INFO>>> Comparing file lists..."
  64.   diff source_files.md5 dest_files.md5 > rclone_file_diff.log
  65.  
  66.   if [ -s file_diff.log ]; then
  67.     log "<<<ERROR>>> Differences found. Check file_diff.log for details."
  68.   else
  69.     log "<<<INFO>>> File lists match. No differences found."
  70.   fi
  71. }
  72.  
  73. # Function to check files between source and destination
  74. check_files_rclone_check() {
  75.   local src="$1"
  76.   local dest="$2"
  77.   local delay="${3:-0}"  # Optional delay parameter, defaults to 0 if not provided
  78.  
  79.   # Extract base path from destination, including everything before and including the colon
  80.   local dest_base=$(echo "$dest" | sed -E 's/^([^:]+:\/[^\/]+\/[^\/]+).*$/\1/')
  81.   local checkers="${CHECKERS_CONFIG[$dest_base]:-1}"
  82.   log "<<<DEBUG>>> rclone check --checkers: $checkers"
  83.   # local checkers=2;
  84.  
  85.   log "<<<INFO>>> Running rclone check from $src to $dest"
  86.   {
  87.  
  88.     log "<<<DEBUG>>> 1 Rclone check sleep delay = $delay seconds."
  89.  
  90.     rclone check "$src" "$dest" \
  91.       --checkers="$checkers" \
  92.       --fast-list \
  93.       --multi-thread-streams=0 \
  94.       --buffer-size=0 \
  95.       --one-way \
  96.       --checksum \
  97.       --log-file="$LOG_FILE" \
  98.       --log-level=DEBUG \
  99.       --retries 3 \
  100.       --retries-sleep 3s \
  101.       --progress
  102.  
  103.       log "<<<DEBUG>>> 2 Rclone check sleep delay = $delay seconds."
  104.  
  105.       # Optional sleep delay
  106.       if [ -n "$delay" ] && [ "$delay" -gt 0 ]; then
  107.         log "<<<INFO>>> Sleeping rclone check for $delay seconds..."
  108.         sleep "$delay"
  109.       fi
  110.  
  111.     if [ $? -eq 0 ]; then
  112.       log "<<<INFO>>> Rclone check completed successfully for $src and $dest"
  113.     else
  114.       log "<<<ERROR>>> Rclone check failed for $src and $dest."
  115.       # Uncomment the exit 1 below for fail fast debugging:
  116.       # exit 1
  117.     fi
  118.   } >> "$LOG_FILE" 2>&1
  119. }
  120.  
  121. # Function to check if directories exist
  122. check_directories() {
  123.   local src="$1"
  124.   local dest="$2"
  125.  
  126.   if [ ! -d "${src#*:}" ]; then
  127.     log "<<<ERROR>>> Source directory does not exist: ${src#*:}. Skipping."
  128.     return 1
  129.   fi
  130.   return 0
  131. }
  132.  
  133. # Function to perform rclone sync
  134. rclone_sync() {
  135.   local src="$1"
  136.   local dest="$2"
  137.   local use_checksum="${3:-false}"  # Defaults to false if not provided
  138.   local buffer_size="$4"  # Optional
  139.   local bwlimit="$5"  # Optional
  140.   local cache_chunk_total_size="$6"  # Optional
  141.   local cache_tmp_upload_path="$7"  # Optional
  142.   local cache_chunk_path="$8"  # Optional
  143.   local cache_info_age="$9"  # Optional
  144.   local delay="${10:-0}"  # Optional delay parameter, defaults to 0 if not provided
  145.  
  146.   # Extract base path from destination, including everything before and including the colon
  147.   local dest_base=$(echo "$dest" | sed -E 's/^([^:]+:\/[^\/]+\/[^\/]+).*$/\1/')
  148.   # Debug output
  149.   log "<<<DEBUG>>> Full destination path: ${dest#*:}"
  150.   log "<<<DEBUG>>> Base destination directory: $dest_base"
  151.   local transfers="${TRANSFERS_CONFIG[$dest_base]:-4}" # Defaults to default value if not provided
  152.   local checkers="${CHECKERS_CONFIG[$dest_base]:-1}" # Defaults to default value if not provided
  153.   log "<<<DEBUG>>> rclone sync --transfers: $transfers"
  154.   log "<<<DEBUG>>> rclone sync --checkers: $checkers"
  155.  
  156.   # Determine if checksum should be used based on the use_checksum parameter
  157.   local checksum_option=""
  158.   if [ "$use_checksum" = true ]; then
  159.     checksum_option="--checksum"
  160.   fi
  161.  
  162.   # Optional parameters
  163.   local buffer_size_option=""
  164.   if [ -n "$buffer_size" ]; then
  165.     buffer_size_option="--buffer-size=$buffer_size"
  166.   fi
  167.  
  168.   local bwlimit_option=""
  169.   if [ -n "$bwlimit" ]; then
  170.     bwlimit_option="--bwlimit=$bwlimit"
  171.   fi
  172.  
  173.   local cache_chunk_total_size_option=""
  174.   if [ -n "$cache_chunk_total_size" ]; then
  175.     cache_chunk_total_size_option="--cache-chunk-total-size=$cache_chunk_total_size"
  176.   fi
  177.  
  178.   local cache_tmp_upload_path_option=""
  179.   if [ -n "$cache_tmp_upload_path" ]; then
  180.     cache_tmp_upload_path_option="--cache-tmp-upload-path=$cache_tmp_upload_path"
  181.   fi
  182.  
  183.   local cache_chunk_path_option=""
  184.   if [ -n "$cache_chunk_path" ]; then
  185.     cache_chunk_path_option="--cache-chunk-path=$cache_chunk_path"
  186.   fi
  187.  
  188.   local cache_info_age_option=""
  189.   if [ -n "$cache_info_age" ]; then
  190.     cache_info_age_option="--cache-info-age=$cache_info_age"
  191.   fi
  192.  
  193.   log "<<<INFO>>> Syncing from $src to $dest"
  194.   {
  195.     rclone sync "$src" "$dest" \
  196.       --transfers="$transfers" \
  197.       --checkers="$checkers" \
  198.       --log-level=DEBUG \
  199.       --log-file="$LOG_FILE" \
  200.       --retries 1 \
  201.       --retries-sleep 1s \
  202.       --copy-links \
  203.       --progress \
  204.       --fast-list \
  205.       --use-mmap \
  206.       $checksum_option \
  207.       $buffer_size_option \
  208.       $bwlimit_option \
  209.       $cache_chunk_total_size_option \
  210.       $cache_tmp_upload_path_option \
  211.       $cache_chunk_path_option \
  212.       $cache_info_age_option \
  213.       --delete-during
  214.  
  215.       # Optional sleep delay
  216.       if [ -n "$delay" ] && [ "$delay" -gt 0 ]; then
  217.         log "<<<INFO>>> Sleeping rclone sync for $delay seconds..."
  218.         sleep "$delay"
  219.       fi
  220.  
  221.     if [ $? -eq 0 ]; then
  222.       log "<<<INFO>>> Rclone sync completed successfully for $src to $dest"
  223.     else
  224.       log "<<<ERROR>>> Rclone sync failed for $src to $dest after 1 retry."
  225.       # Uncomment the exit 1 below for fail fast debugging:
  226.       # exit 1
  227.     fi
  228.   } >> "$LOG_FILE" 2>&1
  229. }
  230.  
  231. # Function to sync source and destination directories to single storage units SSD and NVME
  232. sync_sources_and_destinations_to_single_stores_ssd_nvme() {
  233.   USE_CHECKSUM=false
  234.   for DEST_DIR in "${DEST_DIRS_SINGLE_STORE_SSD_NVME[@]}"; do
  235.     for SRC in "${SRC_DIRS[@]}"; do
  236.       if check_directories "$SRC" "$DEST_DIR/$(basename "$SRC")"; then
  237.         # Working 1 for SSD with below parameters + transfers=4 and checkers=1:
  238.         # Working 1 for SSD with below parameters + transfers=2 and checkers=1:
  239.         # rclone_sync "$SRC" "$DEST_DIR/$(basename "$SRC")" "$USE_CHECKSUM" "16M" "10M" "1G"
  240.         rclone_sync "$SRC" "$DEST_DIR/$(basename "$SRC")" \
  241.           "$USE_CHECKSUM" \
  242.           "16M" \
  243.           "1.5M" \
  244.           "" \
  245.           "" \
  246.           "" \
  247.           ""
  248.       fi
  249.     done
  250.   done
  251. }
  252.  
  253. # Function to check sources and destinations
  254. check_sources_and_destinations_to_single_stores_ssd_nvme() {
  255.   # Check source and destination directories to single storage units SSD and NVME
  256.   for DEST_DIR in "${DEST_DIRS_SINGLE_STORE_SSD_NVME[@]}"; do
  257.     for SRC in "${SRC_DIRS[@]}"; do
  258.       check_files_rclone_check "$SRC" "$DEST_DIR/$(basename "$SRC")" "1"
  259.       # Ensure that only one rclone check runs at a time
  260.       wait
  261.     done
  262.   done
  263. }
  264.  
  265. ################################ Program execution start and function calling.
  266.  
  267. # Capture start time
  268. START_TIME=$(date +%s)
  269. START_TIMESTAMP=$(get_timestamp)
  270.  
  271. # Start logging
  272. log "<<<INFO>>> Starting sync process at $START_TIMESTAMP"
  273.  
  274. # Calling function to sync source and destination directories to single storage units SSD and NVME
  275. sync_sources_and_destinations_to_single_stores_ssd_nvme
  276.  
  277. # Calling function to check sources and destinations to single storage units SSD and NVME
  278. check_sources_and_destinations_to_single_stores_ssd_nvme
  279.  
  280. # Capture end time
  281. END_TIME=$(date +%s)
  282. END_TIMESTAMP=$(get_timestamp)
  283. ELAPSED_TIME=$((END_TIME - START_TIME))
  284. ELAPSED_HOURS=$((ELAPSED_TIME / 3600))
  285. ELAPSED_MINUTES=$(((ELAPSED_TIME % 3600) / 60))
  286. ELAPSED_SECONDS=$((ELAPSED_TIME % 60))
  287.  
  288. # Log end time and elapsed time
  289. log "<<<INFO>>> Sync process finished at $END_TIMESTAMP"
  290. log "<<<INFO>>> Total time taken: ${ELAPSED_HOURS}h ${ELAPSED_MINUTES}m ${ELAPSED_SECONDS}s"
  291.  
  292.  
  293.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement