Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # my log file is n the same dir as the script
- # this script creates backlogs with times stamps for sync ops
- # Re-run with sudo if needed
- if [[ $EUID -ne 0 ]]; then
- echo "Requesting sudo privileges..."
- exec sudo "$0" "$@"
- fi
- set -euo pipefail
- LOGFILE="mca_log-1.csv"
- LOOPS=3000
- REAL_USER="${SUDO_USER:-$USER}"
- DEST_ROOT="$(pwd)"
- SCRIPT_PATH="$(readlink -f "$0")"
- SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
- SRC_LOG="$SCRIPT_DIR/$LOGFILE"
- if [[ ! -f "$SRC_LOG" ]]; then
- echo "Source log file not found: $SRC_LOG"
- exit 1
- fi
- REAL_TS="$(date '+%Y-%m-%d %H:%M:%S')"
- REAL_EPOCH="$(date +%s)"
- echo "Real time saved: $REAL_TS"
- echo "Starting ${LOOPS}-day simulation..."
- cleanup() {
- echo "Restoring real time..."
- date -s "$REAL_TS" >/dev/null 2>&1 || true
- timedatectl set-ntp true >/dev/null 2>&1 || true
- systemctl start systemd-timesyncd >/dev/null 2>&1 || true
- systemctl start chronyd >/dev/null 2>&1 || true
- systemctl start ntpd >/dev/null 2>&1 || true
- echo "Done."
- }
- trap cleanup EXIT
- # Disable time sync
- timedatectl set-ntp false >/dev/null 2>&1 || true
- systemctl stop systemd-timesyncd >/dev/null 2>&1 || true
- systemctl stop chronyd >/dev/null 2>&1 || true
- systemctl stop ntpd >/dev/null 2>&1 || true
- for ((i=1; i<=LOOPS; i++)); do
- FAKE_EPOCH=$(( REAL_EPOCH - i*86400 ))
- FAKE_TIME="$(date -d "@$FAKE_EPOCH" '+%Y-%m-%d %H:%M:%S')"
- echo "[$i/$LOOPS] Setting fake date: $FAKE_TIME"
- date -s "$FAKE_TIME" >/dev/null
- YEAR="$(date '+%Y')"
- MONTH="$(date '+%m')"
- DAY="$(date '+%d')"
- DEST_DIR="$DEST_ROOT/$YEAR/$MONTH/$DAY"
- sudo -u "$REAL_USER" mkdir -p "$DEST_DIR"
- sudo -u "$REAL_USER" cp -f "$SRC_LOG" "$DEST_DIR/$LOGFILE"
- sudo -u "$REAL_USER" touch -d "$FAKE_TIME" "$DEST_DIR/$LOGFILE"
- done
- echo "Simulation complete."
Advertisement
Add Comment
Please, Sign In to add comment