Skip to content

Instantly share code, notes, and snippets.

@bsormagec
Forked from skrajewski/backup.sh
Created July 12, 2021 21:42
Show Gist options
  • Save bsormagec/23882c44369000dba7a2a8f583ce00b9 to your computer and use it in GitHub Desktop.
Save bsormagec/23882c44369000dba7a2a8f583ce00b9 to your computer and use it in GitHub Desktop.

Revisions

  1. @skrajewski skrajewski revised this gist Oct 16, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backup.sh
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ export B2_ACCOUNT_KEY=$(security find-generic-password -s backup-restic-b2-accou
    export RESTIC_REPOSITORY=$(security find-generic-password -s backup-restic-repository -w)
    export RESTIC_PASSWORD_COMMAND='security find-generic-password -s backup-restic-password-repository -w'

    /usr/local/bin/restic backup --verbose -o b2.connections=20 --files-from ~/include.txt
    /usr/local/bin/restic backup --verbose -o b2.connections=20 --files-from ~/backup.txt

    echo $(date +"%Y-%m-%d %T") "Backup finished"
    echo $(date -v +6H +"%s") > $TIMESTAMP_FILE
  2. @skrajewski skrajewski created this gist Oct 16, 2019.
    48 changes: 48 additions & 0 deletions backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/bash

    PID_FILE=~/.restic_backup.pid
    TIMESTAMP_FILE=~/.restic_backup_timestamp

    if [ -f "$PID_FILE" ]; then
    if ps -p $(cat $PID_FILE) > /dev/null; then
    echo $(date +"%Y-%m-%d %T") "File $PID_FILE exist. Probably backup is already in progress."
    exit 1
    else
    echo $(date +"%Y-%m-%d %T") "File $PID_FILE exist but process " $(cat $PID_FILE) " not found. Removing PID file."
    rm $PID_FILE
    fi
    fi

    if [ -f "$TIMESTAMP_FILE" ]; then
    time_run=$(cat "$TIMESTAMP_FILE")
    current_time=$(date +"%s")

    if [ "$current_time" -lt "$time_run" ]; then
    exit 2
    fi
    fi

    if [[ $(networksetup -getairportnetwork en0 | grep -E "Home-Network|Work-Network") == "" ]]; then
    echo $(date +"%Y-%m-%d %T") "Unsupported network."
    exit 3
    fi

    if [[ $(pmset -g ps | head -1) =~ "Battery" ]]; then
    echo $(date +"%Y-%m-%d %T") "Computer is not connected to the power source."
    exit 4
    fi

    echo $$ > $PID_FILE
    echo $(date +"%Y-%m-%d %T") "Backup start"

    export B2_ACCOUNT_ID=$(security find-generic-password -s backup-restic-b2-accound-id -w)
    export B2_ACCOUNT_KEY=$(security find-generic-password -s backup-restic-b2-account-key -w)
    export RESTIC_REPOSITORY=$(security find-generic-password -s backup-restic-repository -w)
    export RESTIC_PASSWORD_COMMAND='security find-generic-password -s backup-restic-password-repository -w'

    /usr/local/bin/restic backup --verbose -o b2.connections=20 --files-from ~/include.txt

    echo $(date +"%Y-%m-%d %T") "Backup finished"
    echo $(date -v +6H +"%s") > $TIMESTAMP_FILE

    rm $PID_FILE
    27 changes: 27 additions & 0 deletions pl.skrajewski.restic_backup.plist
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?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>pl.skrajewski.restic_backup</string>

    <key>Program</key>
    <string>/Users/szymon/scripts/backup.sh</string>

    <key>RunAtLoad</key>
    <true/>

    <key>StartInterval</key>
    <integer>300</integer>

    <key>WorkingDirectory</key>
    <string>/Users/szymon/</string>

    <key>StandardOutPath</key>
    <string>/Users/szymon/Library/Logs/pl.skrajewski.restic_backup.out.log</string>

    <key>StandardErrorPath</key>
    <string>/Users/szymon/Library/Logs/pl.skrajewski.restic_backup.error.log</string>
    </dict>
    </plist>