1.安装rclone

sudo apt install rclone

2.配置腾讯云cos

rclone config

#Current remotes:
#Name                 Type
#====                 ====
#n) New remote
e/n/d/r/c/s/q> n
name> cos_test
#Type of storage to configure.
#Enter a string value. Press Enter for the default ("").
#Choose a number from below, or type in your own value
#4 / Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, Tencent COS, etc)
# \ "s3"
Storage> 4
#** See help for s3 backend at: https://rclone.org/s3/ **
#Choose your S3 provider.
#Enter a string value. Press Enter for the default ("").
#Choose a number from below, or type in your own value
#11 / Tencent Cloud Object Storage (COS)
# \ "TencentCOS"
provider> 11
#Get AWS credentials from runtime (environment variables or EC2/ECS meta data if no env vars).
#Only applies if access_key_id and secret_access_key is blank.
#Enter a boolean value (true or false). Press Enter for the default ("false").
#Choose a number from below, or type in your own value
# 1 / Enter AWS credentials in the next step
# \ "false"
env_auth> 1
#AWS Access Key ID.
#Leave blank for anonymous access or runtime credentials.
#Enter a string value. Press Enter for the default ("").
access_key_id>{SecretId}
#AWS Secret Access Key (password)
#Leave blank for anonymous access or runtime credentials.
#Enter a string value. Press Enter for the default ("").
secret_access_key>{ SecretKey}
#Endpoint for Tencent COS API.
#Enter a string value. Press Enter for the default ("").
#Choose a number from below, or type in your own value
# 6 / Chengdu Region.
# \ "cos.ap-chengdu.myqcloud.com"
endpoint> 6
#Canned ACL used when creating buckets and storing or copying objects.
#This ACL is used for creating objects and if bucket_acl isn't set, for creating buckets too.
#For more info visit https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
#Note that this ACL is applied when server side copying objects as S3
#doesn't copy the ACL from the source but rather writes a fresh one.
#Enter a string value. Press Enter for the default ("").
#Choose a number from below, or type in your own value
# 1 / Owner gets Full_CONTROL. No one else has access rights (default).
# \ "default"
acl> 1
#The storage class to use when storing new objects in Tencent COS.
#Enter a string value. Press Enter for the default ("").
#Choose a number from below, or type in your own value
# 1 / Default
# \ ""
storage_class> 1
#Edit advanced config? (y/n)
#n) No (default)
y/n> n
#Remote config
#--------------------
#[cos_test]
#provider = TencentCOS
#env_auth = false
#access_key_id =  SecretId
#secret_access_key =  SecretKey
#endpoint = cos.ap-chengdu.myqcloud.com
#acl = default
#--------------------
#y) Yes this is OK (default)
#e) Edit this remote
#d) Delete this remote
y/e/d> y
#Current remotes:
#Name                 Type
#====                 ====
#cos_test             s3
#q) Quit config
e/n/d/r/c/s/q> q

列出cos文件

rclone ls cos_test:

挂载cos到本地

rclone mount cos:/backup-0000000000/ /data/rclone/cos --cache-dir /tmp --vfs-cache-mode writes &>/data/rclone/rclone.log &

备份网站脚本

#!/bin/bash

# 设置备份目录和网站目录
backup_dir="/data/rclone/cos/website"
website_dir="/data/nginx/html"


# 循环备份每个二级目录
for dir in "$website_dir"/*/
do
    # 获取二级目录名
    dir_name=$(basename "$dir")

    # 设置备份文件名(以当前日期时间和二级目录名为文件名)
    backup_file="$backup_dir/${dir_name}_backup_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz"

    # 执行备份
    tar -czf "$backup_file" "$dir"
    echo $backup_file

    # 删除旧的备份文件(保留最近7天的备份)
    find "$backup_dir" -type f -name "${dir_name}_backup_*" -mtime +7 -exec rm {} \;
done

备份数据库脚本

基于运行在docker容器中的数据库

#!/bin/bash

# 设置备份目录和 MySQL 容器名称

backup_dir="/data/rclone/cos/database"
container_name="mysql"
password="密码"

# 获取当前日期时间

current_date=$(date +"%Y-%m-%d_%H-%M-%S")

# 执行 db1数据库备份

sudo docker exec "$container_name" mysqldump -u root --password=$password db1 > "$backup_dir/db1_backup_$current_date.sql"

# 执行 db2数据库备份

sudo docker exec "$container_name" mysqldump -u root --password=$password db2 > "$backup_dir/db2_backup_$current_date.sql"

# 如果需要,删除旧的备份文件(保留最近7天的备份)

find "$backup_dir" -type f -name "db1_backup_*" -mtime +7 -exec rm {} \;
find "$backup_dir" -type f -name "db2_backup_*" -mtime +7 -exec rm {} \;
最后修改:2024 年 04 月 21 日
如果觉得我的文章对你有用,请随意赞赏