Last active
January 22, 2025 15:48
-
-
Save kitingChris/b36c7436583fee4755be48f717880d44 to your computer and use it in GitHub Desktop.
Autorun Backup on mounted disk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DIRECTORIES="/etc /home /usr/local /opt" | |
EXCLUDES="/home/chris/.local/share/Cryptomator" | |
USER=chris | |
MOUNT_LABEL="BackupDisk" | |
ALLOWED_MACHINES="CENTAURI" | |
RSYNC_PARAMETERS="-avPEHh --delete --stats --dry-run" | |
# Ensure rsync is available | |
if ! command -v rsync > /dev/null 2>&1; then | |
echo "rsync is not installed. Please install rsync and try again." | |
exit 1 | |
fi | |
# Ensure the script is run as root | |
if [ "$(id -u)" -ne 0 ]; then | |
echo "This script must be run as root. Please run again with sudo or as root." | |
exit 1 | |
fi | |
# Create a PID file to prevent multiple instances of the script | |
PIDFILE="/var/run/autorun-$MOUNT_LABEL.pid" | |
if [ -e "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then | |
echo "The script is already running (PID: $(cat "$PIDFILE")). Exiting." | |
exit 0 | |
fi | |
# Write the current PID to the PID file | |
echo $$ > "$PIDFILE" | |
# Ensure the PID file is removed on script exit | |
trap 'rm -f "$PIDFILE"' EXIT SIGINT SIGTERM | |
# Check if the script is running on an allowed machine | |
CURRENT_MACHINE=$(hostname) | |
if ! echo "$ALLOWED_MACHINES" | tr ' ' '\n' | grep -Fxq "$CURRENT_MACHINE"; then | |
echo "This script is not allowed to run on this machine ($CURRENT_MACHINE)." | |
exit 1 | |
fi | |
if pidof systemd > /dev/null; then | |
SERVICE_NAME="autorun-$MOUNT_LABEL.service" | |
if ! systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then | |
read -p "No enabled systemd service found for $SERVICE_NAME. Do you want to create and enable the systemd service? (y/n): " response | |
if [ "$response" = "y" ]; then | |
echo "Creating and enabling the systemd service..." | |
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME" | |
sudo tee $SERVICE_FILE > /dev/null <<EOF | |
[Unit] | |
Description=Autorun Backup Script for /media/$USER/$MOUNT_LABEL/ | |
After=media-$USER-$MOUNT_LABEL.mount | |
Requires=media-$USER-$MOUNT_LABEL.mount | |
[Service] | |
Type=oneshot | |
User=root | |
ExecStart=/bin/bash -c '[ -f /media/$USER/$MOUNT_LABEL/autorun.sh ] && /media/$USER/$MOUNT_LABEL/autorun.sh' | |
[Install] | |
WantedBy=media-$USER-$MOUNT_LABEL.mount | |
EOF | |
sudo systemctl daemon-reload | |
sudo systemctl enable $SERVICE_NAME | |
echo "Systemd service created and started successfully." | |
else | |
echo "Systemd service creation skipped." | |
echo Systemd service found $SERVICE_NAME | |
fi | |
fi | |
else | |
echo "Systemd is not running. Skipping systemd service creation." | |
fi | |
# Define the backup destination | |
BACKUP_DIR="/media/$USER/$MOUNT_LABEL/backup" | |
LOGS_DIR="/media/$USER/$MOUNT_LABEL/logs" | |
mkdir -p $BACKUP_DIR | |
mkdir -p $LOGS_DIR | |
TIMESTAMP=$(date +"%Y%m%d_%H%M%S") | |
LOG_FILE="$LOGS_DIR/rsync.$TIMESTAMP.log" | |
sudo -u $USER touch $LOG_FILE | |
for DIR in $DIRECTORIES; do | |
EXCLUDE_PARAMS="" | |
for EXCLUDE in $EXCLUDES; do | |
if [ "${EXCLUDE#"$DIR"}" != "$EXCLUDE" ]; then | |
EXCLUDE_PARAMS="$EXCLUDE_PARAMS --exclude=$EXCLUDE" | |
fi | |
done | |
sudo rsync $RSYNC_PARAMETERS $EXCLUDE_PARAMS $DIR $BACKUP_DIR | tee -a $LOG_FILE | |
done | |
# Print a message indicating the backup is complete | |
echo "Backup completed successfully. Backup files are located in $BACKUP_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment