[rclone]를 사용한 클라우드 저장소(OneDrive) 동기화

■ 준비물
- Storage를 탑재한 Linux 머신에 rclone 설치
- 클라이언트 윈도우 PC에 rclone 설치

■ rclone 설치

curl -O https://downloads.rclone.org/rclone-current-linux-arm.zip
unzip rclone-current-linux-arm.zip
cp -fv rclone-*-linux-arm/rclone /usr/bin/
rm -rf rclone-*

윈도우는
https://rclone.org/downloads/ 에서 다운로드 받아 설치한다.

■ 리눅스 서버에서 rclone 설정

아래와 같은 과정으로 설정한다.
$ rclone config

n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> test-onedrive_com
Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
28 / Microsoft OneDrive
   \ (onedrive)
Storage> 28
Option client_id.
OAuth Client Id.
Leave blank normally.
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> [그냥 엔터]
Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own string value.
Press Enter for the default (global).
 1 / Microsoft Cloud Global
   \ (global)
 2 / Microsoft Cloud for US Government
   \ (us)
 3 / Microsoft Cloud Germany
   \ (de)
 4 / Azure and Office 365 operated by 21Vianet in China
   \ (cn)
region> 1
Edit advanced config?
y) Yes
n) No (default)
y/n> 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> y
2022/05/02 14:28:50 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=eZY-4uk4V93RKCMLeEq2jA
2022/05/02 14:28:50 NOTICE: Log in and authorize rclone for access
2022/05/02 14:28:50 NOTICE: Waiting for code...
2022/05/02 14:29:26 NOTICE: Got code
Option config_type.
Type of connection
Choose a number from below, or type in an existing string value.
Press Enter for the default (onedrive).
 1 / OneDrive Personal or Business
   \ (onedrive)
 2 / Root Sharepoint site
   \ (sharepoint)
   / Sharepoint site name or URL
 3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
   \ (url)
 4 / Search for a Sharepoint site
   \ (search)
 5 / Type in driveID (advanced)
   \ (driveid)
 6 / Type in SiteID (advanced)
   \ (siteid)
   / Sharepoint server-relative path (advanced)
 7 | E.g. /teams/hr
   \ (path)
config_type> 1
Drive OK?

Found drive "root" of type "business"
URL: https://unanu-my.sharepoint.com/personal/h1733_e365_fun/Documents

y) Yes (default)
n) No
y/n> y
--------------------
[test-onedrive_com]
type = onedrive
token = {"access_token":"eyJ0eXAiOi...ysh6uSQ","expiry":"2022-05-02T15:44:52.6385577+09:00"}
drive_id = b!b...UK9_HSJs-21ywO...i4_C7O
drive_type = business
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name                 Type
====                 ====
test-onedrive_com       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

■ 윈도우PC에서 rclone 리모트 인증

윈도우 커맨드 프롬트프에서 rclone & 웹브라우저를 사용해서 아래와 같이 인증 받는다.
rclone authorize "onedrive"


■ 리눅스 서버에서 rclone 인증키 입력

result> 여기에 윈도우PC에서 받은 키를 붙여 넣는다. {"access_token":"eyJ0edsfsaer4gfw34...wem4j0SZNqI3SAA","expiry":"2019-12-02T12:30:46.2077483+09:00"}
Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
 3 / Type in driveID
   \ "driveid"
 4 / Type in SiteID
   \ "siteid"
 5 / Search a Sharepoint site
   \ "search"
Your choice> 1
Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=b!kowefawefawefawefawefasdfq23fq23fq23fq23g54gw534rff4f4rgfrg3_HKH
Chose drive to use:> 0
Found drive 'root' of type 'business', URL: https://wenzhan1o-my.sharepoint.com/personal/myid_hotmail_com/Documents
Is that okay?
y) Yes
n) No
y/n>
y
--------------------
[onedrive]
type = onedrive
token = {"access_token":"eyJ0eXAieiwoef...pbHyowem4j0SZNqI3SAA","expiry":"2019-12-02T12:30:46.2077483+09:00"}
drive_id = b!kodQ3NkYXUCYrawefawefawefawefawefw4fu89w4hfwifh0ssQHRqznObX2_HKH
drive_type = business
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name                 Type
====                 ====
onedrive             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


■ rclone-sync.sh를 사용한 동기화

아래의 스크립트를 사용하여 동기화 한다.
이 스크립트는 중복 실행 방지 기능이 있으므로 cron 데몬에 10분 간격으로 실행되도록 해 놓으면 자동으로 동기화 할 수 있다.

#!/bin/bash
ver="10.00"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"  # get cur dir of this script
progName=$(basename -- "$0")
cd $DIR
echo "$progName $ver  written by Claude Pageau"
: '
 If lockCheckFile=True then script checks pi-timolo.sync file exists
 Otherwise no sync is attempted.  This can be useful for
 Low Bandwidth connections with low frequency of motion tracking events
'

#  Customize rclone sync variables Below
# ---------------------------------------

lockFileCheck=false       # true= Checks for pi-timolo.sync file. false = No Check (case sensitive)
rcloneName="onedrive"     # "rclone listremotes" 커맨드로 보이는 클라우드 스토리지 명
syncRoot="/usr"           # 로컬 루트 폴더 (chroot되는 폴더)
localDir="share/X11"            # 로컬 루트 폴더를 기준으로 한 로컬 폴더명
remoteDir="/X11/123/456"          # 원격지 폴더 (폴더가 없으면 생성된다)
rcloneParam="copy"        # 동기화 방법
                          # sync: 양방향 동기화
                          # copy: 로컬 스토리지→원격 스토리지로 복사
                          # move: 로컬 스토리지→원격 스토리지로 이동
# ---------------------------------------


# Display Users Settings
echo "----------- SETTINGS -------------

lockFileCheck : $lockFileCheck
rcloneName    : $rcloneName
syncRoot      : $syncRoot
localDir      : $localDir
remoteDir     : $remoteDir
rcloneParam   : $rcloneParam   (Options are sync, copy or move)

---------------------------------"

lockFilePath="/home/pi/pi-timolo/pi-timolo.sync"

cd $syncRoot   # Change to local rclone root folder
if pidof -o %PPID -x "$progName"; then
    echo "WARN  - $progName Already Running. Only One Allowed."
else
    if [ -f /usr/bin/rclone ]; then    #  Check if rclone installed
        rclone version   # Display rclone version
        if [ ! -d "$localDir" ] ; then   # Check if Local sync Folder Exists
           echo "---------------------------------------------------"
           echo "ERROR : localDir=$localDir Does Not Exist."
           echo "        Please Investigate Bye ..."
           exit 1
        fi
        /usr/bin/rclone listremotes | grep "$rcloneName"  # Check if remote storage name exists
        if [ $? == 0 ]; then    # Check if listremotes found anything
            if $lockFileCheck ; then
                if [ -f "$lockFilePath" ] ; then  # Check if sync lock file exists
                    echo "INFO  : Found Lock File $lockFilePath"
                    echo "        rclone $rcloneParam is Required."
                else
                    echo "INFO  : Lock File Not Found: $lockFilePath"
                    echo "        rclone $rcloneParam is Not Required."
                    echo "Exiting $progName ver $ver"
                    exit 0
                fi
            fi
            echo "INFO  : /usr/bin/rclone $rcloneParam -v $localDir $rcloneName:$remoteDir"
            echo "        One Moment Please ..."
            /usr/bin/rclone $rcloneParam -v $localDir $rcloneName:$remoteDir
            if [ ! $? -eq 0 ]; then
                echo "---------------------------------------------------"
                echo "ERROR : rclone $rcloneParam Failed."
                echo "        Review rclone %rcloneParam Output for Possible Cause."
            else
                echo "INFO  : rclone $rcloneParam Successful ..."
                if $lockFileCheck ; then
                    if [ -f "$lockFilePath" ] ; then
                        echo "INFO  : Delete File $lockFilePath"
                        rm -f $lockFilePath
                    fi
                fi
            fi
        else
            echo "---------------------------------------------------"
            echo "ERROR : rcloneName=$rcloneName Does not Exist"
            echo "INFO  : List Remote Storage Names that are Setup."
            echo "rclone listremotes"
            echo "-------------------"
            rclone listremotes
            echo "--------------------"
            echo "INFO  : If listremotes Listing is Empty, Read pi-timolo Wiki"
            echo "        How to Setup a Remote Storage Name."
        fi
    else
        echo "ERROR : /usr/bin/rclone Not Installed."
        echo "        You Must Install and Configure rclone"
        echo "        See pi-timolo Wiki for Details"
    fi
fi
echo "---------------------------------------------------"
echo "Exiting $progName ver $ver Bye ..."
위로 스크롤