j0h

timeWarp.sh

j0h
Mar 11th, 2026
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #!/bin/bash
  2. # my log file is n the same dir as the script
  3. # this script creates backlogs with times stamps for sync ops
  4. # Re-run with sudo if needed
  5. if [[ $EUID -ne 0 ]]; then
  6. echo "Requesting sudo privileges..."
  7. exec sudo "$0" "$@"
  8. fi
  9.  
  10. set -euo pipefail
  11.  
  12. LOGFILE="mca_log-1.csv"
  13. LOOPS=3000
  14. REAL_USER="${SUDO_USER:-$USER}"
  15. DEST_ROOT="$(pwd)"
  16. SCRIPT_PATH="$(readlink -f "$0")"
  17. SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
  18. SRC_LOG="$SCRIPT_DIR/$LOGFILE"
  19.  
  20. if [[ ! -f "$SRC_LOG" ]]; then
  21. echo "Source log file not found: $SRC_LOG"
  22. exit 1
  23. fi
  24.  
  25. REAL_TS="$(date '+%Y-%m-%d %H:%M:%S')"
  26. REAL_EPOCH="$(date +%s)"
  27.  
  28. echo "Real time saved: $REAL_TS"
  29. echo "Starting ${LOOPS}-day simulation..."
  30.  
  31. cleanup() {
  32. echo "Restoring real time..."
  33. date -s "$REAL_TS" >/dev/null 2>&1 || true
  34. timedatectl set-ntp true >/dev/null 2>&1 || true
  35. systemctl start systemd-timesyncd >/dev/null 2>&1 || true
  36. systemctl start chronyd >/dev/null 2>&1 || true
  37. systemctl start ntpd >/dev/null 2>&1 || true
  38. echo "Done."
  39. }
  40. trap cleanup EXIT
  41.  
  42. # Disable time sync
  43. timedatectl set-ntp false >/dev/null 2>&1 || true
  44. systemctl stop systemd-timesyncd >/dev/null 2>&1 || true
  45. systemctl stop chronyd >/dev/null 2>&1 || true
  46. systemctl stop ntpd >/dev/null 2>&1 || true
  47.  
  48. for ((i=1; i<=LOOPS; i++)); do
  49. FAKE_EPOCH=$(( REAL_EPOCH - i*86400 ))
  50. FAKE_TIME="$(date -d "@$FAKE_EPOCH" '+%Y-%m-%d %H:%M:%S')"
  51.  
  52. echo "[$i/$LOOPS] Setting fake date: $FAKE_TIME"
  53. date -s "$FAKE_TIME" >/dev/null
  54.  
  55. YEAR="$(date '+%Y')"
  56. MONTH="$(date '+%m')"
  57. DAY="$(date '+%d')"
  58.  
  59. DEST_DIR="$DEST_ROOT/$YEAR/$MONTH/$DAY"
  60.  
  61. sudo -u "$REAL_USER" mkdir -p "$DEST_DIR"
  62. sudo -u "$REAL_USER" cp -f "$SRC_LOG" "$DEST_DIR/$LOGFILE"
  63. sudo -u "$REAL_USER" touch -d "$FAKE_TIME" "$DEST_DIR/$LOGFILE"
  64.  
  65. done
  66.  
  67. echo "Simulation complete."
  68.  
Advertisement
Add Comment
Please, Sign In to add comment