利用Google Drive的无限网盘做数据定时备份


最近搬家了,脱坑阿里云,常驻AWS了!哈哈~离开阿里云感觉一身轻松了......不用在粪坑里面挣扎!

废话不多说:既然出国了,那么就可以愉快的玩耍Google Drive了.看了很多前辈们,老师们都在玩.........所以今天我也来写个最新教程.原创归互联网吧~~~毕竟我是互联网的拾荒者.把一些无用的命令重新整理再次发布!

我们先创建2个目录,一个是google drive挂载目录,一个是备份目录.为什么要2个目录,不可以直接备份到google drive去吗?可以的但是由于备份文件是递增的,而网盘是即时同步.所以害怕产生碎片文件.删起来麻烦!以前七牛是这样,现在google drive就不清楚了.测试过的小伙伴记得告诉我情况!

安装

本教程只适用Debian/Ubuntu系统,如果你是CentOS,请参考:https://rclone.org/install/,安装rclone。挂载方法和下面差不多。

废话不多说了,我们"干巴爹"吧!

mkdir /root/gg-web-bak #Google Drive挂载目录
mkdir /root/webbak  #临时备份数据目录

1、安装rclone软件,利用rclone来挂载硬盘

wget https://blog.e9china.net/ssh/rclone_debian.sh && bash rclone_debian.sh

       2、初始化配置

rclone config
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> webbak  #要记住,后面要用到
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 Compliant Storage Providers (AWS, Ceph, Dreamhost, IBM COS, Minio)
   \ "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 / JottaCloud
   \ "jottacloud"
14 / Local Disk
   \ "local"
15 / Mega
   \ "mega"
16 / Microsoft Azure Blob Storage
   \ "azureblob"
17 / Microsoft OneDrive
   \ "onedrive"
18 / OpenDrive
   \ "opendrive"
19 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
20 / Pcloud
   \ "pcloud"
21 / QingCloud Object Storage
   \ "qingstor"
22 / SSH/SFTP Connection
   \ "sftp"
23 / Webdav
   \ "webdav"
24 / Yandex Disk
   \ "yandex"
25 / http Connection
   \ "http"
Storage> 11  #选择11,Google Drive
#然后下面就一直回车
Google Application Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> 
Google Application Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> 
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> 
ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders. (see docs).
Enter a string value. Press Enter for the default ("").
root_folder_id> 
Service Account Credentials JSON file path 
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> 
#一直到这步
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n #选择n
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine or Y didn't work
y) Yes
n) No
y/n> n  #选择n
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth....  #复制到浏览器打开,获取验证码
Log in and authorize rclone for access
Enter verification code>  #填入上面获取到的验证码
Configure this as a team drive?
y) Yes
n) No
y/n> y  #选择y
Fetching team drive list...
No team drives found in your account--------------------
[Rats]
client_id = 
client_secret = 
service_account_file = 
token = {"access_token":"ya29.................."}
team_drive = 
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y  #选择y
Current remotes:

Name                 Type
====                 ====
Rats                 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退出 就安装完毕了

     3、挂载为磁盘

#挂载为磁盘
rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000

DriveName为初始化配置填的name,Folder为Google Drive里的文件夹,LocalFolder为VPS上的本地文件夹。

例子:

rclone mount webbak:web-bak /root/gg-web-bak --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 --daemon

rclone mount  刚才配置的名称:google drive目录 本地目录 .......... 最后面输入--daemon是让他后台运行

挂载成功后,输入df -h命令查看即可!

     4、卸载磁盘

fusermount -qzu LocalFolder

自启动

1、下载并编辑脚本
使用命令:

wget https://blog.e9china.net/ssh/rcloned && nano rcloned

修改一下内容:

NAME=""  #rclone name名,及配置时输入的Name
REMOTE=''  #远程文件夹,Google Drive网盘里的挂载的一个文件夹
LOCAL=''  #挂载地址,VPS本地挂载目录

然后执行

#Debian/Ubuntu 系统
apt-get install sudo -y

#设置自启
mv rcloned /etc/init.d/rcloned
chmod +x /etc/init.d/rcloned
update-rc.d -f rcloned defaults
bash /etc/init.d/rcloned start

检测信息显示rclone启动成功即可。

这里就结束了rclone的按照了,剩下的我们就开始自动备份数据

创建:web脚本.sh

#!/bin/bash
#你要修改的地方从这里开始
WEB_DATA=/home/wwwroot          #要备份的网站数据,如果是使用lnmp安装包,则默认这个为网站目录
#你要修改的地方从这里结束

#定义web数据的名字和web数据的名字
WebBakName=Web_$(date +%Y%m%d).tar.gz
OldWeb=Web_$(date -d -10day +"%Y%m%d").tar.gz

#删除Google Drive10天前的数据
rm -rf /root/gg-web-bak/Web_$(date -d -1day +"%Y%m%d").tar.gz
#进入本地目录
cd /root/webbak

#压缩网站数据
tar zcf /root/webbak/$WebBakName $WEB_DATA

#移动到Google Drive盘里面去
mv /root/webbak/$WebBakName /root/gg-web-bak/
echo “web备份结束”
chmod 777 /root/web-bak.sh

创建:mysql备份.sh

#!/bin/bash
#你要修改的地方从这里开始
MYSQL_USER=root                 #mysql用户名
MYSQL_PASS=xxxxxx               #mysql密码
#你要修改的地方从这里结束
#定义数据库的名字和旧数据库的名字
DataBakName=Data_$(date +"%Y%m%d").tar.gz
OldData=Data_$(date -d -2day +"%Y%m%d").tar.gz
#删除google drive3天前的数据
rm -rf /root/gg-web-bak/Data_$(date -d -1day +"%Y%m%d").tar.gz
#进入本地目录
cd /root/webbak
#导出数据库,一个数据库一个压缩文件
for db in `/usr/local/mysql/bin/mysql -u$MYSQL_USER -p$MYSQL_PASS -B -N -e 'SHOW DATABASES' | xargs`; do
    (/usr/local/mysql/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PASS ${db} | gzip -9 - > ${db}.sql.gz)
done
#压缩数据库文件为一个文件
tar zcf /root/webbak/$DataBakName /root/bak/*.sql.gz
rm -rf /root/webbak/*.sql.gz

#移动到google Drive
mv /root/webbak/$DataBakName /root/gg-web-bak/
echo “备份结束”
chmod 777 /root/mysql-bak.sh

然后把2个sh脚本放置crontab里面去,让他们每个星期甚至每天备份一次.毕竟是无限空间

0 4 * * 0 /root/mysql-bak.sh #每周日4点执行
0 1 * * 0 /root/web-bak.sh #每周日1点执行

 

声明:Sandy'Blog|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 利用Google Drive的无限网盘做数据定时备份


一流的吃貨,二流的廚藝,三流的技術,下流的人品