Last active
April 14, 2026 23:09
-
-
Save smj-edison/33229537007a73520a26a14a03bdb979 to your computer and use it in GitHub Desktop.
B2 backup script
This file contains hidden or 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
| #!/bin/bash | |
| # sleep for a bit to give the computer some time to start up (mainly so the notification shows up) | |
| sleep 5 | |
| cd /home/mason/scripts/backup | |
| # set up environment | |
| source ./restic-env.sh | |
| export LOG_FILE="latest_log.txt" | |
| export NOTIF_TITLE="Computer backup" | |
| export X_USERNAME="mason" | |
| export X_USERID="1000" | |
| set -o pipefail | |
| set -e | |
| send_message () { | |
| sudo -u ${X_USERNAME} DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${X_USERID}/bus notify-send -t 5000 "$NOTIF_TITLE" "$1" | |
| echo "sending message: $1" | tee -a "$LOG_FILE" | |
| } | |
| # how long has it been since we last backed up? | |
| time_now=$(date "+%s") | |
| last_time=$(cat "last_backup_time.txt" || echo "0") | |
| date_diff=$((time_now-last_time)) | |
| # 86400 = 24 hours | |
| if [ $date_diff -lt 86400 ]; then | |
| echo "Recently backed up, not backing up now." | |
| send_message "Backup not needed" | |
| exit 0 | |
| fi | |
| # empty the logfile | |
| echo "Log started" > "$LOG_FILE" | |
| # log current time | |
| timedatectl | tee -a "$LOG_FILE" | |
| # check if computer is plugged in before backing up | |
| charging_status=$(acpi -a | cut -d " " -f 3) | |
| case $charging_status in | |
| "on-line") | |
| echo "Computer is charging, backing up now..." | tee -a "$LOG_FILE" | |
| ;; | |
| "off-line") | |
| send_message "Computer is not charging, backup cancelled." | |
| echo "Computer is not charging, backup cancelled." | tee -a "$LOG_FILE" | |
| exit 1 | |
| ;; | |
| esac | |
| # Be sure to unlock before backing up (I've had stale locks be an issue fairly often, and | |
| # since this is a single computer script I'm not worried about accidental contention) | |
| restic unlock | |
| restic -v backup /home/mason --exclude-file=excludes.txt --compression auto | tee -a "$LOG_FILE" | |
| result=$? | |
| echo "result: $result" | tee -a "$LOG_FILE" | |
| if [[ $result != 0 ]]; then | |
| message="Backup failed." | |
| else | |
| message="Backup succeeded." | |
| # only set the last backup time on a successful backup | |
| echo "$time_now" > last_backup_time.txt | |
| fi | |
| # remove old snapshots | |
| restic forget --keep-daily 14 --keep-weekly 4 --keep-monthly 6 --keep-yearly 100 --prune | |
| send_message "$message" | |
| echo "$message" | tee -a "$LOG_FILE" | |
This file contains hidden or 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
| [Unit] | |
| Description=Run backup.sh | |
| After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target | |
| [Service] | |
| ExecStart=/home/mason/scripts/backup/backup.sh | |
| User=root | |
| [Install] | |
| WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target |
This file contains hidden or 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
| /home/mason/anaconda3 | |
| /home/mason/.conda | |
| /home/mason/.local/share/Steam/steamapps/common | |
| /home/mason/.cache | |
| /home/mason/.cargo | |
| /home/mason/.rustup | |
| /home/mason/.npm | |
| /home/mason/.nuget | |
| /home/mason/.platformio | |
| /home/mason/.thunderbird | |
| /home/mason/.config/google-chrome | |
| /home/mason/.android/avd | |
| /home/mason/.gradle/caches | |
| /home/mason/.local/share/mcpelauncher | |
| /home/mason/.virtualenvs | |
| /home/mason/.vscode/extensions | |
| /home/mason/GrandOrgueCache | |
| /home/mason/.steam/steam/steamapps/common/ | |
| /home/mason/.local/share/Trash/ |
This file contains hidden or 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
| export B2_ACCOUNT_ID="ID" | |
| export B2_ACCOUNT_KEY="KEY" | |
| export RESTIC_REPOSITORY="b2:BUCKET" | |
| export RESTIC_PASSWORD_FILE="/home/mason/scripts/backup/restic-password" |
This file contains hidden or 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
| password here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment