From ba9c96377330b8b4d3a3f054540aa86492a46e34 Mon Sep 17 00:00:00 2001 From: Dmitri Popov Date: Wed, 22 Aug 2018 17:26:52 +0200 Subject: [PATCH] Add remote-backup.sh. Minor fixes --- scripts/camera-backup.sh | 4 +- scripts/card-backup.sh | 8 ++-- scripts/remote-backup.sh | 89 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100755 scripts/remote-backup.sh diff --git a/scripts/camera-backup.sh b/scripts/camera-backup.sh index 1178018..6c43d2d 100755 --- a/scripts/camera-backup.sh +++ b/scripts/camera-backup.sh @@ -42,11 +42,11 @@ sudo shutdown -c # Create the target directory with the camera model as its name CAMERA=$(gphoto2 --summary | grep "Model" | cut -d: -f2 | tr -d '[:space:]') STORAGE_MOUNT_POINT="$HOME_DIR/$CAMERA" -mkdir -p $STORAGE_MOUNT_POINT +mkdir -p "$STORAGE_MOUNT_POINT" # Switch to STORAGE_MOUNT_POINT and transfer files from the camera # Rename the transferred files using the YYYYMMDD-HHMMSS format -cd $STORAGE_MOUNT_POINT +cd "$STORAGE_MOUNT_POINT" gphoto2 --get-all-files --skip-existing # Shutdown diff --git a/scripts/card-backup.sh b/scripts/card-backup.sh index 19e0d0e..36a8d72 100755 --- a/scripts/card-backup.sh +++ b/scripts/card-backup.sh @@ -51,20 +51,20 @@ sudo sh -c "echo 1000 > /sys/class/leds/led0/delay_on" # Wait for a card reader or a camera CARD_READER=$(ls /dev/* | grep $CARD_DEV | cut -d"/" -f3) -until [ ! -z $CARD_READER ] +until [ ! -z "$CARD_READER" ] do sleep 1 CARD_READER=$(ls /dev/sd* | grep $CARD_DEV | cut -d"/" -f3) done # If the card reader is detected, mount it and obtain its UUID -if [ ! -z $CARD_READER ]; then +if [ ! -z "$CARD_READER" ]; then mount /dev/$CARD_DEV $CARD_MOUNT_POINT # # Set the ACT LED to blink at 500ms to indicate that the card has been mounted sudo sh -c "echo 500 > /sys/class/leds/led0/delay_on" # Create a .id random identifier file if doesn't exist - cd $CARD_MOUNT_POINT + cd "$CARD_MOUNT_POINT" if [ ! -f *.id ]; then random=$(echo $RANDOM) touch $(date -d "today" +"%Y%m%d%H%M")-$random.id @@ -77,7 +77,7 @@ if [ ! -z $CARD_READER ]; then BACKUP_PATH=$STORAGE_MOUNT_POINT/"$ID" # Perform backup using rsync - rsync -ah --exclude "*.id" $CARD_MOUNT_POINT/ $BACKUP_PATH + rsync -ah --exclude "*.id" "$CARD_MOUNT_POINT"/ "$BACKUP_PATH" # Turn off the ACT LED to indicate that the backup is completed sudo sh -c "echo 0 > /sys/class/leds/led0/brightness" diff --git a/scripts/remote-backup.sh b/scripts/remote-backup.sh new file mode 100755 index 0000000..bd0d928 --- /dev/null +++ b/scripts/remote-backup.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# IMPORTANT: +# Run the install-little-backup-box.sh script first +# to install the required packages and configure the system. + +# Create a new configuraion file if it doesn't exist +# and prompt for required values + +CONFIG_DIR=$(dirname "$0") +CONFIG="${CONFIG_DIR}/remote-backup.cfg" + +if [ ! -f "$CONFIG" ]; then + echo "Enter remote user name and press [ENTER]:" + read USER + echo 'USER="'$USER'"' >> "$CONFIG" + echo "Enter remote server IP address or domain name and press [ENTER]:" + read REMOTE + echo 'REMOTE="'$REMOTE'"' >> "$CONFIG" + echo "Specify target backup directory (include the trailing slash) and press [ENTER]" + read BACKUP_DIR + echo 'BACKUP_DIR="'$BACKUP_DIR'"' >> "$CONFIG" + echo "Enter your Notify token and press [ENTER]." + echo "Skip to disable:" + read NOTIFY_TOKEN + echo 'NOTIFY_TOKEN="'$NOTIFY_TOKEN'"' >> "$CONFIG" + fi +# Initialize the configuration file +source "$CONFIG" + +# Specify backup device and its mount points, +# and other settings +STORAGE_DEV="sda1" # Name of the storage device +STORAGE_MOUNT_POINT="/media/storage" # Mount point of the storage device +SHUTD="5" # Minutes to wait before shutdown due to inactivity + +# Set the ACT LED to heartbeat +sudo sh -c "echo heartbeat > /sys/class/leds/led0/trigger" + +# Shutdown after a specified period of time (in minutes) if no device is connected. +sudo shutdown -h $SHUTD "Shutdown is activated. To cancel: sudo shutdown -c" + +# Wait for a USB storage device (e.g., a USB flash drive) +STORAGE=$(ls /dev/* | grep $STORAGE_DEV | cut -d"/" -f3) +while [ -z ${STORAGE} ] + do + sleep 1 + STORAGE=$(ls /dev/* | grep $STORAGE_DEV | cut -d"/" -f3) +done + +# When the USB storage device is detected, mount it +mount /dev/"$STORAGE_DEV" "$STORAGE_MOUNT_POINT" + +# Cancel shutdown +sudo shutdown -c + +# Set the ACT LED to blink at 1000ms to indicate that the storage device has been mounted +sudo sh -c "echo timer > /sys/class/leds/led0/trigger" +sudo sh -c "echo 1000 > /sys/class/leds/led0/delay_on" + +# Perform backup using rsync +rsync -avhz -e ssh --delete --progress "$STORAGE_MOUNT_POINT" "$USER"@"$REMOTE":"$BACKUP_DIR" + +if [ ! -z "$NOTIFY_TOKEN" ]; then + TEXT=$(sed 's/ /%20/g' <<< "Remote backup completed.") + curl -k \ +"https://us-central1-notify-b7652.cloudfunctions.net/sendNotification?to=${NOTIFY_TOKEN}&text=${TEXT}" \ +> /dev/null +fi + + # Turn off the ACT LED to indicate that the backup is completed + sudo sh -c "echo 0 > /sys/class/leds/led0/brightness" + +# Shutdown +sync +shutdown -h now