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QoUS4M3vrNgBq+NJGb18EqfLon6egmClsMxU2dN80Dbx+w1uwJLo6wGMtuIWMwfGG5gAIxpU= user@rclone-serve-test
#EOF
chmod 600 ${PUBLIC_KEY}

    run rclone serve sftp --authorized-keys "${PUBLIC_KEY}" --addr ${IP}:${PORT} ${DATADIR}
    
    echo type=sftp
    echo ssh=ssh -i ${PRIVATE_KEY} -o StrictHostKeyChecking=no -p ${PORT} user@${IP}
    echo _connect=${IP}:${PORT}
}

. $(dirname "$0")/rclone-serve.bash
