mirror of
https://github.com/cimryan/teslausb.git
synced 2026-02-28 20:20:32 +00:00
Separate files into those used for setup and those used at runtime.
Files used for both will go in the run tree.
This commit is contained in:
35
run/archive-teslacam-clips
Normal file
35
run/archive-teslacam-clips
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
export LOG_FILE=/tmp/archive-teslacam-clips.log
|
||||
export ARCHIVE_MOUNT=/mnt/archive
|
||||
|
||||
function log () {
|
||||
echo "$( date )" >> "$LOG_FILE"
|
||||
echo "$1" >> "$LOG_FILE"
|
||||
}
|
||||
|
||||
function disconnect_usb_drives_from_host () {
|
||||
log "Disconnecting usb from host..."
|
||||
modprobe -r g_mass_storage
|
||||
log "Disconnected usb from host."
|
||||
}
|
||||
|
||||
export -f log
|
||||
|
||||
log "Starting..."
|
||||
|
||||
/root/bin/connect-archive.sh
|
||||
|
||||
disconnect_usb_drives_from_host
|
||||
|
||||
ensure_cam_file_is_mounted
|
||||
|
||||
fix_errors_in_cam_file
|
||||
|
||||
/root/bin/archive-clips.sh
|
||||
|
||||
/root/bin/disconnect-archive.sh
|
||||
|
||||
unmount_cam_file
|
||||
|
||||
connect_usb_drives_to_host
|
||||
237
run/archiveloop
Normal file
237
run/archiveloop
Normal file
@@ -0,0 +1,237 @@
|
||||
#!/bin/bash -eu
|
||||
ARCHIVE_HOST_NAME="$1"
|
||||
|
||||
LOG_FILE=/tmp/archiveloop.log
|
||||
|
||||
export CAM_MOUNT=/mnt/cam
|
||||
export MUSIC_MOUNT=/mnt/music
|
||||
|
||||
function log () {
|
||||
echo "$( date )" >> "$LOG_FILE"
|
||||
echo "$1" >> "$LOG_FILE"
|
||||
}
|
||||
|
||||
function fix_errors_in_mount_point () {
|
||||
local mount_point="$1"
|
||||
log "Running fsck on $mount_point..."
|
||||
/sbin/fsck "$mount_point" -- -a >> "$LOG_FILE" 2>&1 || echo ""
|
||||
log "Finished fsck on $mount_point."
|
||||
}
|
||||
|
||||
function fix_errors_in_mounted_files () {
|
||||
fix_errors_in_mount_point "$CAM_MOUNT"
|
||||
fix_errors_in_mount_point "$MUSIC_MOUNT"
|
||||
}
|
||||
|
||||
function archive_is_reachable () {
|
||||
local reachable=true
|
||||
ping -q -w 1 -c 1 "$ARCHIVE_HOST_NAME" > /dev/null 2>&1 || reachable=false
|
||||
if [ "$reachable" = false ]
|
||||
then
|
||||
false
|
||||
return
|
||||
fi
|
||||
true
|
||||
}
|
||||
|
||||
function connect_usb_drives_to_host() {
|
||||
log "Connecting usb to host..."
|
||||
modprobe g_mass_storage
|
||||
log "Connected usb to host."
|
||||
}
|
||||
|
||||
function wait_for_archive_to_be_reachable () {
|
||||
log "Waiting for archive to be reachable..."
|
||||
while [ true ]
|
||||
do
|
||||
if archive_is_reachable
|
||||
then
|
||||
log "Archive is reachable."
|
||||
break
|
||||
fi
|
||||
if [ -e /tmp/archive_is_reachable ]
|
||||
then
|
||||
log "Simulating archive is reachable"
|
||||
rm /tmp/archive_is_reachable
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
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."
|
||||
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 fix_errors_in_cam_file () {
|
||||
fix_errors_in_mount_point "$CAM_MOUNT"
|
||||
}
|
||||
|
||||
function ensure_cam_file_is_mounted () {
|
||||
log "Ensuring cam file is mounted..."
|
||||
ensure_mountpoint_is_mounted_with_retry "$CAM_MOUNT"
|
||||
log "Ensured cam file is mounted."
|
||||
}
|
||||
|
||||
function ensure_music_file_is_mounted () {
|
||||
log "Ensuring music backing file is mounted..."
|
||||
ensure_mountpoint_is_mounted_with_retry "$MUSIC_MOUNT"
|
||||
log "Ensured cam drive is mounted."
|
||||
}
|
||||
|
||||
function unmount_mount_point () {
|
||||
local mount_point="$1"
|
||||
log "Unmounting $mount_point..."
|
||||
umount "$mount_point" >> "$LOG_FILE" 2>&1
|
||||
log "Unmounted $mount_point."
|
||||
}
|
||||
|
||||
function unmount_cam_file () {
|
||||
unmount_mount_point "$CAM_MOUNT"
|
||||
}
|
||||
|
||||
function unmount_music_file () {
|
||||
unmount_mount_point "$MUSIC_MOUNT"
|
||||
}
|
||||
|
||||
function fix_errors_in_music_file () {
|
||||
fix_errors_in_mount_point "$MUSIC_MOUNT"
|
||||
}
|
||||
|
||||
function archive_clips () {
|
||||
log "Archiving..."
|
||||
/root/bin/archive-teslacam-clips
|
||||
log "Finished archiving."
|
||||
}
|
||||
|
||||
function wait_for_archive_to_be_unreachable () {
|
||||
log "Waiting for archive to be unreachable..."
|
||||
while [ true ]
|
||||
do
|
||||
if ! archive_is_reachable
|
||||
then
|
||||
log "Archive is unreachable."
|
||||
break
|
||||
fi
|
||||
if [ -e /tmp/archive_is_unreachable ]
|
||||
then
|
||||
log "Simulating archive being unreachable."
|
||||
rm /tmp/archive_is_unreachable
|
||||
break
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
function mount_and_fix_errors_in_cam_file () {
|
||||
ensure_cam_file_is_mounted
|
||||
fix_errors_in_cam_file
|
||||
unmount_cam_file
|
||||
}
|
||||
|
||||
function mount_and_fix_errors_in_music_file () {
|
||||
if [ -e "$MUSIC_MOUNT" ]
|
||||
then
|
||||
ensure_music_file_is_mounted
|
||||
fix_errors_in_music_file
|
||||
unmount_music_file
|
||||
fi
|
||||
}
|
||||
|
||||
function mount_and_fix_errors_in_files () {
|
||||
mount_and_fix_errors_in_cam_file
|
||||
mount_and_fix_errors_in_music_file
|
||||
}
|
||||
|
||||
export -f fix_errors_in_mount_point
|
||||
export -f fix_errors_in_cam_file
|
||||
export -f retry
|
||||
export -f mount_mountpoint
|
||||
export -f ensure_mountpoint_is_mounted
|
||||
export -f ensure_mountpoint_is_mounted_with_retry
|
||||
export -f ensure_cam_file_is_mounted
|
||||
export -f fix_errors_in_cam_file
|
||||
export -f unmount_mount_point
|
||||
export -f unmount_cam_file
|
||||
export -f connect_usb_drives_to_host
|
||||
|
||||
log "Starting..."
|
||||
|
||||
if archive_is_reachable
|
||||
then
|
||||
# archive_clips will fix errors in the cam file
|
||||
mount_and_fix_errors_in_music_file
|
||||
|
||||
archive_clips
|
||||
|
||||
wait_for_archive_to_be_unreachable
|
||||
else
|
||||
mount_and_fix_errors_in_files
|
||||
|
||||
connect_usb_drives_to_host
|
||||
fi
|
||||
|
||||
while [ true ]
|
||||
do
|
||||
wait_for_archive_to_be_reachable
|
||||
|
||||
archive_clips
|
||||
|
||||
wait_for_archive_to_be_unreachable
|
||||
done
|
||||
18
run/cifs_archive/archive-clips.sh
Normal file
18
run/cifs_archive/archive-clips.sh
Normal file
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
log "Moving clips to archive..."
|
||||
|
||||
NUM_FILES_MOVED=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."
|
||||
NUM_FILES_MOVED=$((NUM_FILES_MOVED + 1))
|
||||
done
|
||||
log "Moved $NUM_FILES_MOVED file(s)."
|
||||
|
||||
/root/bin/send-pushover "$NUM_FILES_MOVED"
|
||||
|
||||
log "Finished moving clips to archive."
|
||||
26
run/cifs_archive/configure-archive.sh
Normal file
26
run/cifs_archive/configure-archive.sh
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
function configure_archive () {
|
||||
local archive_server_ip_address="$1"
|
||||
|
||||
echo "Configuring the archive..."
|
||||
|
||||
local archive_path="/mnt/archive"
|
||||
|
||||
if [ ! -e "$archive_path" ]
|
||||
then
|
||||
mkdir "$archive_path"
|
||||
fi
|
||||
|
||||
local credentials_file_path="/root/.teslaCamArchiveCredentials"
|
||||
|
||||
/root/bin/write-archive-credentials-to.sh "$credentials_file_path"
|
||||
|
||||
echo "//$archive_server_ip_address/$sharename $archive_path cifs vers=${cifs_version},credentials=${credentials_file_path},iocharset=utf8,file_mode=0777,dir_mode=0777 0" >> /etc/fstab
|
||||
|
||||
echo "Configured the archive."
|
||||
}
|
||||
|
||||
ARCHIVE_SERVER_IP_ADDRESS="$( /root/bin/get-archiveserver-ip-address.sh )"
|
||||
|
||||
configure_archive "$ARCHIVE_SERVER_IP_ADDRESS"
|
||||
9
run/cifs_archive/connect-archive.sh
Normal file
9
run/cifs_archive/connect-archive.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
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."
|
||||
}
|
||||
|
||||
ensure_archive_is_mounted
|
||||
2
run/cifs_archive/disconnect-archive.sh
Normal file
2
run/cifs_archive/disconnect-archive.sh
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash -eu
|
||||
# Nothing to do. It's okay to leave the archive mounted.
|
||||
47
run/cifs_archive/verify-archive-configuration.sh
Normal file
47
run/cifs_archive/verify-archive-configuration.sh
Normal file
@@ -0,0 +1,47 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
function check_archive_server_reachable () {
|
||||
echo "Verifying that the archive server $archiveserver is reachable..."
|
||||
local serverunreachable=false
|
||||
ping -c 1 -w 1 "$archiveserver" 1>/dev/null 2>&1 || serverunreachable=true
|
||||
|
||||
if [ "$serverunreachable" = true ]
|
||||
then
|
||||
echo "STOP: The archive server $archiveserver is unreachable. Try specifying its IP address instead."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "The archive server is reachable."
|
||||
}
|
||||
|
||||
function check_archive_mountable () {
|
||||
local archive_server_ip_address="$1"
|
||||
|
||||
local test_mount_location="/tmp/archivetestmount"
|
||||
|
||||
if [ ! -e "$test_mount_location" ]
|
||||
then
|
||||
mkdir "$test_mount_location"
|
||||
fi
|
||||
|
||||
local tmp_credentials_file_path="/tmp/teslaCamArchiveCredentials"
|
||||
|
||||
/root/bin/write-archive-credentials-to.sh "$tmp_credentials_file_path"
|
||||
|
||||
local mount_failed=false
|
||||
mount -t cifs "//$archive_server_ip_address/$sharename" "$test_mount_location" -o "vers=${cifs_version},credentials=${tmp_credentials_file_path},iocharset=utf8,file_mode=0777,dir_mode=0777" || mount_failed=true
|
||||
|
||||
if [ "$mount_failed" = true ]
|
||||
then
|
||||
echo "STOP: The archive couldn't be mounted with CIFS version ${cifs_version}. Try specifying a lower number for the CIFS version like this: export cifs_version=2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
umount "$test_mount_location"
|
||||
}
|
||||
|
||||
check_archive_server_reachable
|
||||
|
||||
ARCHIVE_SERVER_IP_ADDRESS="$( /root/bin/get-archiveserver-ip-address.sh )"
|
||||
|
||||
check_archive_mountable "$ARCHIVE_SERVER_IP_ADDRESS"
|
||||
6
run/cifs_archive/write-archive-credentials-to.sh
Normal file
6
run/cifs_archive/write-archive-credentials-to.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
FILE_PATH="$1"
|
||||
|
||||
echo "username=$shareuser" > "$FILE_PATH"
|
||||
echo "password=$sharepassword" >> "$FILE_PATH"
|
||||
3
run/get-archiveserver-ip-address.sh
Normal file
3
run/get-archiveserver-ip-address.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
echo "$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')"
|
||||
9
run/remountfs_rw
Normal file
9
run/remountfs_rw
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
echo "Remounting / as read/write"
|
||||
mount / -o remount,rw
|
||||
echo "Remounting /boot as read/write"
|
||||
mount /boot -o remount,rw
|
||||
echo "Done. Make any changes needed, and reboot the Pi when ready."
|
||||
echo "Filesystems / and /boot will be mounted as read only after reboot."
|
||||
|
||||
16
run/rsync_archive/archive-clips.sh
Normal file
16
run/rsync_archive/archive-clips.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
log "Archiving through rsync..."
|
||||
|
||||
source /root/.teslaCamRsyncConfig
|
||||
|
||||
num_files_moved=$(rsync -auvh --stats --log-file=/tmp/archive-rsync-cmd.log /mnt/cam/TeslaCam/saved* $user@$server:$path | awk '/files transferred/{print $NF}')
|
||||
|
||||
/root/bin/send-pushover "$num_files_moved"
|
||||
|
||||
if [ $num_files_moved > 0 ]
|
||||
then
|
||||
log "Successfully synced files through rsync."
|
||||
else
|
||||
log "No files to archive through rsync."
|
||||
fi
|
||||
16
run/rsync_archive/configure-archive.sh
Normal file
16
run/rsync_archive/configure-archive.sh
Normal file
@@ -0,0 +1,16 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
function configure_archive () {
|
||||
local archive_server_ip_address="$1"
|
||||
|
||||
echo "Configuring the archive..."
|
||||
|
||||
echo "Configuring for Rsync..."
|
||||
echo "user=$RSYNC_USER" > /root/.teslaCamRsyncConfig
|
||||
echo "server=$RSYNC_SERVER" >> /root/.teslaCamRsyncConfig
|
||||
echo "path=$RSYNC_PATH" >> /root/.teslaCamRsyncConfig
|
||||
}
|
||||
|
||||
ARCHIVE_SERVER_IP_ADDRESS="$( /root/bin/get-archiveserver-ip-address.sh )"
|
||||
|
||||
configure_archive "$ARCHIVE_SERVER_IP_ADDRESS"
|
||||
2
run/rsync_archive/connect-archive.sh
Normal file
2
run/rsync_archive/connect-archive.sh
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash -eu
|
||||
# Nothing to do.
|
||||
2
run/rsync_archive/disconnect-archive.sh
Normal file
2
run/rsync_archive/disconnect-archive.sh
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/bin/bash -eu
|
||||
# Nothing to do.
|
||||
1
run/rsync_archive/verify-archive-configuration.sh
Normal file
1
run/rsync_archive/verify-archive-configuration.sh
Normal file
@@ -0,0 +1 @@
|
||||
#!/bin/bash -eu
|
||||
21
run/send-pushover
Normal file
21
run/send-pushover
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash -eu
|
||||
|
||||
NUM_FILES_MOVED="$1"
|
||||
|
||||
function log () {
|
||||
echo "$( date )" >> "$LOG_FILE"
|
||||
echo "$1" >> "$LOG_FILE"
|
||||
}
|
||||
|
||||
if [ -r "/root/.teslaCamPushoverCredentials" ] && [ $NUM_FILES_MOVED > 0]
|
||||
then
|
||||
log "Sending Pushover message for moved files."
|
||||
|
||||
source /root/.teslaCamPushoverCredentials
|
||||
|
||||
curl -F "token=$pushover_app_key" \
|
||||
-F "user=$pushover_user_key" \
|
||||
-F "title=Dashcam Copy Complete" \
|
||||
-F "message=$NUM_FILES_MOVED file(s) were copied." \
|
||||
https://api.pushover.net/1/messages
|
||||
fi
|
||||
Reference in New Issue
Block a user