目 录CONTENT

文章目录

Rclone数据同步&备份神器如何安装及常用的设置语法功能

俊阳IT知识库
2023-06-28 / 0 评论 / 0 点赞 / 3,801 阅读 / 2,632 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-07-20,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告

前言

数据是无价的。

我们在自己建站或者用服务器存储一些数据的时候,记得一定要定期备份,防止出现数据丢失等问题。

Rclone 是一个命令行工具,支持在不同对象存储、网盘间同步、上传、下载数据,还有挂载等功能。并且通过一些设置可以实现离线下载、服务器备份等非常实用的功能(目前我认为是最强大的脚本备份同步程序)。

PS:也可以把其他网盘挂载在本地,例如阿里云盘。

支持 Linux、Windows、MacOS等系统。

再配合【cron】可以实现定时备份数据至我们的网盘、OSS存储等系统。

支持的【存储提供商】:

具体的配合第三方网盘、OSS等存储系统的使用请参考我的文章:使用Rclone同步&备份数据至七牛云、腾讯云OSS、谷歌云盘(Google Drive)

安装

官方提供了【一键安装脚本

curl https://rclone.org/install.sh | sudo bash

如果curl有问题,也可以在自己服务器中 vim install.sh,然后把下面代码粘贴进去执行install.sh脚本就好了

#!/usr/bin/env bash

# error codes
# 0 - exited without problems
# 1 - parameters not supported were used or some unexpected error occurred
# 2 - OS not supported by this script
# 3 - installed version of rclone is up to date
# 4 - supported unzip tools are not available

set -e

#when adding a tool to the list make sure to also add its corresponding command further in the script
unzip_tools_list=('unzip' '7z' 'busybox')

usage() { echo "Usage: sudo -v ; curl https://rclone.org/install.sh | sudo bash [-s beta]" 1>&2; exit 1; }

#check for beta flag
if [ -n "$1" ] && [ "$1" != "beta" ]; then
    usage
fi

if [ -n "$1" ]; then
    install_beta="beta "
fi


#create tmp directory and move to it with macOS compatibility fallback
tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'rclone-install.XXXXXXXXXX')
cd "$tmp_dir"


#make sure unzip tool is available and choose one to work with
set +e
for tool in ${unzip_tools_list[*]}; do
    trash=$(hash "$tool" 2>>errors)
    if [ "$?" -eq 0 ]; then
        unzip_tool="$tool"
        break
    fi
done  
set -e

# exit if no unzip tools available
if [ -z "$unzip_tool" ]; then
    printf "\nNone of the supported tools for extracting zip archives (${unzip_tools_list[*]}) were found. "
    printf "Please install one of them and try again.\n\n"
    exit 4
fi

# Make sure we don't create a root owned .config/rclone directory #2127
export XDG_CONFIG_HOME=config

#check installed version of rclone to determine if update is necessary
version=$(rclone --version 2>>errors | head -n 1)
if [ -z "$install_beta" ]; then
    current_version=$(curl -fsS https://downloads.rclone.org/version.txt)
else
    current_version=$(curl -fsS https://beta.rclone.org/version.txt)
fi

if [ "$version" = "$current_version" ]; then
    printf "\nThe latest ${install_beta}version of rclone ${version} is already installed.\n\n"
    exit 3
fi


#detect the platform
OS="$(uname)"
case $OS in
  Linux)
    OS='linux'
    ;;
  FreeBSD)
    OS='freebsd'
    ;;
  NetBSD)
    OS='netbsd'
    ;;
  OpenBSD)
    OS='openbsd'
    ;;  
  Darwin)
    OS='osx'
    binTgtDir=/usr/local/bin
    man1TgtDir=/usr/local/share/man/man1
    ;;
  SunOS)
    OS='solaris'
    echo 'OS not supported'
    exit 2
    ;;
  *)
    echo 'OS not supported'
    exit 2
    ;;
esac

OS_type="$(uname -m)"
case "$OS_type" in
  x86_64|amd64)
    OS_type='amd64'
    ;;
  i?86|x86)
    OS_type='386'
    ;;
  aarch64|arm64)
    OS_type='arm64'
    ;;
  armv7*)
    OS_type='arm-v7'
    ;;
  armv6*)
    OS_type='arm-v6'
    ;;
  arm*)
    OS_type='arm'
    ;;
  *)
    echo 'OS type not supported'
    exit 2
    ;;
esac


#download and unzip
if [ -z "$install_beta" ]; then
    download_link="https://downloads.rclone.org/rclone-current-${OS}-${OS_type}.zip"
    rclone_zip="rclone-current-${OS}-${OS_type}.zip"
else
    download_link="https://beta.rclone.org/rclone-beta-latest-${OS}-${OS_type}.zip"
    rclone_zip="rclone-beta-latest-${OS}-${OS_type}.zip"
fi

curl -OfsS "$download_link"
unzip_dir="tmp_unzip_dir_for_rclone"
# there should be an entry in this switch for each element of unzip_tools_list
case "$unzip_tool" in
  'unzip')
    unzip -a "$rclone_zip" -d "$unzip_dir"
    ;;
  '7z')
    7z x "$rclone_zip" "-o$unzip_dir"
    ;;
  'busybox')
    mkdir -p "$unzip_dir"
    busybox unzip "$rclone_zip" -d "$unzip_dir"
    ;;
esac

cd $unzip_dir/*

#mounting rclone to environment

case "$OS" in
  'linux')
    #binary
    cp rclone /usr/bin/rclone.new
    chmod 755 /usr/bin/rclone.new
    chown root:root /usr/bin/rclone.new
    mv /usr/bin/rclone.new /usr/bin/rclone
    #manual
    if ! [ -x "$(command -v mandb)" ]; then
        echo 'mandb not found. The rclone man docs will not be installed.'
    else 
        mkdir -p /usr/local/share/man/man1
        cp rclone.1 /usr/local/share/man/man1/
        mandb
    fi
    ;;
  'freebsd'|'openbsd'|'netbsd')
    #binary
    cp rclone /usr/bin/rclone.new
    chown root:wheel /usr/bin/rclone.new
    mv /usr/bin/rclone.new /usr/bin/rclone
    #manual
    mkdir -p /usr/local/man/man1
    cp rclone.1 /usr/local/man/man1/
    makewhatis
    ;;
  'osx')
    #binary
    mkdir -m 0555 -p ${binTgtDir}
    cp rclone ${binTgtDir}/rclone.new
    mv ${binTgtDir}/rclone.new ${binTgtDir}/rclone
    chmod a=x ${binTgtDir}/rclone
    #manual
    mkdir -m 0555 -p ${man1TgtDir}
    cp rclone.1 ${man1TgtDir}    
    chmod a=r ${man1TgtDir}/rclone.1
    ;;
  *)
    echo 'OS not supported'
    exit 2
esac


#update version variable post install
version=$(rclone --version 2>>errors | head -n 1)

printf "\n${version} has successfully installed."
printf '\nNow run "rclone config" for setup. Check https://rclone.org/docs/ for more details.\n\n'
exit 0

如果是Windows,可以去官网下载对应的压缩包进行安装。

设置

Rclone设置

rclone config - 进入交互式配置选项,进行添加、删除、管理网盘等操作。详细操作参见:https://rclone.org/commands/rclone_config/

例如:

  • rclone config file - 显示配置文件的路径,一般配置文件在 ~/.config/rclone/rclone.conf
  • rclone config show - 显示配置文件信息

命令语法

# 本地到网盘
rclone [功能选项] <本地路径> <网盘名称:路径> [参数] [参数] ...

# 网盘到本地
rclone [功能选项] <网盘名称:路径> <本地路径> [参数] [参数] ...

# 网盘到网盘
rclone [功能选项] <网盘名称:路径> <网盘名称:路径> [参数] [参数] ...

常用的功能选项

  • rclone copy - 复制
  • rclone move - 移动,如果要在移动后删除空源目录,请加上 --delete-empty-src-dirs 参数
  • rclone sync - 同步:将源目录同步到目标目录,只更改目标目录。
  • rclone size - 查看网盘文件占用大小。
  • rclone delete - 删除路径下的文件内容。
  • rclone purge - 删除路径及其所有文件内容。
  • rclone mkdir - 创建目录。
  • rclone rmdir - 删除目录。
  • rclone rmdirs - 删除指定灵境下的空目录。如果加上 --leave-root 参数,则不会删除根目录。
  • rclone check - 检查源和目的地址数据是否匹配。
  • rclone ls - 列出指定路径下的所有的文件以及文件大小和路径。
  • rclone lsl - 比上面多一个显示上传时间。
  • rclone lsd 列出指定路径下的目录
  • rclone lsf - 列出指定路径下的目录和文件

常用参数

  • -n = --dry-run - 测试运行,用来查看 rclone 在实际运行中会进行哪些操作。
  • -P = --progress - 显示实时传输进度,500mS 刷新一次,否则默认 1 分钟刷新一次。
  • --cache-chunk-size SizeSuffi - 块的大小,默认5M,理论上是越大上传速度越快,同时占用内存也越多。如果设置得太大,可能会导致进程中断。
  • --cache-chunk-total-size SizeSuffix - 块可以在本地磁盘上占用的总大小,默认10G。
  • --transfers=N - 并行文件数,默认为4。在比较小的内存的VPS上建议调小这个参数,比如128M的小鸡上使用建议设置为1。
  • --config string - 指定配置文件路径,string为配置文件路径。
  • --ignore-errors - 跳过错误。比如 OneDrive 在传了某些特殊文件后会提示Failed to copy: failed to open source object: malwareDetected: Malware detected,这会导致后续的传输任务被终止掉,此时就可以加上这个参数跳过错误。但需要注意 RCLONE 的退出状态码不会为0

更多参数参考:https://rclone.org/commands/rclone_mount/

日志

rclone 有 4 个级别的日志记录,ERRORNOTICEINFODEBUG。默认情况下,rclone 将生成 ERRORNOTICE 级别消息。

  • -q - rclone将仅生成 ERROR 消息。
  • -v - rclone将生成 ERRORNOTICEINFO 消息,推荐此项
  • -vv - rclone 将生成 ERRORNOTICEINFODEBUG 消息。
  • --log-level LEVEL - 标志控制日志级别。

输出日志到文件

使用 --log-file=FILE 选项,rclone 会将 ErrorInfoDebug 消息以及标准错误重定向到 FILE,这里的 FILE 是你指定的日志文件路径。

另一种方法是使用系统的指向命令,比如:

rclone sync -v Onedrive:/DRIVEX Gdrive:/DRIVEX > "~/DRIVEX.log" 2>&1

文件过滤

--exclude - 排除文件或目录。

--include - 包含文件或目录。

--filter - 文件过滤规则,相当于上面两个选项的其它使用方式。包含规则以 + 开头,排除规则以 - 开头。

文件类型过滤

比如 --exclude "*.bak"--filter "- *.bak",排除所有 bak 文件。也可以写作。

比如 --include "*.{png,jpg}"--filter "+ *.{png,jpg}",包含所有 pngjpg 文件,排除其他文件。

--delete-excluded 删除排除的文件。需配合过滤参数使用,否则无效。

目录过滤

目录过滤需要在目录名称后面加上 /,否则会被当做文件进行匹配。以 / 开头只会匹配根目录(指定目录下),否则匹配所目录。这同样适用于文件。

--exclude ".git/" 排除所有目录下的.git 目录。

--exclude "/.git/" 只排除根目录下的.git 目录。

--exclude "{Video,Software}/" 排除所有目录下的 VideoSoftware 目录。

--exclude "/{Video,Software}/" 只排除根目录下的 VideoSoftware 目录。

--include "/{Video,Software}/**" 仅包含根目录下的 VideoSoftware 目录的所有内容。

文件大小过滤

默认大小单位为 kBytes ,但可以使用 kMG 后缀。

--min-size 过滤小于指定大小的文件。比如 --min-size 50 表示不会传输小于 50k 的文件。

--max-size 过滤大于指定大小的文件。比如 --max-size 1G 表示不会传输大于 1G 的文件。

TIPS: 博主在实际使用中发现大小过滤两个选项不能同时使用。

过滤规则文件

--filter-from <规则文件> 从文件添加包含 / 排除规则。比如 --filter-from filter-file.txt

过滤规则文件示例:

- secret*.jpg
+ *.jpg
+ *.png
+ file2.avi
- /dir/Trash/**
+ /dir/**
- *

更多高级用法请参考官方文档:https://rclone.org/filtering/

0

评论区