From 858ae5da3900b0181a0464bbb83bf4fd56c96490 Mon Sep 17 00:00:00 2001 From: cimryan Date: Thu, 18 Oct 2018 20:45:48 -0700 Subject: [PATCH] Fix errors on both the cam and the music filesystems before the archive loop starts. --- windows_archive/archive-teslacam-clips | 87 +--------------- windows_archive/archiveloop | 133 +++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 83 deletions(-) diff --git a/windows_archive/archive-teslacam-clips b/windows_archive/archive-teslacam-clips index c6be29a..8b2d793 100644 --- a/windows_archive/archive-teslacam-clips +++ b/windows_archive/archive-teslacam-clips @@ -1,7 +1,6 @@ #!/bin/bash -eu LOG_FILE=/tmp/archive-teslacam-clips.log -CAM_MOUNT=/mnt/cam ARCHIVE_MOUNT=/mnt/archive function log () { @@ -9,66 +8,6 @@ function log () { echo "$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." - 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..." local move_count=0 @@ -95,42 +34,24 @@ function disconnect_usb_drives_from_host () { 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_file_is_mounted -ensure_cam_drive_is_mounted +fix_errors_in_cam_file move_clips_to_archive -unmount_cam_drive +unmount_cam_file -connect_usb_drives_to_host \ No newline at end of file +connect_usb_drives_to_host diff --git a/windows_archive/archiveloop b/windows_archive/archiveloop index 09f276d..4dd01c7 100644 --- a/windows_archive/archiveloop +++ b/windows_archive/archiveloop @@ -2,13 +2,29 @@ # Change the value on the right side of the equal sign to the name of the server hosting the archive. ARCHIVE_HOST_NAME=archiveserver + LOGFILE=/tmp/archiveloop.log +export CAM_MOUNT=/mnt/cam +export MUSIC_MOUNT=/mnt/music + function log () { echo "$( date )" >> "$LOGFILE" echo "$1" >> "$LOGFILE" } +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 @@ -39,6 +55,105 @@ function wait_for_archive_to_be_reachable () { 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 () { + ensure_cam_file_is_mounted + fix_errors_in_mount_point "$CAM_MOUNT" + unmount_cam_file +} + +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 "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 () { + ensure_music_file_is_mounted + fix_errors_in_mount_point "$MUSIC_MOUNT" + unmount_music_file +} + function archive_clips () { log "Archiving..." /root/bin/archive-teslacam-clips @@ -58,16 +173,34 @@ function wait_for_archive_to_be_unreachable () { done } +function fix_errors_in_files () { + fix_errors_in_cam_file + fix_errors_in_music_file +} + +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_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 + fix_errors_in_music_file + archive_clips wait_for_archive_to_be_unreachable else + fix_errors_in_files + connect_usb_drives_to_host fi