#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --source <папка> --remote [--apply] [--delete] [--config FILE]" } source_dir="" remote="" apply=0 delete_mode=0 config_file="" while [[ $# -gt 0 ]]; do case "$1" in --source) source_dir="${2:-}"; shift 2 ;; --remote) remote="${2:-}"; shift 2 ;; --apply) apply=1; shift ;; --delete) delete_mode=1; shift ;; --config) config_file="${2:-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$source_dir" || -z "$remote" ]]; then usage >&2 exit 2 fi if ! command -v rclone >/dev/null 2>&1; then echo "rclone не найден. Установите rclone." >&2 exit 3 fi if [[ ! -d "$source_dir" ]]; then echo "Источник не найден: $source_dir" >&2 exit 2 fi if [[ "$remote" != *:* || "$remote" == -* ]]; then echo "Remote должен иметь вид имя:путь и не начинаться с дефиса." >&2 exit 2 fi if [[ -n "$config_file" && ! -f "$config_file" ]]; then echo "Конфиг rclone не найден: $config_file" >&2 exit 2 fi source_abs="$(cd "$source_dir" && pwd -P)" options=(--transfers 4 --checkers 8 --stats 30s --stats-one-line) if [[ -n "$config_file" ]]; then options+=(--config "$config_file") fi if [[ "$delete_mode" -eq 1 ]]; then if [[ "$apply" -eq 0 ]]; then echo "Режим --delete требует явного --apply." >&2 exit 2 fi echo "Внимание: rclone sync удалит в remote файлы, которых нет в источнике." >&2 command=(rclone sync "$source_abs" "$remote" "${options[@]}") else command=(rclone copy "$source_abs" "$remote" "${options[@]}") fi if [[ "$apply" -eq 0 ]]; then command+=(--dry-run) echo "Предпросмотр без загрузки. Для выполнения добавьте --apply." fi "${command[@]}" echo "Готово."