Raspberry Pi4에서 Google Drive sync

워누·2022년 11월 8일
0

Raspberry Pi4

목록 보기
2/2
post-thumbnail

Overview

오늘은 Raspberry pi4에서 google drive sync 기능을 활용 해보려고 합니다.
평소에 필요한 configuration 정보나 codeserver 정보들을 백업하기 위함입니다.

Docker Container

Ubuntu:22.04 image를 통해 container를 생성 하였습니다.

$ docker run -it --name google_drive -v ${MOUNT_PATH}:/google_drive ubuntu:22.04 /bin/bash

RCLONE

RCLONE 많은 원격 스토리지 서비스와 파일 및 디렉토리를 동기화하는 CLI 프로그램입니다. 이 유틸리티를 사용하여 Rasberry pi 컴퓨터와 원격 클라우드 스토리지 서비스 간의 폴더/파일을 업데이트 및 관리할 수 있습니다.

그리고 rpi4에서 RCLONE을 사용할 수 있는 util이 open source로 존재 합니다.

RCLONE-RPI4-UTILS

RCLONE 설치

$ wget https://raw.github.com/pageauc/rclone4pi/master/rclone-install.sh
$ chmod +x rclone-install.sh

설치 스크립트 치환

기본 ubuntu image로 container를 생성하면 sudo가 없어서 파일에서 sudo 사용내용을 치환합니다.

$ sed -i 's/sudo //g' rclone-install.sh

설치 스크립트 실행

설치를 하면 아래와 같이 기본 가이드 내용이 출력됩니다.

$ ./rclone-install.sh
.
.
.
-------------------------------------------------------------------------------
                 INSTRUCTIONS Google Drive Example

1 You will be required to have a login account on the remote storage service
  Open putty SSH login session to RPI and execute command below

  rclone config

  Follow rclone prompts. For more Details See
  https://github.com/pageauc/rclone4pi/wiki/Home
2 At name> prompt specify a reference name  eg gdmedia
3 At storage> prompt Enter a remote storage number from List
4 Select Auto Config, At Link: prompt, left click
  and highlight rclone url link (do not hit enter)
5 on computer web browser url bar right click paste and go.
6 On computer web browser security page, Confirm access.
7 Copy web browser access security token and paste
  into RPI SSH session rclone prompt. Enter to accept
8 To test remote service access. Execute the following where
  gdmedia is the name you gave your remote service

  rclone ls gdmedia:/

Example sync command make source identical to destination

rclone sync -v /home/pi/rpi-sync gdmedia:/rpi-sync

To upgrade

  cd rpi-sync
  ./rclone-install.sh upgrade

For more Details See https://github.com/pageauc/rclone4pi/wiki/Home
Bye

RCLONE 설정

처음 사용이기 때문에 rclone.conf 파일을 생성합니다. n
이름은 google_drive로 설정하였습니다. 그리고 Google Drive를 사용할 것이기 때문에 18번을 선택 합니다.

$ rclone config
2022/11/08 00:05:26 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
Enter name for new remote.
name> google_drive
Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
.
.
17 / Google Cloud Storage (this is not Google Drive)
   \ (google cloud storage)
18 / Google Drive
   \ (drive)
19 / Google Photos
   \ (google photos)
20 / HTTP
   \ (http)
21 / Hadoop distributed file system
   \ (hdfs)
.
.
Storage> 18

cline_id는 생략하였습니다. client_id를 empty 값으로 두면 성능이 느린 내부키를 사용한다고 합니다.
client_secret 값도 일반적으론 비워둔다 하니 비워둡니다.

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 value. Press Enter to leave empty.
client_id> 
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> 

Permission 관련해서는 1번 전체권한을 주도록 합니다.

Option scope.
Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value.
Press Enter to leave empty.
 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

그리고 중요한 것은 현재작업을 remote 환경에서 작업하기 때문에 아래와 같은 내용 출력시 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

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 "drive" "blahblah"
Then paste the result.
Enter a value.

위와 같은 내용이 출력되면 GUI가 작동되는 host pc에서 출력된 command를 입력합니다.

$ wget https://downloads.rclone.org/rclone-current-linux-amd64.deb
$ sudo dpkg rclone-current-linux-amd64.deb
$ rclone authorize "drive" "blahblah"
<5>NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=Z1pVIzk10fQDKyyzqrB-3w
<5>NOTICE: Log in and authorize rclone for access
<5>NOTICE: Waiting for code...
<5>NOTICE: Got code
Paste the following into your remote machine --->
blahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKeyblahblahKey
<---End paste

출력된 key를 rpi4에서 입력합니다. Copy & Paste

Shared Drive를 사용하려면 y 사용 안하려면 n을 입력합니다.

Configure this as a Shared Drive (Team Drive)?

y) Yes
n) No (default)
y/n> n

RCLONE Sync 사용

RCLONE-RPI4-UTILS을 통해 RCLONE을 설치할때 sync utils가 제공됩니다.

$ ls rpi-sync
Readme.md  rclone-install.sh  rclone-sync.sh

Google drive용 sync script를 생성하겠습니다.

$ cp rclone-sync.sh google-drive-sync.sh
$ cat google-drive-sync.sh
.
.
#  Customize rclone sync variables Below
# ---------------------------------------

lockFileCheck=false      # true= Checks for pi-timolo.sync file. false = No Check (case sensitive)
rcloneName="gdmedia"     # Name of Remote Storage Service
syncRoot="/home/pi"      # Root Folder to Start
localDir="rpi-sync"      # Source Folder on Local
remoteDir="syncdemo"     # Destination Folder on Remote
rcloneParam="sync"       # rclone option to perform  Eg  sync, copy, move
                         # IMPORTANT: sync will make remoteDir identical to localDir
                         # so remoteDir Files that do not exist on localDir will be Deleted.
# ---------------------------------------
.
.

위 항목의 내용을 자신이 설정에 맞게 변경합니다.

$ vim google-drive-sync.sh
#  Customize rclone sync variables Below
# ---------------------------------------

lockFileCheck=false      # true= Checks for pi-timolo.sync file. false = No Check (case sensitive)
rcloneName="google_drive"     # Name of Remote Storage Service
syncRoot="/google_drive"      # Root Folder to Start
localDir="code-server"      # Source Folder on Local
remoteDir="rpi4/code-server"     # Destination Folder on Remote
rcloneParam="sync"       # rclone option to perform  Eg  sync, copy, move
                         # IMPORTANT: sync will make remoteDir identical to localDir
                         # so remoteDir Files that do not exist on localDir will be Deleted.
# ---------------------------------------

이제 실행하면 아래와같이 성공 메시지가 출력됩니다.

$ ./google-drive-sync.sh 
google-drive-sync.sh 10.00  written by Claude Pageau
----------- SETTINGS -------------

lockFileCheck : false
rcloneName    : google-drive
syncRoot      : /google_drive
localDir      : code-server
remoteDir     : rpi4/code-server
rcloneParam   : sync   (Options are sync, copy or move)

---------------------------------
rclone v1.60.0
- os/version: ubuntu 22.04 (64 bit)
- os/kernel: 5.15.0-1017-raspi (aarch64)
- os/type: linux
- os/arch: arm
- go/version: go1.19.2
- go/linking: static
- go/tags: none
google-drive:
INFO  : /usr/bin/rclone sync -v code-server google-drive:rpi4/code-server
        One Moment Please ...
2022/11/08 01:57:56 INFO  : There was nothing to transfer
2022/11/08 01:57:56 INFO  : 
Transferred:   	          0 B / 0 B, -, 0 B/s, ETA -
Checks:                 1 / 1, 100%
Elapsed time:         1.6s

INFO  : rclone sync Successful ...
---------------------------------------------------
Exiting google-drive-sync.sh ver 10.00
Bye ...

0개의 댓글