当前位置:正能量 » 代码编程 » Linux » 写一个ossutil的工具脚本

写一个ossutil的工具脚本

缘由

经常要备份linux上的文件,直接复制和下载有时候不是很方便,即使下载下来了,也可能由于一些原因给忘记保存到相对安全的磁盘上。故写一个脚本来实现备份。

脚本

#!/bin/bash

# 阿里云 OSS 相关配置
ACCESS_KEY="<YourAccessKey>"
SECRET_KEY="<YourSecretKey>"
ENDPOINT="<YourEndpoint>"
BUCKET="<YourBucket>"

# 检查命令行参数
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 source_file target_file"
    exit 1
fi

# 获取待上传的文件路径和目标文件路径
SOURCE=$1
TARGET=$2

# 检查是否已安装 ossutil
if ! command -v ossutil64 &> /dev/null; then
    echo "ossutil not found, downloading..."
    # 下载 ossutil64,此处最好替换成自己的ossutil地址,文件去阿里云官网找
    wget -O ossutil64 https://demo.znl.net/ossutil64
    # 赋予执行权限
    chmod +x ossutil64
    # 移动到系统路径
    sudo mv ossutil64 /usr/local/bin/ossutil64
fi

# 使用 ossutil 配置
ossutil64 config --access-key-id $ACCESS_KEY --access-key-secret $SECRET_KEY --endpoint $ENDPOINT

# 执行 ossutil 命令
ossutil64 cp -r $SOURCE oss://$BUCKET/$TARGET

使用方式

bash <(curl -s https://file.znl.net/jiaoben.sh) /path/to/source/file /path/to/target/file

效果

Succeed: Total num: 1, size: 163,480,502. OK num: 1(upload 1 files).

average speed 11756000(byte/s)

13.906585(s) elapsed
root@C20230915182797:~/.halo# bash <(curl -s https://demo.znl.net/ossutil.sh) ./file-backup-20231227.zip file/file-backup-20231227.zip
cp: overwrite "oss://mmgg/file/file-backup-20231227.zip"(y or N)? y
Succeed: Total num: 1, size: 163,480,502. OK num: 1(upload 1 files).

average speed 9181000(byte/s)

17.806552(s) elapsed

注意事项

jiaoben.sh需上传到自己的web服务器上,这样每次只需要按这个命令去执行就可以自动备份了。
OssUtil需要时间同步,如果当前linux机器的时间不正确,可能会有以下错误:

retry count:9,multipart upload file:file-backup-20231227.zip.
Total num: 1, size: 163,480,502. Dealed num: 0, Transfer size: 0. When error happens.

average speed 0(byte/s)
Total num: 1, size: 163,480,502. Dealed num: 1(Error 1 files), OK size: 0, Progress: 0.000%, Speed: 0.00KB/sError: oss: service returned error: StatusCode=403, ErrorCode=RequestTimeTooSkewed, ErrorMessage="The difference between the request time and the current time is too large.", RequestId=658BA4164F7BC43236D9A708, Ec=0002-00000504, File=file-backup-20231227.zip

遇到此情况,请自行通过ntpdate同步时间即可。

未经允许不得转载:正能量 » 写一个ossutil的工具脚本

相关文章

评论 (0)

7 + 9 =