diff --git a/README.md b/README.md index 4795950..78c815c 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ A collection of useful scripts that I use everyday - getStarred.sh - Get starred articles from Miniflux and copy to clipboard. [Notes](https://www.alexhyett.com/notes/getting-starred-items-from-miniflux/) - unstar.sh - Unstar all starred articles on Miniflux. [Notes](https://www.alexhyett.com/notes/getting-starred-items-from-miniflux/) -- syncBlog.sh - The script I use to sync my notes and articles from Obsidian to my website. Converting the links. [Notes](https://www.alexhyett.com/notes/syncing-my-obsidian-notes-with-my-blog/) \ No newline at end of file +- syncBlog.sh - The script I use to sync my notes and articles from Obsidian to my website. Converting the links. [Notes](https://www.alexhyett.com/notes/syncing-my-obsidian-notes-with-my-blog/) +- moveFiles.sh - This script checks to see if my server (friday) is online and the folders are mounted before moving files downloaded using rsync. \ No newline at end of file diff --git a/copyFiles.sh b/copyFiles.sh new file mode 100755 index 0000000..1f25bf7 --- /dev/null +++ b/copyFiles.sh @@ -0,0 +1,51 @@ +#!/bin/bash +parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) +cd "$parent_path" + +SERVER_IP=friday +DOWNLOADS="" +MOUNT="" + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + --downloads) + DOWNLOADS="$2" + shift 2 + ;; + --mount) + MOUNT="$2" + shift 2 + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done + +if [ -z "$DOWNLOADS" ] || [ -z "$MOUNT" ]; then + echo "Usage: $0 --downloads --mount " + exit 1 +fi + +# Extract the folder name from the last part of the MOUNT path +FOLDER_NAME=$(basename "$MOUNT") + +ping -c1 -W2 "$SERVER_IP" > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + + if mountpoint -q "$MOUNT"; then + echo "$FOLDER_NAME mounted. Copying files." | ts '[%Y-%m-%d %H:%M:%S]' + files=$(ls "$MOUNT") # Trigger drive spin up + sleep 5 + + output=$(rsync -avzh --update --stats --exclude '.gitkeep' "$DOWNLOADS" "$MOUNT" | grep -A 1 'files transferred') + files=$(echo "$output" | awk '/files transferred/{print $NF}') + + if [[ "$files" != 0 ]]; then + echo "Copied $files files" | ts '[%Y-%m-%d %H:%M:%S]' + ./slackit.sh "$output" "$FOLDER_NAME File Transfer" + fi + fi +fi diff --git a/moveFiles.sh b/moveFiles.sh new file mode 100755 index 0000000..1c49bd2 --- /dev/null +++ b/moveFiles.sh @@ -0,0 +1,60 @@ +#!/bin/bash +parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) +cd "$parent_path" + +SERVER_IP=friday +DOWNLOADS="" +MOUNT="" +DELETE_EMPTY=false + +# Parse command line arguments +while [[ $# -gt 0 ]]; do + case $1 in + --downloads) + DOWNLOADS="$2" + shift 2 + ;; + --mount) + MOUNT="$2" + shift 2 + ;; + --delete-empty) + DELETE_EMPTY=true + shift + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done + +if [ -z "$DOWNLOADS" ] || [ -z "$MOUNT" ]; then + echo "Usage: $0 --downloads --mount [--delete-empty]" + exit 1 +fi + +# Extract the folder name from the last part of the MOUNT path +FOLDER_NAME=$(basename "$MOUNT") + +ping -c1 -W2 "$SERVER_IP" > /dev/null 2>&1 +if [[ $? -eq 0 ]]; then + + if mountpoint -q "$MOUNT"; then + echo "$FOLDER_NAME mounted. Moving files." | ts '[%Y-%m-%d %H:%M:%S]' + files=$(ls "$MOUNT") # Trigger drive spin up + sleep 5 + + output=$(rsync -avzh --remove-source-files --stats --exclude '.gitkeep' "$DOWNLOADS" "$MOUNT" | grep -A 1 'files transferred') + files=$(echo "$output" | awk '/files transferred/{print $NF}') + + if [ "$DELETE_EMPTY" = true ]; then + find "$DOWNLOADS" -type d -empty -delete + fi + + if [[ "$files" != 0 ]]; then + echo "Transferred $files files" | ts '[%Y-%m-%d %H:%M:%S]' + ./slackit.sh "$output" "$FOLDER_NAME File Transfer" + fi + fi +fi diff --git a/slackit.sh b/slackit.sh new file mode 100755 index 0000000..bce1d94 --- /dev/null +++ b/slackit.sh @@ -0,0 +1,13 @@ +#!/bin/bash -e +message=$(echo $1 | tr -d '\n' | tr -d '"') +username=$(echo $2 | tr -d '\n' | tr -d '"') + +SLACK_URL=https://hooks.slack.com/services/SDS9SDID9D9SJS9SDS + +if [ -z "$username" ]; then + username="Server" +fi + +if [ ! -z "$message" ]; then + curl -X POST -H 'Content-type: application/json' --data "{ \"channel\": \"#notifications\", \"username\": \"${username}\", \"icon_emoji\": \":bot:\", \"text\": \"${message}\" }" $SLACK_URL +fi \ No newline at end of file