Server

rclone 으로 google drive 로컬 드라이브 만들기 (리눅스)

HoodieJun 2021. 4. 18. 12:34

1. 사용 동기
구글 서비스들이 정말 다양하고 안드로이드 폰을 사용하려면 구글 계정이 꼭 필요합니다. 그래서 1인당 최소 구글 계정 하나는 갖고 있을 텐데요. 구글에서 계정만 만들면 기본으로 구글 드라이브에서 사용할 수 있는 15GB 용량을 제공합니다.
원드라이브는 5GB, 드롭박스는 2GB 인 것에 비하면 정말 많은 양을 줍니다. 실제로 문서나 간단한 파일 정도만 저장한다면 큰 문제없이 사용할 수 있습니다.
하지만 이렇게 유용한 구글 드라이브를 사용하려면 브라우저에서 구글에 접속하여 드라이브 페이지에 또 들어가야 합니다.
그래서 저는 파일 탐색기에서 바로 구글 드라이브에 접속하면 편하지 않을까 하는 생각에 이 방법을 사용하게 되었습니다.

2. 필요성
윈도우를 사용하시는 분들 중에 구글 계정 하나만 쓰신다면 굳이 이 방법을 사용하는 것보다 구글에서 제공하는 구글 드라이브 스트림을 사용하시면 될 것 같습니다. 그리고 리눅스에서 gnome 데스크톱 환경을 사용하시는 분들은 online account 설정에서 로그인하시면 nautilus로 바로 접근할 수 있습니다.
그 외에 구글 드라이브를 여러 개 사용하시거나 용량이 큰 파일을 스트리밍 하는데 끊김 없이 보고 싶으신 분, 혹은 리눅스에서 gnome이 아닌 다른 데스크톱 환경을 사용하시는 분은 이 방법을 사용해보시면 좋을 것 같습니다.
윈도우 버전과 리눅스 버전으로 나눠서 설명하겠습니다.

1. rclone 설치

$ curl https://rclone.org/install.sh | sudo bash
$ rclone --version
rclone v1.54.0
- os/arch: linux/amd64
- go version: go1.15.7

위의 명령어 한 줄로 설치가 끝납니다.

2. rclone.conf 파일 생성
드라이브 설정 파일을 생성하는 마법사를 실행합니다.

$ rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n

name> gdrive
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"
(...)
15 / Google Drive
   \ "drive"
(...)
40 / http Connection
   \ "http"
41 / premiumize.me
   \ "premiumizeme"
42 / seafile
   \ "seafile"
Storage> drive

구글 드라이브는 "drive" 이므로 drive 입력해주세요. 원하는 파일 시스템에 맞게 선택해주세요.

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> 

OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> 

API를 이용하실 분들은 http://cloud.google.com에 접속하셔서 id 랑 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> drive

읽고 쓰기, 실행하기 다 허용할 것이기 때문에 "drive" 입력하시면 됩니다.

scope> drive
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> 
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/n)
y) Yes
n) No (default)
y/n> 
Remote config
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> y

auto config 설정할 때까지 Enter로 넘어갑니다. auto config에서 개인용 컴퓨터를 사용하시는 경우 "y"를 입력하시고 server나 원격 접속을 하고 계신 경우는 "n"을 입력하세요.
y를 선택하신 분들은 자동 브라우저로 연결되고 로그인하시면 됩니다. n을 선택하신 분들은 안내받은 링크를 복사해서 진행해주세요.

Configure this as a team drive?
y) Yes
n) No (default)
y/n> n
--------------------
[gdrive]
type = drive
scope = drive
token = {"access_token": 토큰값"}
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

팀 드라이브 사용하지 않으므로 넘겨주시고 마지막에 잘 설정되었나 확인한 후 "y"를 입력하세요. (공유 드라이브를 설정하고 싶으시다면 team drive에서 y를 선택하세요.)

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> 

gdrive 가 잘 설정되어있는 것을 확인할 수 있습니다. "q"로 빠져나와 주세요.

3.  접속 잘 되는지 check 하기

$ rclone lsd gdrive:
          -1 2020-09-16 09:39:52        -1 Notability
          -1 2020-12-15 10:40:41        -1 Photo

위의 명령어를 입력하시면 현재 존재하는 폴더가 나타납니다.

4. gdrive mount 시키기
설정한 구글 드라이브를 외장하드처럼 사용하기 위해 파일 매니저(탐색기)에서도 바로 접속할 수 있도록 마운트 시켜주겠습니다.
* 마운트 시킬 빈 폴더 만들어주기

$ sudo mkdir /mnt/gdrive
$ sudo chown user:user /mnt/gdrive

"user"를 본인 컴퓨터 계정으로 바꿔주세요.
* root 유저가 아니어도 마운트 할 수 있도록 설정하기

$ sudo vim /etc/fuse.conf
# /etc/fuse.conf - Configuration file for Filesystem in Userspace (FUSE)

# Set the maximum number of FUSE mounts allowed to non-root users.
# The default is 1000.
#mount_max = 1000

# Allow non-root users to specify the allow_other or allow_root mount options.
user_allow_other

마지막에 "user_allow_other" 앞에 코멘트 처리된 "#"을 제거해주세요.
* 컴퓨터 부팅할 때마다 자동으로 마운트 시키도록 서비스 등록하기

$ sudo vim /etc/systemd/system/rclone-gdrive.service

[Unit]
Description=rclone mount for google drive
Wants=network-online.target
After=network-online.target

[Service]
Type=notify
KillMode=none
RestartSec=5
ExecStart=/usr/bin/rclone mount gdrive:/ /mnt/gdrive \
--config /home/user/.config/rclone/rclone.conf \
--umask 002 \
--allow-other \
--allow-non-empty \
--fast-list \
--drive-skip-gdocs \
--poll-interval=1m \
--buffer-size=32M \
--vfs-read-chunk-size=32M \
--vfs-read-chunk-size-limit 2048M \
--vfs-cache-mode writes \
--dir-cache-time=1m
ExecStop=/usr/bin/fusermount -uz /mnt/gdrive
Restart=on-failure
User=user
Group=user

[Install]
WantedBy=multi-user.target

설정해보니 이 정도 옵션이 사용하기 좋았습니다. 마음에 드시는 옵션이 있으면 수정해서 사용하시고 저와 같은 방법으로 하실 분은 복사해서 붙여 넣고 "user" 부분만 본인 컴퓨터 계정 이름으로 바꾸시면 됩니다.

5. 서비스 시작해주기

$ sudo systemctl start rclone-gdrive.service
$ systemctl status rclone-gdrive.service
● rclone-gdrive.service - rclone mount for google drive
     Loaded: loaded (/etc/systemd/system/rclone-gdrive.service; disabled; vendor preset: ena>
     Active: active (running) since Thu 2021-02-11 16:33:52 KST; 7s ago

초록불과 함께 "active (running)"이라는 문구가 뜨면 정상적으로 작동하고 있는 것입니다.
혹시 빨간불이나 회색 불이 뜨고 작동하지 않는다면 위에서 작성한 "rclone-gdrive.service" 파일에서 잘못 작성한 부분이 없는지 확인해주세요.
제대로 작동하면 서비스 자동시작을 등록해주겠습니다.

$ sudo systemctl enable rclone-gdrive.service 
Created symlink /etc/systemd/system/multi-user.target.wants/rclone-gdrive.service → /etc/systemd/system/rclone-gdrive.service.

앞으로 컴퓨터를 부팅할 때마다 설정해놓은 구글 드라이브를 자동을 마운트 하는 걸 볼 수 있습니다.
내 컴퓨터에서 외장하드 접속하듯이 편하게 클라우드 서비스를 사용해보세요!
다음번엔 윈도우에서 설정하고 마운트 시키는 방법을 소개하겠습니다.

감사합니다!