Script for init.d?

Hi,

I found that script on reddit to make an automatic backup and the guy added that script and only wrote all in one for init.d, but I am not so familiar with script, so could you tell me where I need to put it? I guess not in the init.d?

 #!/bin/sh /etc/rc.common
    
    START=99
    STOP=1
    
    
    
    start() {
        echo "Starting OWRT config change watcher service..."
    
    while [ ! -d "$BACKUP_MOUNT" ]; do
        echo "Waiting for $BACKUP_MOUNT to mount..."
        sleep $MOUNT_WAIT
    done
    
    # If the required files and directories don't exist, create them
    if [ ! -f "$REF_FILE" ]; then
        REF_DIR=$(dirname "$REF_FILE")
    
        if [ ! -d "$REF_DIR" ]; then
            echo "Reference file directory does not exist, creating $REF_DIR"
            mkdir -p "$REF_DIR"
        fi
    
        echo "Reference file does not exist, creating $REF_FILE"
        touch "$REF_FILE"
    
    fi
    
    if [ ! -d "$BACKUP_PATH" ]; then
       echo "Backup path does not exist, creating $BACKUP_PATH"
       mkdir -p "$BACKUP_PATH"
    fi
    
        # Check if change watcher service is already running
        if [ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
            echo "Service is already running with PID $(cat "$PID_FILE")."
            return 1
        fi
    
        # Monitoring loop
        (
                    trap "rm -f $PID_FILE; exit 0" INT TERM
            while true; do
                echo "Checking for changes in watched directories..."
                CHANGED_FILES=$(find $WATCHED_DIRS -type f -newer $REF_FILE 2>/dev/null)
    
                if [ -z "$CHANGED_FILES" ]; then
                    echo "No changes detected, waiting for next check..."
                    sleep "$CHECK_INTERVAL"
                    continue
                fi
    
                echo "Changes detected, creating backup..."
                TIMESTAMP=$(date +'%F-%H%M%S-%N')
                sysupgrade -b /mnt/bkps/backup-${HOSTNAME}-${TIMESTAMP}.tar.gz
    
                # Update the reference file to the current time
                touch "$REF_FILE"
                echo "Backup created at mnt/bkps/backup-${HOSTNAME}-${TIMESTAMP}.tar.gz"
            done
        ) &
        echo $! > "$PID_FILE"
        echo "Change watcher service started with process ID $(cat "$PID_FILE")."
    }
    
    stop() {
        echo "Stopping OWRT config change watcher service..."
    
        if [ -f "$PID_FILE" ]; then
            PID=$(cat "$PID_FILE")
            if kill -0 "$PID" 2>/dev/null; then
                kill "$PID"
                echo "Change watcher service with PID $PID stopped."
            else
                echo "No active service found with PID $PID."
            fi
            rm -f "$PID_FILE"
        else
            echo "No PID file found. Service may not be running."
        fi
    }
    
    restart() {
        echo "Restarting OWRT config change watcher service..."
        stop
        start
    }

Since there's a start and stop, it should go into init.d.

1 Like

/etc/init.d/backup start

Might want to do an enable too, unless you want to start it manually after every reboot ...

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.