diff options
| author | Steph Enders <steph@senders.io> | 2025-12-12 14:18:24 -0500 |
|---|---|---|
| committer | Steph Enders <steph@senders.io> | 2025-12-12 14:18:24 -0500 |
| commit | 85ed3880767ac2d31461fa5fefa3a6b0726ad82c (patch) | |
| tree | 2da7b46eb8beee3629eeb2d5b851be9c92f3a680 | |
| parent | ca3d6f3f9401c1ab3e1017827bfac315b09db3cf (diff) | |
Update ssync-index to use params over config
This version will require two separate calls to index local and
remote.
Local should be `ssync-index -b -o /output/local.idx ROOT_DIR`
Remote should be `ssync-index -r -w N -o /output/remote.idx ROOT_DIR`
| -rwxr-xr-x | ssync-index | 171 |
1 files changed, 74 insertions, 97 deletions
diff --git a/ssync-index b/ssync-index index e19ee78..4593aa4 100755 --- a/ssync-index +++ b/ssync-index @@ -1,12 +1,20 @@ #!/usr/bin/env sh -USAGE="ssync-index [options] [CONFIG_FILE] +USAGE="ssync-index [options] ROOT_DIR OPTIONS - -k [KEY_FILE] override conigured key file - -o [OUTPUT_DIR] override configured index output dir - -l local only - -r remote only + -b index basename only + -k KEY_FILE (optional: if indexing local) + ssh-key file to use (needs to be non-interactive) + optional: will use default session key + or key set in ssh_config for HOST + -o OUTPUT_FILE + -r REMOTE_HOST (optional) + remote host to index from such as user@hostname + username can be omitted if identical to $USER + or if set in ssh_config -v verbose logging + -w NUM_SECONDS + index all files newer than NOW - NUM_SECONDS -h print this message" # HELPER METHODS @@ -18,140 +26,109 @@ function verbose_log { function lines { wc -l $1 | cut -d' ' -f1 } -function get_default_index_dir { - local cache_path=ssync/index/ - local cache_dir= - if [ ! -z "$XDG_CACHE_DIR" ]; then - cache_dir=$XDG_CACHE_DIR/$cache_path - else - cache_dir=/tmp/ - fi - - mkdir -p $cache_dir/$cache_path - echo $cache_dir/$cache_path -} # GLOBAL VALUES -DEFAULT_INDEX_WINDOW=86400 -LOCAL_ONLY_FLAG= -REMOTE_ONLY_FLAG= -VERBOSE_FLAG= +BASENAME_ONLY_FLAG= KEY_FILE_FLAG= KEY_FILE_ARG= -OUTPUT_DIR_FLAG= -OUTPUT_DIR_ARG= +OUTPUT_FILE_FLAG= +OUTPUT_FILE_ARG= +REMOTE_HOST_FLAG= +REMOTE_HOST_ARG= +WINDOW_FLAG= +WINDOW_ARG= +VERBOSE_FLAG= # OPTIONS PARSING -while getopts "hvlro:k:" opt; do +while getopts "hvbr:k:o:w:" opt; do case "${opt}" in h) echo "$USAGE" exit 1 ;; - o) OUTPUT_DIR_FLAG=1 - OUTPUT_DIR_ARG="$OPTARG" + b) BASENAME_ONLY_FLAG=1 ;; k) KEY_FILE_FLAG=1 KEY_FILE_ARG="$OPTARG" ;; - l) LOCAL_ONLY_FLAG=1 + o) OUTPUT_FILE_FLAG=1 + OUTPUT_FILE_ARG="$OPTARG" ;; - r) REMOTE_ONLY_FLAG=1 + r) REMOTE_HOST_FLAG=1 + REMOTE_HOST_ARG="$OPTARG" ;; v) VERBOSE_FLAG=1 ;; + w) WINDOW_FLAG=1 + WINDOW_ARG="$OPTARG" + ;; esac done shift $(($OPTIND -1)) -if [ $# -eq 0 ]; then +if [ $# -ne 1 ]; then echo "$USAGE" exit 1 fi -CONFIG_FILE=$1 - -# CONFIG PARSING -if [ -z "$CONFIG_FILE" ]; then - echo "CONFIG_FILE is missing." - exit 1 -fi - -if [ ! -f $CONFIG_FILE ]; then - echo "$CONFIG_FILE does not exist." - exit 1 -fi - -source $CONFIG_FILE - -# CONFIG VALIDATION -if [ -z "$remote_host" ]; then - echo "Config is missing remote_host" - exit 1 -fi +# ROOT_DIR validation -if [ -z "$remote_root_dir" ]; then - echo "Config is missing remote_root_dir" - exit 1 -fi +ROOT_DIR=$1 -if [ -z "$keyfile" ]; then - echo "Config is missing keyfile" - exit 1 -elif [ ! -f "$keyfile" ]; then - echo "Configured keyfile '$keyfile' does not exist" +if [ -z "$ROOT_DIR" ]; then + echo "ROOT_DIR is missing." exit 1 fi - -if [ -z "$local_root_dir" ]; then - echo "Config is missing local_root_dir" - exit 1 -elif [ ! -d "$local_root_dir" ]; then - echo "Configured local_root_dir '$local_root_dir' does not exist" - exit 1 +if [ -z "$REMOTE_HOST_ARG" ]; then + # local so check if directory exists + if [ ! -d "$ROOT_DIR" ]; then + echo "$ROOT_DIR does not exist." + exit 1 + fi fi -# VALUE SETTING - -if [ -z "$index_window_s" ]; then - index_window_s=$DEFAULT_INDEX_WINDOW +# Option Validation +output_to= +if [ ! -z "$OUTPUT_FILE_FLAG" ]; then + output_to="> $OUTPUT_FILE_ARG" fi -local_index_file= -remote_index_file= -if [ ! -z "$OUTPUT_DIR_FLAG" ]; then - index_dir=$OUTPUT_DIR_ARG +keyfile_cmd="" +if [ ! -z "$KEY_FILE_FLAG" ]; then + if [ -f "$KEY_FILE_ARG" ]; then + echo "Identity file '$KEY_FILE_ARG' does not exist" + exit 1 + fi + keyfile_cmd="-i $KEY_FILE_ARG" fi -if [ ! -z "$index_dir" ]; then - if [ ! -d "$index_dir" ]; then - echo "Configured index_dir '$index_dir' does not exist" +windowing_flag="" +if [ ! -z "$WINDOW_FLAG" ]; then + if [ "$WINDOW_ARG" -gt 0 ]; then + newermt=$(date -d "$WINDOW_ARG seconds ago" -u -Is) + windowing_flag="-newermt $newermt" + verbose_log "Targetting files from $WINDOW_ARG seconds ago: $newermt" + else + echo "Invalid window value: $WINDOW_ARG" exit 1 fi -else - index_dir=$(get_default_index_dir) fi -# RUN INDEXING - -ts=$(date -u +%s) -local_index_file=${index_dir}/${ts}_local.idx -remote_index_file=${index_dir}/${ts}_remote.idx -newermt=$(date -d "$index_window_s seconds ago" -u -Is) -verbose_log "Targetting files from $index_window_s seconds ago: $newermt" - -# index remote host relative to your fetch dir -if [ -z "$LOCAL_ONLY_FLAG" ]; then - verbose_log "Generating remote index from ${remote_root_dir} to ${remote_index_file}" - ssh -i $keyfile $remote_host \ - "find ${remote_root_dir} -type f -newermt $newermt -exec realpath {} \;" > $remote_index_file - verbose_log "Indexed $(lines $remote_index_file) remote files" +index_value_cmd= +if [ ! -z "$BASENAME_ONLY_FLAG" ]; then + index_value_cmd="basename {}" +else + index_value_cmd="realpath {}" fi -# index local filenames ONLY -if [ -z "$REMOTE_ONLY_FLAG" ]; then - verbose_log "Generate local index from ${local_root_dir} to ${local_index_file}" - find ${local_root_dir} -type f -exec basename {} \; > $local_index_file - verbose_log "Indexed $(lines $local_index_file) local files" +if [ -z "$REMOTE_HOST_FLAG" ]; then + verbose_log "Generate local index from ${ROOT_DIR} to ${OUTPUT_FILE_ARG}" + find ${ROOT_DIR} -type f ${windowing_flag} -exec ${index_value_cmd} \; > $OUTPUT_FILE_ARG + verbose_log "Indexed $(lines $OUTPUT_FILE_ARG) local files" +else + verbose_log "Generate remote index from $REMOTE_HOST_ARG:${ROOT_DIR} to ${OUTPUT_FILE_ARG}" + ssh $keyfile_cmd $REMOTE_HOST_ARG \ + "find ${ROOT_DIR} -type f ${windowing_flag} -exec ${index_value_cmd} \;" > $OUTPUT_FILE_ARG + verbose_log "Indexed $(lines $OUTPUT_FILE_ARG) local files" fi |