macosbackupplistlaunchd

Run remote backup on regulary base


I wrote a shell script which checks if a remote server is reachable. If it is reachable, it runs via ssh a command at a different server. After that, it does some rsync tasks.

Everything works well, so I wrote a plist-files to run the whole staff every 4 hours.

What's work:

My problem:

If the job starts during the macbook sleeps, the ping works well, but the ssh command doesn't come back. The command is executed at the server, but the script stops there. If I kill the process with the ssh-command the script finish well.

What can I do to get more information. What could be the error? What could be another solution (Timemachine is not an option, because I want a general readable backup)?

Mac OS BigSur 11.4 M1-Chip

Here my plist-files

?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
        <dict>
                <key>Label</key>
                <string>de.stanetz.backup</string>
                <key>Program</key>
                <string>/usr/local/sbin/remoteBackup.nsh</string>
                <key>RunAtLoad</key>
                <false/>
                <key>StandardOutPath</key>
                <string>/var/log/launch-stdout.log</string>
                <key>StandardErrorPath</key>
                <string>/var/log/launch-stderr.log</string>
                <key>Debug</key>
                <true/>
                <key>StartCalendarInterval</key>
                <array>
                        <dict>
                                <key>Hour</key>
                                <integer>9</integer>
                                <key>Minute</key>
                                <integer>15</integer>
                        </dict>
                </array>
                <key>ExitTimeOut</key>
                <integer>360</integer>
                <key>TimeOut</key>
                <integer>360</integer>
        </dict>
</plist>

Update: I changed the ssh command to ssh -vv pooh 'echo "this runs remotely"' >/tmp/so-debug.log 2>&1. Which works fine. If I call debug1: Sending command: /usr/local/sbin/handleBackupPlatte.nsh open via ssh (which runs 10 seconds), the complete shell-script finished, but I found in the log:

debug2: channel 0: send eof
debug2: channel 0: input drain -> closed
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 2 clearing O_NONBLOCK
Killed by signal 15.

The duration of time till the remote-script finished seems to be the problem.

#!/bin/bash
echo "this runs remotely"
sleep 10
echo "for 10 seconds"

shows the effect. But I can't make the script faster :-/ Any ideas, except to send the ssh command to background?

Update 2: It seems that every network call which needs 10 seconds will hang and never finished if the mac book is in sleep-state.


Solution

  • I switch to a different approach. I install sleepwatcher which notify if a macbook is wake up. Furthermore, I combined this with a script which ensures that not on every wake up the backup is running. Here the script

    #!/bin/bash
    #set -x
    set -eu
    
    
    ## RETURN_CODES
    # 11 -> BACKUP_ALREADY_RUNNING
    # 12 -> NIGHT -> No Backup
    # 13 -> Last backup is too young.
    
    #VARIABLES
    LOCK_FILE="/tmp/.backup.lock"
    LAST_SUCCESS_FILE="/var/log/.backup.last_success"
    RUN_CONTROL="/var/log/throttle.log"
    MIN_BACKUP_AGE=240
    
    cleanup() {
            exitCode=$?
            date +"Exit Throttle at  %d.%m.%Y %H:%M:%S with $exitCode" >> ${RUN_CONTROL}    
            
            if [[ "$exitCode" -ne 11 ]]; then
                    rm ${LOCK_FILE}
            fi
            echo "$(tail -n 100 ${RUN_CONTROL})" > ${RUN_CONTROL}
            
            exit $exitCode  
    }
    
    trap cleanup EXIT
    
    test -e ${LOCK_FILE} && echo "${LOCK_FILE} exists" && exit 11
    touch ${LOCK_FILE}
    
    hour=$(date +%H)
    
    if [ "$hour" -gt 22 ] || [ "$hour" -lt 7 ]; then
        exit 12
    fi
    
    test -e "$(find ${LAST_SUCCESS_FILE} -mmin -${MIN_BACKUP_AGE})" && exit 13 || true
    
    /usr/local/sbin/remoteBackup.nsh
    echo "$(tail ${LAST_SUCCESS_FILE})" > ${LAST_SUCCESS_FILE}
    date >> ${LAST_SUCCESS_FILE}
    

    and the /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
            <key>Label</key>
            <string>de.bernhard-baehr.sleepwatcher</string>
            <key>ProgramArguments</key>
            <array>
                    <string>/opt/homebrew/sbin/sleepwatcher</string>
                    <string>-V</string>
                    <string>-w /usr/local/sbin/throttledBackup.sh</string>
            </array>
            <key>RunAtLoad</key>
            <true/>
            <key>KeepAlive</key>
            <true/>
            <key>StandardOutPath</key>
            <string>/var/log/sleepwatcher-stdout.log</string>
            <key>StandardErrorPath</key>               
        <string>/var/log/sleepwatcher-stderr.log</string>
    </dict>
    </plist>