1298 字
6 分钟
伪 NAS 利用 VPS 实现备份、下载、串流播放一条龙(下)
本片讲述挂载Google Drive扩展你的VPS硬盘并把Aria2下载后的文件自动上传到Google Drive中等玩法。
一、安装rclone
curl https://rclone.org/install.sh | sudo bash二、初始化配置
rclone config按照下面选择走:
Current remotes:
Name Type==== ====gdrive drive
e) Edit existing remoten) New remoted) Delete remoter) Rename remotec) Copy remotes) Set configuration passwordq) Quit confige/n/d/r/c/s/q> n 这里输入n,新建一个name> 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 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 输入11Google Application Client IdLeave blank normally.Enter a string value. Press Enter for the default ("").client_id> 直接回车Google Application Client SecretLeave 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> 1 输入1ID of the root folderLeave 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 pathLeave 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) Yesn) Noy/n> n 输入nRemote configUse auto config? * Say Y if not sure * Say N if you are working on a remote or headless machine or Y didn't worky) Yesn) Noy/n> n 输入nIf your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=xxxxxxxxxxxxxxxxxxxxxxxxxx这里会给你一个链接,打开后用你的Google Drive授权就会得到下面一串代码填入即可Log in and authorize rclone for accessEnter verification code> 4/rQDL5M58Dv_MjwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxConfigure this as a team drive?y) Yesn) Noy/n> y 输入yFetching team drive list...No team drives found in your account--------------------[test]type = drivescope = drivetoken = {"access_token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","token_type":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","expiry":"2018-12-08T14:45:49.328865767+08:00"}--------------------y) Yes this is OKe) Edit this remoted) Delete this remotey/e/d> y 输入yCurrent remotes:
Name Type==== ====gdrive drivetest drive
e) Edit existing remoten) New remoted) Delete remoter) Rename remotec) Copy remotes) Set configuration passwordq) Quit confige/n/d/r/c/s/q> q 输入q退出三、挂载
1.手动挂载
# 新建本地文件夹,路径自己定,即下面的LocalFoldermkdir /root/GoogleDrive# 挂载为磁盘,下面的DriveName、Folder、LocalFolder参数根据说明自行替换rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 0002.开机自动挂载
新建 /lib/systemd/system/rclone.service
nano /lib/systemd/system/rclone.service写入,注意 gdrive后最好跟盘内的文件夹
[Unit]Description = rclone
[Service]User = rootExecStart = /usr/bin/rclone mount gdrive: /mnt/gdrive --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000Restart = on-abort
[Install]WantedBy = multi-user.target重载daemon
systemctl daemon-reload启动Rclone
systemctl start rclone设置开机自启
systemctl enable rclone二、Aria2下载完后自动上传
这里用到了Aria2的一个on-download-complete功能,这个功能可以做很多事情,这里只用到了自动上传
新建脚本文件gdupload.sh,并复制下面代码:
# !/bin/bashpath="$3" #取原始路径downloadpath='/data/wwwroot/dd.test.com/download' #下载目录rclone='/data/wwwroot/dd.test.com/cloud' #rclone挂载的目录
if [ $2 -eq 0 ] #下载文件为0跳出脚本 then exit 0fi
while true; do #提取下载文件根路径 filepath=$path path=${path%/*}; if [ "$path" = "$downloadpath" ] && [ $2 -eq 1 ] then rclone=${filepath/#$downloadpath/$rclone} #替换路径 cp -R "${filepath}" "${rclone}" exit 0 elif [ "$path" = "$downloadpath" ] #文件夹 then cp -R "${filepath}" "${rclone}"/ exit 0 fidone注意:本脚本上传下载文件后并没有删除原文件,因为rclone在上传多文件时,可能会丢文件,所以这个脚本并没有删除原文件 如果想要自动删除的话,可以改成下面这样子:
.....................前面不变 rclone=${filepath/#$downloadpath/$rclone} #替换路径 mv -f "${filepath}" "${rclone}" exit 0 elif [ "$path" = "$downloadpath" ] #文件夹 then mv -f "${filepath}" "${rclone}"/ rm -rf "${filepath}" exit 0 fidone修改Aria2配置文件
首先授权chmod +x gdupload.sh然后再到Aria2配置文件中加上一行on-download-complete=/root/gdupload.sh即可,后面为脚本的路径。
三、Nextcloud添加外置存储
由于我们已经把Google Drive挂载到本地了,所以我们直接添加外置存储即可
首先在Nextcloud中安装外部存储插件
选择应用-External storage support-启用即可
接着设置-外部存储按图所示添加即可

伪 NAS 利用 VPS 实现备份、下载、串流播放一条龙(下)
https://lwnlh.com/posts/6_fake_nas_use_vps_to_realize_backup_download_and_streaming_playback_3/