#!/bin/sh # syncs files located in ~/sync with those located in the ~/sync on SERVER SERVER=10.0.1.36 local_files=$(mktemp) remote_files=$(mktemp) ls ~/sync > $local_files ssh $SERVER "ls sync" > $remote_files local_files_missing=$(mktemp) diff $local_files $remote_files | grep "^> " | awk '{$1=""; print $0}' | sed "s/^\ *//g" > $local_files_missing remote_files_missing=$(mktemp) diff $local_files $remote_files | grep "^< " | awk '{$1=""; print $0}' | sed "s/^\ *//g" > $remote_files_missing # upload files missing on server while IFS='' read -r LINE || [ -n "${LINE}" ]; do echo "${LINE} not found on skoskapet, uploading..." scp ~/sync/"${LINE}" $SERVER:~/sync done < $remote_files_missing # download missing files while IFS='' read -r LINE || [ -n "${LINE}" ]; do echo "missing file ${LINE}, downloading..." # scp is stupid and needs all filenames with spaces to have explicit escaping escaped_file=$(echo "${LINE}" | sed "s/\ /\\\ /g") scp $SERVER:~/sync/"$escaped_file" ~/sync done < $local_files_missing # update modified files for file in ~/sync/*; do server_file=$(mktemp) local_file_time=$(ls -l --time-style=+%s "$file" | cut -d ' ' -f 6) remote_file_time=$(ssh $SERVER "ls -l --time-style=+%s \"$file\" | cut -d ' ' -f 6") if [ "$local_file_time" -lt "$remote_file_time" ]; then echo "Local copy of \"$file\" is older, downloading new copy from server..." escaped_file=$(echo "$file" | sed "s/\ /\\\ /g") scp $SERVER:"$escaped_file" ~/sync else echo "Remote copy of \"$file\" is outdated, uploading new version..." scp "$file" $SERVER:~/sync fi done echo "all good!"