#!/bin/bash TARGET_REMOTE_USER=adm0 TARGET_OWNER_USER=owner TARGET_REMOTE_PATH=/usr/disp/ export PATH if [ "$#" -ne "1" ] && [ "$#" -ne "2" ] && [ "$#" -ne "3" ] then echo "$0 --help " exit 0 fi if [ "$1" == "--help" ] then echo "sshdb - utility for simpler database management between local machines vie authorized_keys and rsync." echo " " echo "USAGE: sshdb [-FLAG] [ARGS..]" echo " " echo "FLAGS:" echo "--help Show this sheet." echo " " echo "-g [IP] [|PATH] Copy database from remote machine to PATH/" echo " or ./ if the PATH argument is missing." echo " Example: sshdb -g 10.64.184.60" echo " sshdb -g 10.64.184.60 ./tempdb/" echo " " echo "-l [IP] [|FILE] Copy database from FILE to remote machine" echo " and execute initial maintenance from" echo " ./database.sqlite if the FILE argument is missing." echo " Example: sshdb -l 10.64.184.60" echo " sshdb -l 10.64.184.60 /home/myuser/folder/folder2/database.sqlite" echo " " echo "-c [IP1] [IP2] Copy database from IP1 to IP2 and execute initial maintenance." echo " Example: sshdb -c 172.23.16.176 10.64.184.60" echo " " exit 0 fi # ----------------------------------- if [ "$1" == "-g" ] then if [ -z "$(cat ~/.ssh/id_rsa.pub)" ] then echo "SSH key is needed. Proceed to generating it by ssh-keygen." exit 1 fi if [ -z "$2" ] then echo "Target IP is needed. Run $0 --help." exit 1 fi LOCAL_DATABASE_PATH="./" if [ ! -z "$3" ] then LOCAL_DATABASE_PATH="$3" fi if [ ! -d $LOCAL_DATABASE_PATH ] then echo "Path $LOCAL_DATABASE_PATH is missing." exit 1 fi ssh-copy-id "$TARGET_REMOTE_USER"@"$2" ssh "$TARGET_REMOTE_USER"@"$2" "sudo apt install rsync" rsync "$TARGET_REMOTE_USER"@"$2":${TARGET_REMOTE_PATH}database.sqlite $LOCAL_DATABASE_PATH exit 0 fi # ----------------------------------- if [ "$1" == "-l" ] then if [ -z "$(cat ~/.ssh/id_rsa.pub)" ] then echo "SSH key is needed. Proceed to generating it by ssh-keygen." exit 1 fi if [ -z "$2" ] then echo "Target IP is needed. Run $0 --help." exit 1 fi LOCAL_DATABASE_FILE="./database.sqlite" if [ ! -z "$3" ] then LOCAL_DATABASE_FILE="$3" fi if [ ! -f $LOCAL_DATABASE_FILE ] then echo "File $LOCAL_DATABASE_FILE is missing." exit 1 fi ssh-copy-id "$TARGET_REMOTE_USER"@"$2" ssh "$TARGET_REMOTE_USER"@"$2" "sudo apt install rsync" rsync $LOCAL_DATABASE_FILE "$TARGET_REMOTE_USER"@"$2":/home/"$TARGET_REMOTE_USER"/ ssh "$TARGET_REMOTE_USER"@"$2" "sudo mv -f ./database.sqlite ${TARGET_REMOTE_PATH}database.sqlite && sudo chown ${owner}:${owner} ${TARGET_REMOTE_PATH}database.sqlite" exit 0 fi # ----------------------------------- if [ "$1" == "-c" ] then if [ -z "$(cat ~/.ssh/id_rsa.pub)" ] then echo "SSH key is needed. Proceed to generating it by ssh-keygen." exit 1 fi if [ -z "$2" ] then echo "Target IP of the first machine is needed. Run $0 --help." exit 1 fi if [ -z "$3" ] then echo "Target IP of the second machine is needed. Run $0 --help." exit 1 fi ssh-copy-id "$TARGET_REMOTE_USER"@"$2" ssh "$TARGET_REMOTE_USER"@"$2" "sudo apt install rsync" rsync "$TARGET_REMOTE_USER"@"$2":${TARGET_REMOTE_PATH}database.sqlite ./.temp.sqlite ssh-copy-id "$TARGET_REMOTE_USER"@"$3" ssh "$TARGET_REMOTE_USER"@"$3" "sudo apt install rsync" rsync ./.temp.sqlite "$TARGET_REMOTE_USER"@"$3":/home/"$TARGET_REMOTE_USER"/ ssh "$TARGET_REMOTE_USER"@"$3" "sudo mv -f ./database.sqlite ${TARGET_REMOTE_PATH}database.sqlite && sudo chown ${owner}:${owner} ${TARGET_REMOTE_PATH}database.sqlite" rm -f ./.temp.sqlite exit 0 fi