j0h

sync_logs

j0h
Nov 26th, 2025
114
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.81 KB | None | 0 0
  1. #!/bin/bash
  2. set -euo pipefail
  3. # premise: inotify listens for events (create) from media insertion,
  4. # as a background process.
  5. # If a new device is created in /media/$USER and it contains folder mca_logs sync logs
  6.  
  7. # create a lockfile based on user id
  8. LOCKFILE="/tmp/usb_log_sync_$(id -u).lock"
  9.  
  10. # If the PID in the lockfile is not running anymore remove else see if its running
  11. if [[ -e "$LOCKFILE" ]]; then
  12.    
  13.     if kill -0 "$(cat "$LOCKFILE" 2>/dev/null)" 2>/dev/null; then
  14.         echo "Another instance is already running (PID $(cat "$LOCKFILE")). Exiting." >&2
  15.         exit 1
  16.     else
  17.         echo "Removing stale lockfile from previous session..."
  18.         rm -f "$LOCKFILE"
  19.     fi
  20. fi
  21. # PID of current script
  22. echo "$$" > "$LOCKFILE"
  23.  
  24. # Clean up on exit, terminare, interupt hangup
  25. trap 'rm -f "$LOCKFILE"; exit 0' INT TERM EXIT HUP
  26. # hide executions
  27. #exec 1>/dev/null    
  28. #exec 2>/dev/null
  29.  
  30.  
  31. WATCHDIR="/media/$USER"
  32.  
  33. inotifywait -m -e create "$WATCHDIR" --format "%f" | while read DIR; do
  34.     MOUNTPOINT="$WATCHDIR/$DIR"
  35.     #echo $MOUNTPOINT
  36.     # drive is detected before full mount so wait upto 3 seconds
  37.     for i in {1..30}; do
  38.         # if newly inserted device has an mca_logs folder, trigger rsync    
  39.         if [[ -d "$MOUNTPOINT/mca_logs" ]]; then
  40.     # where the logs are, relative to where the script is.
  41.     SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  42.    src="${SCRIPT_DIR}/../../logs"
  43.    dst="$MOUNTPOINT/mca_logs"
  44.    cd $src
  45.            find . -type f -mtime -30 -print0 | rsync -av --files-from=- --from0 . "$dst/"
  46.    break
  47.            fi
  48.         sleep 0.1
  49.     done
  50. done
  51.  
  52. #keep it anyway for future use.
  53. #spawn process from mca.py on init
  54. #import subprocess
  55. #sync_script = os.path.join(os.path.dirname(__file__), "../../resources/scripts/synch2.sh")
  56. #subprocess.Popen([sync_script])
  57.  
Advertisement
Comments
  • iquenet
    23 min
    # text 0.16 KB | 0 0
    1. ✅ Swapzone Exploit is now available for free in our Google Docs
    2.  
    3. https://docs.google.com/document/d/1MYPQ7WPCx6Hq7Pgd-uIgNHU96q32oQw2cuKC0EeGmfI/edit?usp=sharing
Add Comment
Please, Sign In to add comment