diff --git a/GetShellWithoutMonitorOnWindows.md b/GetShellWithoutMonitorOnWindows.md index 86a8773..205c0b2 100644 --- a/GetShellWithoutMonitorOnWindows.md +++ b/GetShellWithoutMonitorOnWindows.md @@ -14,7 +14,7 @@ 1. Run the following commands: ``` cd ~ - wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 + wget https://raw.githubusercontent.com/cimryan/teslausb/master/setup-piForHeadlessConfig.ps1 -OutFile setup-piForHeadlessConfig.ps1 ./setup-piForHeadlessConfig.ps1 -Verbose ``` 1. Enter the single letter of the "boot" drive and press Enter. diff --git a/README.md b/README.md index 6f3c49e..8f8c5fa 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,19 @@ Download and install: * [Etcher](http://etcher.io) ## Create your archive -### Hosting on Windows -Set up a share on a Windows (or macOS using Sharing, or Linux using Samba) machine to host the archive. These instructions assume that you created a share named "SailfishCam" on the server "Nautilus". It is recommended that you create a new user. Grant the user you'll be using read/write access to the share. These instructions will assume that the user you've created is named "sailfish" and that the password for this user is "pa$$w0rd". +### Hosting on Windows File Shares, MacOS Sharing, or Samba on Linux +Set up a share to host the archive. These instructions assume that you created a share named "SailfishCam" on the server "Nautilus". It is recommended that you create a new user. Grant the user you'll be using read/write access to the share. These instructions will assume that the user you've created is named "sailfish" and that the password for this user is "pa$$w0rd". -Get the IP address of the archive machine. You'll need this later, so write it down, somewhere. You can do this by opening a command prompt on the archive machine and typing ipconfig. Get the IP address from the line labeled "IPv4 Address". These instructions will assume that the IP address of the archive server is 192.168.0.41. +Get the IP address of the archive machine. You'll need this later, so write it down, somewhere. +* On Windows you can do this by opening a command prompt on the archive machine and typing ipconfig. Get the IP address from the line labeled "IPv4 Address". These instructions will assume that the IP address of the archive server is 192.168.0.41. +* On MacOS or Linux open a terminal and type ifconfig. -### TODO Other hosting solutions +### Hosting via SFTP/rsync +**EXPERIMENTAL - Hosting the archive on SFTP hasn't been thoroughly tested** + +Since sftp/rsync is accessing a computer through SSH, the only requirement for hosting an SFTP/rsync server is to have a box running Linux. An example can be another Raspberry Pi connected to your local network with a USB storage drive plugged in. The official Raspberry Pi site has a good example on [how to mount an external drive](https://www.raspberrypi.org/documentation/configuration/external-storage.md). You will need the username and host/IP of the storage server, as well as the path for the files to go in, and the storage server will need to allow SSH. + +### ***TODO: Other hosting solutions*** ## Set up the Raspberry Pi There are three phases to setting up the Pi: @@ -92,18 +99,23 @@ Now that you have Wifi up and running, it's time to set up the USB storage and s ping 192.168.0.41 ``` 1. If you can't ping the archive server by IP address from the Pi, you should go do whatever you need to on your network to fix that. If you can't reach the archive server by name, from the Pi but you can by IP address, then use its IP address, below, in place of its name. -1. Run these commands, subsituting your values. The last line is the percent of the drive you want to allocate for dashcam storage. The remaining percentage will be allocated for music. +1. Determine how much, as a percentage, of the drive you want to allocate to recording dashcam footage by using: + ``` + export campercent= + ``` + For example, using `export campercent=100` would allocate 100% of the space to recording footage from your car, and would not create a separate music partition. `export campercent=50` would be only allocate half of the space for a dashcam footage drive, and allocates the other half to be a music storage drive. +1. If you are trying to archive on an SFTP/rsync server, then follow these [instructions](SetupRSync.md) and skip step 7. Otherwise, skip this step. +1. If you are trying to archive on a shared drive, run these commands, subsituting your values for your shared drive: ``` export archiveserver=Nautilus export sharename=SailfishCam export shareuser=sailfish export sharepassword=pa$$w0rd - export campercent=100 ``` 1. If you'd like to receive a text message when your Pi finishes archiving clips follow these [Instructions](ConfigureNotificationsForArchive.md). 1. Run these commands: ``` - wget https://raw.githubusercontent.com/cimryan/teslausb/master/windows_archive/setup-teslausb + wget https://raw.githubusercontent.com/cimryan/teslausb/master/setup-teslausb chmod +x setup-teslausb ./setup-teslausb ``` diff --git a/SetupRSync.md b/SetupRSync.md new file mode 100644 index 0000000..7106a7a --- /dev/null +++ b/SetupRSync.md @@ -0,0 +1,43 @@ +# Introduction +This guide will show you how to utilize [rsync](https://rsync.samba.org/) to archive your saved TeslaCam footage on a remote storage server. In my case, I use this for a networked pi storage server. + +This guide makes the following assumptions: +1. You are running your own ftp/rsync server that you have admin rights to, or can at least add a public key to its `~/.ssh/authorized_keys` file +1. The ftp/rsync server has rsync installed (raspbian automatically does) +2. You have **NOT** run the `setup-teslacam` script yet +# Step 1: Authentication +Similar to sftp, rsync by default utilizes ssh to connect to a remote server and transfer files. This guide will use a generated ssh keypair, hence the first assumption above. + +1. On your teslausb pi, run `ssh-keygen` to generate an ssh key **for the ROOT user!** If you followed the previous steps, you should have already ran `sudo -i` to become the root user on the telsausb pi. If you didn't, run `sudo -i` and re-run `ssh-keygen`. You can be sure that it is generating for root if it asks to store the key in `/root/.ssh/` (versus something like `/home/pi/.ssh`). + +1. Add the contents of the newly generated `/root/.ssh/id_rsa.pub` file from your teslausb pi to the storage server's `~/.ssh/authorized_keys` file. This will allow a nice and easy connection through rsync, no passwords needed! + +1. Lastly, you will need to authorize the connection to the FTP/Rsync server and test that the key works, so try connecting to the server (through ssh), and **if it asks if you wish to continue connecting, make sure to type `yes`!** If you do not do this, rsync will fail to connect and thus fail to archive your clips. + +# Step 2: Exports +To be able to configure the teslausb pi use rsync, you'll need to export a few things. On your teslausb pi, run: + +``` +export RSYNC_ENABLE=true +export RSYNC_USER= +export RSYNC_SERVER= +export RSYNC_PATH= +``` + +Explanations for each: +* `RSYNC_ENABLE`: `true` for enabling rsync +* `RSYNC_USER`: The user on the FTP server +* `RSYNC_SERVER`: The IP address/hostname of the destination machine +* `RSYNC_PATH`: The path on the destination machine where the files will be saved + +An example (of my) config is listed below: + +``` +export RSYNC_ENABLE=true +export RSYNC_USER=pi +export RSYNC_SERVER=192.168.1.254 +export RSYNC_PATH=/mnt/PIHDD/TeslaCam/ +``` +***Note: RSYNC_ENABLE=true is going to disable the default archive server. Perhaps future releases will allow both to be defined and function at the same time, for redundancy, but for now just pick one that you'll want the most.*** + +You should be ready to run the setup script now, so return back to step 8 of the [Main Instructions](README.md). \ No newline at end of file diff --git a/archive-teslacam-clips b/archive-teslacam-clips new file mode 100644 index 0000000..a29be15 --- /dev/null +++ b/archive-teslacam-clips @@ -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 diff --git a/windows_archive/archiveloop b/archiveloop similarity index 97% rename from windows_archive/archiveloop rename to archiveloop index 257eba9..006540e 100644 --- a/windows_archive/archiveloop +++ b/archiveloop @@ -1,7 +1,5 @@ #!/bin/bash -eu - -# Change the value on the right side of the equal sign to the name of the server hosting the archive. -ARCHIVE_HOST_NAME=archiveserver +ARCHIVE_HOST_NAME="$1" LOG_FILE=/tmp/archiveloop.log diff --git a/cifs_archive/archive-clips.sh b/cifs_archive/archive-clips.sh new file mode 100644 index 0000000..26b634f --- /dev/null +++ b/cifs_archive/archive-clips.sh @@ -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." diff --git a/cifs_archive/configure-archive.sh b/cifs_archive/configure-archive.sh new file mode 100644 index 0000000..eff994f --- /dev/null +++ b/cifs_archive/configure-archive.sh @@ -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" \ No newline at end of file diff --git a/cifs_archive/connect-archive.sh b/cifs_archive/connect-archive.sh new file mode 100644 index 0000000..c93f12f --- /dev/null +++ b/cifs_archive/connect-archive.sh @@ -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 diff --git a/cifs_archive/disconnect-archive.sh b/cifs_archive/disconnect-archive.sh new file mode 100644 index 0000000..dee089a --- /dev/null +++ b/cifs_archive/disconnect-archive.sh @@ -0,0 +1,2 @@ +#!/bin/bash -eu +# Nothing to do. It's okay to leave the archive mounted. \ No newline at end of file diff --git a/cifs_archive/verify-archive-configuration.sh b/cifs_archive/verify-archive-configuration.sh new file mode 100644 index 0000000..614e88b --- /dev/null +++ b/cifs_archive/verify-archive-configuration.sh @@ -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" diff --git a/cifs_archive/write-archive-credentials-to.sh b/cifs_archive/write-archive-credentials-to.sh new file mode 100644 index 0000000..edce256 --- /dev/null +++ b/cifs_archive/write-archive-credentials-to.sh @@ -0,0 +1,6 @@ +#!/bin/bash -eu + +FILE_PATH="$1" + +echo "username=$shareuser" > "$FILE_PATH" +echo "password=$sharepassword" >> "$FILE_PATH" diff --git a/windows_archive/create-backingfiles-partition.sh b/create-backingfiles-partition.sh similarity index 100% rename from windows_archive/create-backingfiles-partition.sh rename to create-backingfiles-partition.sh diff --git a/windows_archive/create-backingfiles.sh b/create-backingfiles.sh similarity index 100% rename from windows_archive/create-backingfiles.sh rename to create-backingfiles.sh diff --git a/get-archiveserver-ip-address.sh b/get-archiveserver-ip-address.sh new file mode 100644 index 0000000..8a3936e --- /dev/null +++ b/get-archiveserver-ip-address.sh @@ -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 '()')" \ No newline at end of file diff --git a/windows_archive/make-root-fs-readonly.sh b/make-root-fs-readonly.sh similarity index 100% rename from windows_archive/make-root-fs-readonly.sh rename to make-root-fs-readonly.sh diff --git a/windows_archive/remountfs_rw b/remountfs_rw similarity index 100% rename from windows_archive/remountfs_rw rename to remountfs_rw diff --git a/rsync_archive/archive-clips.sh b/rsync_archive/archive-clips.sh new file mode 100644 index 0000000..29f5a55 --- /dev/null +++ b/rsync_archive/archive-clips.sh @@ -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 diff --git a/rsync_archive/configure-archive.sh b/rsync_archive/configure-archive.sh new file mode 100644 index 0000000..4fcb84c --- /dev/null +++ b/rsync_archive/configure-archive.sh @@ -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" \ No newline at end of file diff --git a/rsync_archive/connect-archive.sh b/rsync_archive/connect-archive.sh new file mode 100644 index 0000000..cfcbd8e --- /dev/null +++ b/rsync_archive/connect-archive.sh @@ -0,0 +1,2 @@ +#!/bin/bash -eu +# Nothing to do. \ No newline at end of file diff --git a/rsync_archive/disconnect-archive.sh b/rsync_archive/disconnect-archive.sh new file mode 100644 index 0000000..cfcbd8e --- /dev/null +++ b/rsync_archive/disconnect-archive.sh @@ -0,0 +1,2 @@ +#!/bin/bash -eu +# Nothing to do. \ No newline at end of file diff --git a/rsync_archive/verify-archive-configuration.sh b/rsync_archive/verify-archive-configuration.sh new file mode 100644 index 0000000..95dbbf8 --- /dev/null +++ b/rsync_archive/verify-archive-configuration.sh @@ -0,0 +1 @@ +#!/bin/bash -eu \ No newline at end of file diff --git a/send-pushover b/send-pushover new file mode 100644 index 0000000..3a6192a --- /dev/null +++ b/send-pushover @@ -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 diff --git a/windows_archive/setup-piForHeadlessConfig.ps1 b/setup-piForHeadlessConfig.ps1 similarity index 100% rename from windows_archive/setup-piForHeadlessConfig.ps1 rename to setup-piForHeadlessConfig.ps1 diff --git a/mac_linux_archive/setup-piForHeadlessConfig.sh b/setup-piForHeadlessConfig.sh old mode 100755 new mode 100644 similarity index 100% rename from mac_linux_archive/setup-piForHeadlessConfig.sh rename to setup-piForHeadlessConfig.sh diff --git a/setup-teslausb b/setup-teslausb new file mode 100644 index 0000000..8c0d6b1 --- /dev/null +++ b/setup-teslausb @@ -0,0 +1,247 @@ +#!/bin/bash -eu + +USER_ENABLED_PUSHOVER=false + +REPO=${REPO:-cimryan} + +BRANCH=${BRANCH:-master} + +if ! [ $(id -u) = 0 ] +then + echo "STOP: Run sudo -i." + exit 1 +fi + +function check_variable () { + local var_name="$1" + if [ -z "${!var_name+x}" ] + then + echo "STOP: Define the variable $var_name like this: export $var_name=value" + exit 1 + fi +} + +function check_pushover_enabled () { + if [ ! -z "${pushover_enabled+x}" ] + then + if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] + then + echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." + echo "Define the variables like this:" + echo "export pushover_user_key=put_your_userkey_here" + echo "export pushover_app_key=put_your_appkey_here" + exit 1 + elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] + then + echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." + exit 1 + else + USER_ENABLED_PUSHOVER=true + echo "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials + echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials + echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials + fi + fi +} + +function check_available_space () { + echo "Verifying that there is sufficient space available on the MicroSD card..." + + local available_space="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )" + + if [ "$available_space" -lt 4294967296 ] + then + echo "STOP: The MicroSD card is too small." + exit 1 + fi + + echo "There is sufficient space available." +} + +function get_script () { + local local_path="$1" + local name="$2" + local remote_path="${3:-}" + + wget -O "$local_path/$name" https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/"$remote_path"/"$name" + chmod +x "$local_path/$name" +} + +function get_ancillary_setup_scripts () { + get_script /tmp create-backingfiles-partition.sh + get_script /tmp create-backingfiles.sh + get_script /tmp make-root-fs-readonly.sh +} + +function fix_cmdline_txt_modules_load () +{ + echo "Fixing the modules-load parameter in /boot/cmdline.txt..." + cp /boot/cmdline.txt ~ + cat ~/cmdline.txt | sed 's/ modules-load=dwc2,g_ether/ modules-load=dwc2/' > /boot/cmdline.txt + rm ~/cmdline.txt + echo "Fixed cmdline.txt." +} + +BACKINGFILES_MOUNTPOINT=/backingfiles + +function create_usb_drive_backing_files () { + if [ ! -e "$BACKINGFILES_MOUNTPOINT" ] + then + mkdir "$BACKINGFILES_MOUNTPOINT" + fi + + if [ ! -e /dev/mmcblk0p3 ] + then + /tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT" + fi + + if ! findmnt --mountpoint /backingfiles + then + echo "Mounting the partition for the backing files..." + mount /backingfiles + echo "Mounted the partition for the backing files." + fi + + if [ ! -e /backingfiles/*.bin ] + then + /tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" + fi +} + +function configure_archive_scripts () { + echo "Configuring the archive scripts..." + + get_script /root/bin archiveloop + get_script /root/bin archive-teslacam-clips + + if [ $RSYNC_ENABLE = true ] + then + get_script /root/bin archive-clips.sh rsync_archive + get_script /root/bin connect-archive.sh rsync_archive + get_script /root/bin disconnect-archive.sh rsync_archive + else + get_script /root/bin archive-clips.sh cifs_archive + get_script /root/bin connect-archive.sh cifs_archive + get_script /root/bin disconnect-archive.sh cifs_archive + fi + + get_script /root/bin remountfs_rw + + echo "Configured the archive scripts." +} + +function configure_pushover_scripts() { + get_script /root/bin send-pushover +} + +function configure_rc_local () { + if grep -q archiveloop /etc/rc.local + then + return + fi + + echo "Configuring /etc/rc.local to run the archive scripts at startup..." + echo "#!/bin/bash -eu" > ~/rc.local + echo "archiveserver=\"${archiveserver}\"" >> ~/rc.local +cat << 'EOF' >> ~/rc.local +LOGFILE=/tmp/rc.local.log + +function log () { + echo "$( date )" >> "$LOGFILE" + echo "$1" >> "$LOGFILE" +} + +log "Launching archival script..." +/root/bin/archiveloop "$archiveserver" & +log "All done" +exit 0 +EOF + + cat ~/rc.local > /etc/rc.local + rm ~/rc.local + echo "Configured rc.local." +} + +function configure_hostname () { + echo "Configuring the hostname..." + + local new_host_name="teslausb" + cp /etc/hosts ~ + sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts + + cp /etc/hostname ~ + sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname + echo "Configured the hostname." +} + +function make_root_fs_readonly () { + /tmp/make-root-fs-readonly.sh +} + +echo "Verifying environment variables..." + +RSYNC_ENABLE="${RSYNC_ENABLE:-false}" + +if [ "$RSYNC_ENABLE" = true ] +then + check_variable "RSYNC_USER" + check_variable "RSYNC_SERVER" + export archiveserver="$RSYNC_SERVER" + check_variable "RSYNC_PATH" +else # Else for now, TODO allow both for more redundancy? + check_variable "sharename" + check_variable "shareuser" + check_variable "sharepassword" + + export cifs_version="${cifs_version:-3}" +fi + +check_variable "archiveserver" +check_variable "campercent" + +check_pushover_enabled + +if [ ! -e /root/bin ] +then + mkdir /root/bin +fi + +if [ "$RSYNC_ENABLE" = true ] +then + get_script /root/bin verify-archive-configuration.sh rsync_archive + get_script /root/bin configure-archive.sh rsync_archive +else + get_script /root/bin verify-archive-configuration.sh cifs_archive + get_script /root/bin configure-archive.sh cifs_archive + get_script /root/bin write-archive-credentials-to.sh cifs_archive +fi + +get_script /root/bin get-archiveserver-ip-address.sh + +/root/bin/verify-archive-configuration.sh + +check_available_space + +get_ancillary_setup_scripts + +pushd ~ + +configure_archive_scripts + +configure_pushover_scripts + +fix_cmdline_txt_modules_load + +echo "" >> /etc/fstab + +create_usb_drive_backing_files + +/root/bin/configure-archive.sh + +configure_rc_local + +configure_hostname + +make_root_fs_readonly + +echo "All done." diff --git a/windows_archive/archive-teslacam-clips b/windows_archive/archive-teslacam-clips deleted file mode 100644 index 8b2d793..0000000 --- a/windows_archive/archive-teslacam-clips +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash -eu - -LOG_FILE=/tmp/archive-teslacam-clips.log -ARCHIVE_MOUNT=/mnt/archive - -function log () { - echo "$( date )" >> "$LOG_FILE" - echo "$1" >> "$LOG_FILE" -} - -function move_clips_to_archive () { - log "Moving clips to archive..." - local 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 [ -r "/root/.teslaCamPushoverCredentials" ] && [ $move_count > 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 ensure_archive_is_mounted () { - log "Ensuring cam archive is mounted..." - ensure_mountpoint_is_mounted_with_retry "$ARCHIVE_MOUNT" - log "Ensured cam archive is mounted." -} - -log "Starting..." - -ensure_archive_is_mounted - -disconnect_usb_drives_from_host - -ensure_cam_file_is_mounted - -fix_errors_in_cam_file - -move_clips_to_archive - -unmount_cam_file - -connect_usb_drives_to_host diff --git a/windows_archive/send-pushover b/windows_archive/send-pushover deleted file mode 100644 index 6948c06..0000000 --- a/windows_archive/send-pushover +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash -eu - -function log () { - echo "$( date )" >> "$LOG_FILE" - echo "$1" >> "$LOG_FILE" -} - -source /root/.teslaCamPushoverCredentials - -curl -F "token=$pushover_app_key" \ --F "user=$pushover_user_key" \ --F "title=Dashcam Copy Complete" \ --F "message=$1 file(s) were copied." \ -https://api.pushover.net/1/messages diff --git a/windows_archive/setup-teslausb b/windows_archive/setup-teslausb deleted file mode 100644 index c50b31b..0000000 --- a/windows_archive/setup-teslausb +++ /dev/null @@ -1,281 +0,0 @@ -#!/bin/bash -eu - -USER_ENABLED_PUSHOVER=false - -REPO=${REPO:-cimryan} - -BRANCH=${BRANCH:-master} - -if ! [ $(id -u) = 0 ] -then - echo "STOP: Run sudo -i." - exit 1 -fi - -function check_variable () { - local var_name="$1" - if [ -z "${!var_name+x}" ] - then - echo "STOP: Define the variable $var_name like this: export $var_name=value" - exit 1 - fi -} - -function check_pushover_enabled () { - if [ ! -z "${pushover_enabled+x}" ] - then - if [ ! -n "${pushover_user_key+x}" ] || [ ! -n "${pushover_app_key+x}" ] - then - echo "STOP: You're trying to setup Pushover but didn't provide your User and/or App key." - echo "Define the variables like this:" - echo "export pushover_user_key=put_your_userkey_here" - echo "export pushover_app_key=put_your_appkey_here" - exit 1 - elif [ "${pushover_user_key}" = "put_your_userkey_here" ] || [ "${pushover_app_key}" = "put_your_appkey_here" ] - then - echo "STOP: You're trying to setup Pushover, but didn't replace the default User and App key values." - exit 1 - else - USER_ENABLED_PUSHOVER=true - echo "export pushover_enabled=true" > /root/.teslaCamPushoverCredentials - echo "export pushover_user_key=$pushover_user_key" >> /root/.teslaCamPushoverCredentials - echo "export pushover_app_key=$pushover_app_key" >> /root/.teslaCamPushoverCredentials - fi - fi -} - -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 write_archive_credentials_to () { - local file_path="$1" - - echo "username=$shareuser" > "$file_path" - echo "password=$sharepassword" >> "$file_path" - -} - -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" - - write_archive_credentials_to "$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" -} - -function check_available_space () { - echo "Verifying that there is sufficient space available on the MicroSD card..." - - local available_space="$( parted -m /dev/mmcblk0 u b print free | tail -1 | cut -d ":" -f 4 | sed 's/B//g' )" - - if [ "$available_space" -lt 4294967296 ] - then - echo "STOP: The MicroSD card is too small." - exit 1 - fi - - echo "There is sufficient space available." -} - -function get_ancillary_setup_scripts () { - pushd /tmp - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles-partition.sh - chmod +x ./create-backingfiles-partition.sh - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/create-backingfiles.sh - chmod +x ./create-backingfiles.sh - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/make-root-fs-readonly.sh - chmod +x ./make-root-fs-readonly.sh - popd -} - -function fix_cmdline_txt_modules_load () -{ - echo "Fixing the modules-load parameter in /boot/cmdline.txt..." - cp /boot/cmdline.txt ~ - cat ~/cmdline.txt | sed 's/[[:space:]]\+modules-load=[^ [:space:]]\+//' | sed 's/rootwait/rootwait modules-load=dwc2/' > /boot/cmdline.txt - rm ~/cmdline.txt - echo "Fixed cmdline.txt." -} - -BACKINGFILES_MOUNTPOINT=/backingfiles - -function create_usb_drive_backing_files () { - mkdir "$BACKINGFILES_MOUNTPOINT" - /tmp/create-backingfiles-partition.sh "$BACKINGFILES_MOUNTPOINT" - - echo "Mounting the partition for the backing files..." - mount /backingfiles - echo "Mounted the partition for the backing files." - - /tmp/create-backingfiles.sh "$campercent" "$BACKINGFILES_MOUNTPOINT" -} - -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" - - write_archive_credentials_to "$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." -} - -function configure_archive_scripts () { - echo "Configuring the archive scripts..." - mkdir /root/bin - - pushd ~ - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/archiveloop - sed s/ARCHIVE_HOST_NAME=archiveserver/ARCHIVE_HOST_NAME=$archiveserver/ ~/archiveloop > /root/bin/archiveloop - rm ~/archiveloop - chmod +x /root/bin/archiveloop - popd - - pushd /root/bin - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/archive-teslacam-clips - chmod +x archive-teslacam-clips - popd - echo "Configured the archive scripts." - - pushd /root - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/remountfs_rw - chmod +x remountfs_rw - popd - echo "Downloaded script to remount filesystems read/write if needed (/root/remountfs_rw)." -} - -function configure_pushover_scripts() { -if [ ${USER_ENABLED_PUSHOVER} = "true" ] -then - pushd /root/bin - wget https://raw.githubusercontent.com/"$REPO"/teslausb/"$BRANCH"/windows_archive/send-pushover - chmod +x send-pushover - popd -fi -} - -function configure_rc_local () { - echo "Configuring /etc/rc.local to run the archive scripts at startup..." - echo "#!/bin/bash -eu" > ~/rc.local - tail -n +2 /etc/rc.local | sed '$d' >> ~/rc.local -cat << 'EOF' >> ~/rc.local -LOGFILE=/tmp/rc.local.log - -function log () { - echo "$( date )" >> "$LOGFILE" - echo "$1" >> "$LOGFILE" -} - -log "Launching archival script..." -/root/bin/archiveloop & -log "All done" -exit 0 -EOF - - cat ~/rc.local > /etc/rc.local - rm ~/rc.local - echo "Configured rc.local." -} - -function configure_hostname () { - echo "Configuring the hostname..." - - local new_host_name="teslausb" - cp /etc/hosts ~ - sed "s/raspberrypi/$new_host_name/g" ~/hosts > /etc/hosts - - cp /etc/hostname ~ - sed "s/raspberrypi/$new_host_name/g" ~/hostname > /etc/hostname - echo "Configured the hostname." -} - -function make_root_fs_readonly () { - /tmp/make-root-fs-readonly.sh -} - -echo "Verifying environment variables..." - -if [ ! -n "${cifs_version+x}" ] -then - cifs_version=3 -fi - -check_variable "archiveserver" -check_variable "sharename" -check_variable "shareuser" -check_variable "sharepassword" -check_variable "campercent" - -check_pushover_enabled - -check_archive_server_reachable - -ARCHIVE_SERVER_IP_ADDRESS="$(ping -c 1 -w 1 $archiveserver 2>/dev/null | head -n 1 | grep -o -e "(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\})" | tr -d '()')" - -check_archive_mountable "$ARCHIVE_SERVER_IP_ADDRESS" - -check_available_space - -get_ancillary_setup_scripts - -pushd ~ - -configure_archive_scripts - -configure_pushover_scripts - -fix_cmdline_txt_modules_load - -echo "" >> /etc/fstab - -create_usb_drive_backing_files - -configure_archive "$ARCHIVE_SERVER_IP_ADDRESS" - -configure_rc_local - -configure_hostname - -make_root_fs_readonly - -echo "All done."