Fix errors on both the cam and the music filesystems before the archive loop starts.

This commit is contained in:
cimryan
2018-10-18 20:45:48 -07:00
parent ccfc724f39
commit 858ae5da39
2 changed files with 137 additions and 83 deletions

View File

@@ -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