Files
teslausb/windows_archive/archive-teslacam-clips
Richard Goodwin 5cfe22fb92 Check for teslacam folder
And other tweaks.
2018-10-21 14:04:23 -05:00

142 lines
2.9 KiB
Bash

#!/bin/bash -eu
LOG_FILE=/tmp/archiveloop.log
CAM_MOUNT=/mnt/cam
ARCHIVE_MOUNT=/mnt/archive
function log () {
echo "$( date ) : $1" >> "$LOG_FILE"
}
function retry () {
local attempts=0
while [ true ]
do
if eval "$@"
then
true
return
fi
if [ "$attempts" -ge 10 ]
then
log "Attempts exhausted."
false
return
fi
log "Sleeping before retry..."
/bin/sleep 1
attempts=$((attempts + 1))
log "Retrying..."
done
false
return
}
function mount_mountpoint () {
local mount_point="$1"
log "Mounting $mount_point..."
local mounted=true
mount "$mount_point" >> "$LOG_FILE" 2>&1 || mounted=false
if [ "$mounted" = true ]
then
log "Mounted $mount_point."
if [ -d "/mnt/cam/TeslaCam" ]
then
log "TeslaCam folder exists"
true
return
else
mkdir /mnt/cam/TeslaCam
true
return
else
log "Failed to mount $mount_point."
false
return
fi
}
function ensure_mountpoint_is_mounted () {
local mount_point="$1"
local mount_exists=true
findmnt --mountpoint "$mount_point" > /dev/null || mount_exists=false
if [ "$mount_exists" = true ]
then
log "$mount_point is already mounted."
else
mount_mountpoint "$mount_point"
fi
}
function ensure_mountpoint_is_mounted_with_retry () {
retry ensure_mountpoint_is_mounted "$1"
}
function move_clips_to_archive () {
log "Moving clips to archive..."
move_count=0
for file_name in "$CAM_MOUNT"/TeslaCam/saved*; do
[ -e "$file_name" ] || continue
log "Moving $file_name ..."
mv -- "$file_name" "$ARCHIVE_MOUNT" >> "$LOG_FILE" 2>&1 || echo ""
log "Moved $file_name."
move_count=$((move_count + 1))
done
log "Moved $move_count file(s)."
if [ -e "/root/.teslaCamPushoverCredentials" ] && [ ${move_count} -gt 0 ]
then
log "Sending Pushover message for copied files."
/root/bin/send-pushover $move_count
fi
log "Finished moving clips to archive."
}
function disconnect_usb_drives_from_host () {
log "Disconnecting usb from host..."
modprobe -r g_mass_storage
log "Disconnected usb from host."
}
function fix_errors_on_cam_drive () {
log "Running fsck..."
/sbin/fsck "$CAM_MOUNT" -- -a >> "$LOG_FILE" 2>&1 || echo ""
log "Finished running fsck."
}
function ensure_archive_is_mounted () {
log "Ensuring cam archive is mounted..."
ensure_mountpoint_is_mounted_with_retry "$ARCHIVE_MOUNT"
log "Ensured cam archive is mounted."
}
function ensure_cam_drive_is_mounted () {
log "Ensuring cam drive is mounted..."
ensure_mountpoint_is_mounted_with_retry "$CAM_MOUNT"
log "Ensured cam drive is mounted."
}
function unmount_cam_drive () {
log "Unmounting cam drive..."
umount "$CAM_MOUNT"
log "Unmounted cam drive."
}
log "Starting..."
ensure_archive_is_mounted
disconnect_usb_drives_from_host
fix_errors_on_cam_drive
ensure_cam_drive_is_mounted
move_clips_to_archive
unmount_cam_drive
connect_usb_drives_to_host