check_ssh_connection() { if ! ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -T [email protected] | grep -q meleuo; then echo "登录失败" >&2 exit 1 fi }
check_age_command() { if ! command -v age &> /dev/null; then echo "age 命令不可用" >&2 exit 1 fi }
cleanup() { local uuid=$1 if [[ -d /tmp/${uuid} ]]; then rm -rf "/tmp/${uuid}" fi }
backup_and_push() { local dir_path=$1 local taskname=$2 local dir_name dir_name=$(basename "${dir_path}") local uuid uuid=$(openssl rand -hex 16) local datetime datetime=$(date +"%Y%m%d%H%M") local archive_name="${taskname}_${uuid}_${datetime}.tar.gz" local encrypted_name="${taskname}_${datetime}.bin"
mkdir "/tmp/${uuid}" cd "/tmp/${uuid}" || exit 1
if ! tar -czf "${archive_name}" -C "$(dirname "${dir_path}")" "$(basename "${dir_path}")"; then echo "压缩失败" >&2 cleanup "${uuid}" exit 1 fi
if ! tar cvz "${archive_name}" | age -r [age] > "${encrypted_name}"; then echo "加密失败" >&2 cleanup "${uuid}" exit 1 fi