惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threatpost
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
O
OpenAI News
Project Zero
Project Zero
G
GRAHAM CLULEY
P
Privacy International News Feed
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
W
WeLiveSecurity
P
Proofpoint News Feed
腾讯CDC
Cloudbric
Cloudbric
S
Secure Thoughts
C
Check Point Blog
博客园 - Franky
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
GbyAI
GbyAI
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
F
Fortinet All Blogs
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
C
Cisco Blogs
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research

atpX

自建 S3 兼容对象存储服务 Garage 听风的歌 偶尔是深夜就好了 新玩具 AirPods Pro 3 公交车上的时间 从 WHOIS 到 RDAP 通过 WireGuard 访问 NFS 共享文件 自建音乐串流服务探索 我停止了探索 Fediverse 互联网背景噪音 我的数字生活降级 小城与确定性的墙 一只特立独行的猪 Fediverse 与社交 承认的勇气 mediaX - 轻量书影音记录管理工具 我的博客写作流程 网站新增 Misc 页面 擅长对线的鲍勃 在 Chroot 环境下使用 Rsync 同步 再见 JavaScript 当我玩博客时我在玩什么 为什么我的博客没有友链页面 小熊猫与大熊猫 (HDR 照片测试) 是时候为网站开启 HTTP/3 支持了吗 使用 AdGuard Home 搭建自用 DoH 服务 谈谈读书与消遣 2023 年终总结 西安两日游 从 AirPods「升级」到 EarPods Docker 搭建去中心化的微博客平台 Mastodon 聊聊 iPhone 15 Pro 使用感受 能否将 TOTP 二次验证存放在密码管理器里 认识史蒂夫·乔布斯 与“锤哥”克里斯·海姆斯沃斯一起养生 逃离爆炸的信息 你想活出怎样的人生 重温《龙珠》动画 为什么关闭评论 从域名注册商到 DNS 服务,找到自己的组合 如何提高用户网页阅读体验 从 Typecho 迁移到 Hugo 个人博客的最终归宿是静态网站吗 我选择了放弃 jQuery 关于我的小破站折腾速度优化这件事 记一次家庭网络折腾 贴一些 SmokePing 记录 我的域名邮箱选择 不存在的语录 Typecho 评论验证插件 CaptchaPlus 谈谈写字这件事 新的网站 Logo 探索 曲线解决 Typecho 图片占位抖动问题 自建 vaultwarden / bitwarden_rs 密码管理器 使用 Plausible 自建网站流量统计分析工具 如何优雅地徒手剥开火腿肠 简单的集中隔离生活记录 新玩具 M2 MacBook Air 使用 acme.sh 申请 Google 公共证书 Docker 搭建去中心化的微博客平台 Misskey 搭建 Lsky Pro 兰空图床 折腾树莓派系统的一天 我的吉卜力之旅 使用 Uptime Kuma 自建服务器/网站在线率监控 Spartan Host Review - Best Hosting & Service Support Debian 下 Nginx 配合 Fail2Ban 减少恶意扫描和攻击 升级 Typecho 到最新开发版本 v1.2.0 Linux rm 命令详解 Debian 系统安装 Docker 教程 BandwagonHost - Reliable Blog and Business VPS Hosting 使用 Isso 为 Hugo/Hexo 等静态网站添加评论功能 推荐 5 款免费开源的网站流量分析统计工具 搭建 Shynet 网站流量统计分析工具 聊聊 DDoS 攻击那些事 Hugo 使用 Fancybox 实现图片灯箱/放大功能 从 Debian 10 升级到 Debian 11 教程 使用 Umami 自建网站流量统计分析工具 Linux 一次执行多个命令的 3 种方式 Typecho 修改永久链接后旧链接 301 跳转到新链接 从不同需求推荐几家稳定可靠的 VPS 服务商 Build EchoIP service with Docker 浅谈一下这些年折腾过的 VPS 单线复用解决一根网线同时 IPTV 和宽带问题 不完全吃灰的树莓派 开发一个自己的 Telegram Bot 使用 Nginx 实现 TCP 四层反向代理 画画真难 我读村上春树 使用 acme.sh 自动签发和更新证书 卡拉马佐夫式悲剧 安装 File Browser 轻量网盘工具 新玩具树莓派到手 记一次服务器崩溃 使用 rsync 同步文件 短信的消失 幻灭的艺术家 新玩具戴尔 S2721DGF 开箱 蝴蝶 用 Docker 整合 SeafilePro 搭建私人云盘 搭建自己的 kms 服务器
Linux 定时自动备份数据到 OneDrive/Google Drive
ATP · 2020-11-18 · via atpX

鉴于上次的服务器崩溃事件,写一个数据自动备份脚本就显得尤为重要。折腾了一晚,最终实现效果是每晚三点自动备份数据库和网站等数据并使用 Openssl 加密归档,保留最近一个月的数据,通过开源rclone 工具同步到 Google Drive 和 Onedrive 上,这样就万无一失了。🍟

编写 Shell 脚本

脚本是自己使用,因此比较简单。

#!/bin/bash
# Writen by Atp on Nov 15, 2020
# Website:  https://atpx.com

DATE=`date +%Y%m%d`
BACKUP_TEMP_PATH=/backup/tmp
# Database
DB_NAME=
DB_USR=
DB_PW=
DB_OUT_NAME="db_$DATE"
# Website
WEB_PATH=
WEB_OUT_NAME="web_$DATE"
# Archive
ARCHIVE_PATH=/backup/archives
ARCHIVE_LOG_FILE=/backup/log/backup.log

# Backup mysql
# mysqldump -u$DB_USR -p$DB_PW --databases $DB_NAME > $BACKUP_TEMP_PATH/$DB_OUT_NAME.sql
mysqldump --login-path=$DB_USR --databases $DB_NAME > $BACKUP_TEMP_PATH/$DB_OUT_NAME.sql

# Backup webdsite
cd $WEB_PATH
zip -rq $BACKUP_TEMP_PATH/$WEB_OUT_NAME.zip *

# Archive files
cd $BACKUP_TEMP_PATH
# tar cf - * | openssl enc -e -aes-128-cbc -pbkdf2 -k [password] -out $ARCHIVE_PATH/$DATE.tar.aes
tar cf - * | gpg -e -r [recipient] -o $ARCHIVE_PATH/$DATE.tar.gpg

# Backup the latest 30 days
rm $BACKUP_TEMP_PATH/*
find $ARCHIVE_PATH -name "*.tar.gpg" -type f -mtime +30 -exec rm {} \;

# Sync to Google Drive
rclone sync $ARCHIVE_PATH gdrive:[sync path]

# Sync to Microsoft OneDrive
rclone sync $ARCHIVE_PATH onedrive:[sync path]

# Output log file
echo -e "\e[1;32m---------------------------------------------\e[0m" >> $ARCHIVE_LOG_FILE
echo -e "Backup date: "$(date +%Y-%m-%d)"\t""Finish time: "$(date +%H:%M:%S) >> $ARCHIVE_LOG_FILE
echo -e "\e[1;32m---------------------------------------------\e[0m\n" >> $ARCHIVE_LOG_FILE

这里说明一下,使用mysqldump备份的时候,如果通过账号密码导出会提示使用明文密码不安全的警告,虽然不影响使用,但看着就不爽,因此改用了 MySQL5.7 之后提供的新方式,通过 --login-path= 的方式登录数据库,首先保存一个加密验证信息:

mysql_config_editor set --login-path=[name] --host=localhost --user=[usr] --password

查看所设置的信息:

mysql_config_editor print --all

删除指定信息:

mysql_config_editor remove --login-path=[usr]

值得一提的,我第一次设置的时候毫无疑问的失败了- -,一直无法登录,后来才知道所设置的用户密码中不能包括"#“符号,不然就会报错。

配置 rclone

官方有一键安装脚本,适用于 Linux/macOS/BSD 系统:

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

之后需要在服务器上配置 Google Dirve 和 OneDrive 的连接。以下默认使用官方 API,由于官方 API 为所有用户共享,速度可能容易被限制,你如果要求较高可以官方参照教程去 Google Cloud 或者 Microsoft Azure(目前 Azure 新建 API 有效值最多两年)自己注册应用并设置 API 获取相应的 client_idclient_secret 以减少限制。

连接 Google Drive

输入 rclone config 命令,根据提示的信息进行操作,下面演示的版本为 v1.57.0,不同版本可能会略有出入,网盘序号也会改变,配置的时候稍微注意点就行了。

查看连接 Google Drive 步骤
- using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n  # 输入 n,新建远程连接
name> gdrive  # 输入自定义名称,用于区分不同连接
Option Storage.
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.
 1 / 1Fichier
   \ "fichier"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Better checksums for other remotes
   \ "hasher"
 7 / Box
   \ "box"
 8 / Cache a remote
   \ "cache"
 9 / Citrix Sharefile
   \ "sharefile"
10 / Compress a remote
   \ "compress"
11 / Dropbox
   \ "dropbox"
12 / Encrypt/Decrypt a remote
   \ "crypt"
13 / Enterprise File Fabric
   \ "filefabric"
14 / FTP Connection
   \ "ftp"
15 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
16 / Google Drive
   \ "drive"
17 / Google Photos
   \ "google photos"
18 / Hadoop distributed file system
   \ "hdfs"
19 / Hubic
   \ "hubic"
20 / In memory object storage system.
   \ "memory"
21 / Jottacloud
   \ "jottacloud"
22 / Koofr
   \ "koofr"
23 / Local Disk
   \ "local"
24 / Mail.ru Cloud
   \ "mailru"
25 / Mega
   \ "mega"
26 / Microsoft Azure Blob Storage
   \ "azureblob"
27 / Microsoft OneDrive
   \ "onedrive"
28 / OpenDrive
   \ "opendrive"
29 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
30 / Pcloud
   \ "pcloud"
31 / Put.io
   \ "putio"
32 / QingCloud Object Storage
   \ "qingstor"
33 / SSH/SFTP Connection
   \ "sftp"
34 / Sia Decentralized Cloud
   \ "sia"
35 / Sugarsync
   \ "sugarsync"
36 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
37 / Transparently chunk/split large files
   \ "chunker"
38 / Union merges the contents of several upstream fs
   \ "union"
39 / Uptobox
   \ "uptobox"
40 / Webdav
   \ "webdav"
41 / Yandex Disk
   \ "yandex"
42 / Zoho
   \ "zoho"
43 / http Connection
   \ "http"
44 / premiumize.me
   \ "premiumizeme"
45 / seafile
   \ "seafile"
Storage> 16 # 找到 Google Drive 的序号,我这里的版本是 16
Option client_id.
Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> # 一般留空,直接回车,如果你需要同步大量文件可以自己去 Google Cloud 注册应用程序并设置 API,而不使用 rclone 的 API 减少限制
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 一般留空,直接回车
Option scope.
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value.
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1 # 根据你的需求选择访问权限,这里选择完全权限
Option root_folder_id.
ID of the root folder.
Leave blank normally.
Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.
Enter a string value. Press Enter for the default ("").
root_folder_id> # 一般留空,直接回车
Option service_account_file.
Service Account Credentials JSON file path.
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.
Enter a string value. Press Enter for the default ("").
service_account_file> # 一般留空,直接回车
Edit advanced config?
y) Yes
n) No (default)
y/n> n # 输入 n,不进行高级设置
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine

y) Yes (default)
n) No
y/n> n # 输入 n
Option config_verification_code.
Verification code
Go to this URL, authenticate then paste the code here.
https://accounts.google.com/o/oauth2/xxxxxx
Enter a string value. Press Enter for the default ("").
config_verification_code> # 输入访问上述网址后的 token
Configure this as a Shared Drive (Team Drive)?

y) Yes
n) No (default)
y/n> n # 是否配置为团队盘
--------------------
[gdrive]
type = drive
scope = drive
token = {"access_token":""}
team_drive = 
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y # 上述配置是否正确,输入 y 确认即可完成配置
Current remotes:

Name                 Type
====                 ====
gdrive               drive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q # 输入 q 退出配置

连接 OneDrive

连接 OneDrive 的 token 需要在本地 Win­dows 电脑上下载 Windows 版 rclone 获取,下载后解压,在 rclone 文件夹中的资源管理器地址栏输入 cmd,打开命令提示符,输入 rclone 配置中提示的命令:

rclone authorize "onedrive"

接下来会弹出浏览器,登录账号进行授权,然后返回 cmd 窗口会出现类似信息:

2021/11/27 21:53:38 NOTICE: Log in and authorize rclone for access
2021/11/27 21:53:38 NOTICE: Waiting for code...
2021/11/27 21:53:42 NOTICE: Got code
Paste the following into your remote machine --->
xxxx
<---End paste

复制保存对应 “xxxx” 的所有内容,接下来会用到,然后回到服务器上输入 rclone config 进行配置:

查看连接 OneDrive 步骤
- using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n  # 输入 n,新建远程连接
name> onedrive  # 输入自定义名称,用于区分不同连接
Option Storage.
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.
 1 / 1Fichier
   \ "fichier"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Better checksums for other remotes
   \ "hasher"
 7 / Box
   \ "box"
 8 / Cache a remote
   \ "cache"
 9 / Citrix Sharefile
   \ "sharefile"
10 / Compress a remote
   \ "compress"
11 / Dropbox
   \ "dropbox"
12 / Encrypt/Decrypt a remote
   \ "crypt"
13 / Enterprise File Fabric
   \ "filefabric"
14 / FTP Connection
   \ "ftp"
15 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
16 / Google Drive
   \ "drive"
17 / Google Photos
   \ "google photos"
18 / Hadoop distributed file system
   \ "hdfs"
19 / Hubic
   \ "hubic"
20 / In memory object storage system.
   \ "memory"
21 / Jottacloud
   \ "jottacloud"
22 / Koofr
   \ "koofr"
23 / Local Disk
   \ "local"
24 / Mail.ru Cloud
   \ "mailru"
25 / Mega
   \ "mega"
26 / Microsoft Azure Blob Storage
   \ "azureblob"
27 / Microsoft OneDrive
   \ "onedrive"
28 / OpenDrive
   \ "opendrive"
29 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
30 / Pcloud
   \ "pcloud"
31 / Put.io
   \ "putio"
32 / QingCloud Object Storage
   \ "qingstor"
33 / SSH/SFTP Connection
   \ "sftp"
34 / Sia Decentralized Cloud
   \ "sia"
35 / Sugarsync
   \ "sugarsync"
36 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
37 / Transparently chunk/split large files
   \ "chunker"
38 / Union merges the contents of several upstream fs
   \ "union"
39 / Uptobox
   \ "uptobox"
40 / Webdav
   \ "webdav"
41 / Yandex Disk
   \ "yandex"
42 / Zoho
   \ "zoho"
43 / http Connection
   \ "http"
44 / premiumize.me
   \ "premiumizeme"
45 / seafile
   \ "seafile"
Storage> 27 # 找到 OneDrive 的序号,我这里的版本是 27
Option client_id.
Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> # 一般留空,直接回车,如果你需要同步大量文件可以自己去 Microsoft Azure 注册应用程序并设置 API,而不使用 rclone 的 API 减少限制
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 一般留空,直接回车
Option region.
Choose national cloud region for OneDrive.
Enter a string value. Press Enter for the default ("global").
Choose a number from below, or type in your own value.
 1 / Microsoft Cloud Global
   \ "global"
 2 / Microsoft Cloud for US Government
   \ "us"
 3 / Microsoft Cloud Germany
   \ "de"
 4 / Azure and Office 365 operated by 21Vianet in China
   \ "cn"
region> 1 # 选择你的 OneDrive 区域
Edit advanced config?
y) Yes
n) No (default)
y/n> n # 输入 n,不进行高级设置
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine

y) Yes (default)
n) No
y/n> n # 输入 n
Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
        rclone authorize "onedrive"
Then paste the result.
Enter a string value. Press Enter for the default ("").
config_token> # 粘贴前面在 Windows 上获取的内容
Option config_type.
Type of connection
Enter a string value. Press Enter for the default ("onedrive").
Choose a number from below, or type in an existing value.
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
   / Sharepoint site name or URL
 3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
   \ "url"
 4 / Search for a Sharepoint site
   \ "search"
 5 / Type in driveID (advanced)
   \ "driveid"
 6 / Type in SiteID (advanced)
   \ "siteid"
   / Sharepoint server-relative path (advanced)
 7 | E.g. /teams/hr
   \ "path"
config_type> 1 # 输入你的 OneDrive 类型
Drive OK?

Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=xxxxx

y) Yes (default)
n) No
y/n> y # 输入 y
--------------------
[onedrive]
type = onedrive
client_id = 
client_secret = 
token = {"access_token":"","expiry":""}
drive_id = 
drive_type = personal
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y # 输入 y 确认
Current remotes:

Name                 Type
====                 ====
gdrive               drive
onedrive             onedrive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q # 输入 q 退出配置

测试配置

配置完成后,你可以同步一个简单的文件进行测试,比如在 /home/rclone-test 目录下新建一个 1.txt 文件,然后使用同步命令:

rclone sync /home/rclone-test onedrive:rclone-test/temp

然后登录你的 OneDrive 查看 rclone-test/temp 目录下是否有 1.txt 文件。

至于 rclone 完整的命令操作,可以查看官方文档

设置定时任务

设置好 rclone 之后就该定时执行备份脚本了,这里通过 Crontab 设置定时任务,每晚两点执行备份脚本,记得给脚本可执行权限:

crontab -e
00 2 * * * /backup/backup.sh

使用说明:

分 时 日 月 周 执行命令
第 1 列:分钟 0-59,每分钟用 * 或者 */1 表示,整点分钟数为 00 或 0
第 2 列:小时 0-23(0 表示 0 点)
第 3 列:日 1-31
第 4 列:月 1-12
第 5 列:星期 0-6(0 表示星期天)
第 6 列:运行的命令

Updated 2020-11-21

经研究发现,Openssl 并不适合用于文档加密,因此改用 GnuPG