BLOGTIMES
2023/04/09

rclone で OneDrive の内容をサーバにバックアップ

  onedrive 
このエントリーをはてなブックマークに追加

大事なファイルを OneDrive に置いといて吹っ飛ばしたという話を聞くので、Rclone を使って日次でバックアップを取るようにしてみました。
最近のサービスは OAuth2 なのでちょっと設定が面倒ですが、一度設定してしまえば rsync とほぼ同じように運用できますね。

初期設定

初めに rclone の認証を設定します。
ちょっとわかり辛いのですが、> の後が入力になります。

ここでは設定名を MyOneDrive にしていますが、好きな名前をつけて大丈夫です。
また、表示が多少変化しますが、OneDrive Business でも Personal でも同じ手順で実行できます。

$ rclone config 2023/04/09 16:31:07 NOTICE: Config file "/home/user/.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 name> MyOneDrive Option Storage. Type of storage to configure. Choose a number from below, or type in your own value. 1 / 1Fichier \ (fichier) ・・・省略・・・ 45 / seafile \ (seafile) Storage> onedrive 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) ・・・省略・・・ 4 / Azure and Office 365 operated by 21Vianet in China \ (cn) region> global 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 2023/04/09 16:31:41 NOTICE: If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=3Wto-UoM6Wnas_TDPilwzg ### ここで、rcloneのインストール先がサーバなどでブラウザが開けない場合、以下のコマンドを使ってローカルにログイン画面をトンネルすることで手元のマシンのブラウザから認証を行うことができる ### ssh -L localhost:53682:localhost:53682 username@remote_server ### ブラウザでの認証が終わると以下に進む 2023/04/09 16:31:41 NOTICE: Log in and authorize rclone for access 2023/04/09 16:31:41 NOTICE: Waiting for code... 2023/04/09 16:33:03 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) ・・・省略・・・ / Sharepoint server-relative path (advanced) 7 | E.g. /teams/hr \ (path) config_type> onedrive Drive OK? Found drive "root" of type "business" URL: https://example.sharepoint.com/personal/user_example_com/Documents y) Yes (default) n) No y/n> y -------------------- [MyOneDrive] type = onedrive token = {"access_token":"####################################################","expiry":"2023-04-09T17:35:02.176261971+09:00"} drive_id = ################################################################## 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 ==== ==== MyOneDrive 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

同期してみる

あとは以下のコマンドでバックアップ先(以下では /path/to/backup)にコピーなり、同期なりをすればOKです。

rclone copy MyOneDrive: /path/to/backup --dry-run # 全てコピー rclone sync MyOneDrive: /path/to/backup --dry-run # 同期

同期の場合は、リモートでファイルが削除されている時、ローカルのファイルを削除してしまうので注意が必要です。
いきなり実行すると予想外の結果を招く可能性があるので、初めは --dry-run をつけて同期の方向をしっかり確認し、その後で --dry-run を外すと良いと思います。

cron 等で定時実行する場合には、以下のように --config で設定ファイルを直接参照しておくと安心です。

rclone sync MyOneDrive: /path/to/backup --dry-run --config="/path/to/rclone.conf"

僕のバックアップディレクトリは zfs で構成されていて、日次でスナップショット取ってあるので、たとえファイルが消えても一定期間であれば問題なくリストアできます。

参考


    トラックバックについて
    Trackback URL:
    お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
    このエントリへのTrackbackにはこのURLが必要です→https://blog.cles.jp/item/13921
    Trackbacks
    このエントリにトラックバックはありません
    Comments
    愛のあるツッコミをお気軽にどうぞ。[policy]
    古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
    コメントはありません
    Comments Form

    OpenID を使ってログインすることができます。

    Identity URL: Yahoo! JAPAN IDでログイン