Administrator
发布于 2018-03-29 / 7 阅读
0
0

使用Rclone挂载网盘,自动备份网站数据库

本教程适用于Ubuntu、Debian系的系统 此处以挂载OneDrive为例,如需挂载Google Drive可参考:Huyal大佬的文章 挂载OneDrive,参考:Rat's Blog

获取OneDrive客户端授权

在Windows电脑上下载rclone,下载地址:https://github.com/ncw/rclone/releases。然后解压,进入解压后的文件夹,按住Shift键和F10后选择在此处打开命令窗口,输入以下命令
rclone authorize "onedrive"
会出现以下信息:
C:\Users\Administrator\Downloads\rclone-v1.40-windows-amd64>rclone authorize "on
edrive"
2018/03/28 22:14:11 NOTICE: Config file "C:\\Users\\Administrator\\.config\\rclo
ne\\rclone.conf" not found - using defaults
Choose OneDrive account type?
 * Say b for a OneDrive business account
 * Say p for a personal OneDrive account
b) Business
p) Personal
b/p> b     #这里选择Business,你想挂载个人版就选择p
If your browser doesn't open automatically go to the following link: http://127.
0.0.1:53682/auth       #接下来会弹出浏览器,要求你登录账号进行授权
Log in and authorize rclone for access
Waiting for code...
Got code
Paste the following into your remote machine --->
{"access_token":"XXXXXX"}             #请复制{xx}整个{ }里面的内容(包括{}),后面需要用到
<---End paste

安装Rclone

安装

原作者提供的下载地址
wget https://www.moerats.com/usr/shell/rclone_debian.sh && bash rclone_debian.sh
我备份的下载地址
wget https://storage.hilove.cc/Source%20Code/rclone_debian.sh && bash rclone_debian.sh

初始化配置

rclone config
会出现以下信息:
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> OneDrive   #随便填,后面要用到
Type of storage to configure.
Choose a number from below, or type in your own value
 1 / Alias for a existing remote
   \ "alias"
 2 / Amazon Drive
   \ "amazon cloud drive"
 3 / Amazon S3 (also Dreamhost, Ceph, Minio, IBM COS)
   \ "s3"
 4 / Backblaze B2
   \ "b2"
 5 / Box
   \ "box"
 6 / Cache a remote
   \ "cache"
 7 / Dropbox
   \ "dropbox"
 8 / Encrypt/Decrypt a remote
   \ "crypt"
 9 / FTP Connection
   \ "ftp"
10 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
11 / Google Drive
   \ "drive"
12 / Hubic
   \ "hubic"
13 / Local Disk
   \ "local"
14 / Microsoft Azure Blob Storage
   \ "azureblob"
15 / Microsoft OneDrive
   \ "onedrive"
16 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
17 / Pcloud
   \ "pcloud"
18 / QingCloud Object Storage
   \ "qingstor"
19 / SSH/SFTP Connection
   \ "sftp"
20 / Webdav
   \ "webdav"
21 / Yandex Disk
   \ "yandex"
22 / http Connection
   \ "http"
Storage> 15      #选择15,Microsoft OneDrive
Microsoft App Client Id - leave blank normally.
client_id>  #留空 
Microsoft App Client Secret - leave blank normally.
client_secret>  #留空 
Remote config
Choose OneDrive account type?
 * Say b for a OneDrive business account
 * Say p for a personal OneDrive account
b) Business
p) Personal
b/p> b  #这里选择Business,你想挂载个人版就选择p
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n  #选择n
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
    rclone authorize "onedrive"
Then paste the result below:
result> {"access_token":""}  #输入之前在客户端授权的内容
--------------------
[Rats]
client_id = 
client_secret = 
token = {"access_token":""}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y   #选择y
Current remotes:

Name                 Type
====                 ====
Rats                 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退出

Rclone常用命令

rclone copy /root OneDrive:backup         同步本地/root 到OneDrive backup文件夹下。
rclone copy OneDrive:backup /root  同步OneDrive backup文件夹到本地/root下。
rclone sync /root   OneDrive:sync    同步本地root和OneDrive sync文件夹。
更多命令请查看rclone官方文档

备份网站数据库

nano backup.sh
粘贴以下代码(来自Huyal大佬),按自己的实际情况修改
#!/bin/bash
#Name:backup.sh
#backup sql and web files to googledrive.
#rclone关联账户名称记得加":"
rclonename="xxdrive:"
#备份数据库保存路径
backupdir="/root/mysql"
#数据库用户名
mysqluser="xxx"
#数据库密码
mysqlpassword="xxx"
#数据库名
mysqldb="xxx"
if [ ! -d $backupdir ]; then
  mkdir $backupdir
fi
time=`date +%Y%m%d%H`
mysqldump -u $mysqluser -p$mysqlpassword $mysqldb | gzip > $backupdir/$mysqldb.$time.sql.gz
rclone copy $backupdir $rclonename
find $backupdir -name "*.sql.gz" -type f -mtime +5 -exec rm {} \; > /dev/null 2>&1
修改好保存以后执行
chmod u+x backup.sh
然后可以先测试一下能否正常工作
./backup.sh
如果没问题就可以设置计划任务了
crontab -e
添加如下代码(每小时备份一次,按实际情况修改)
* */1 * * * /root/backup.sh 

评论