2023/04/26

LinuxでOneDriveと同期

バックアップしたデータをローカルにおいておくのも不安だな、とふと思ったので、M365 familyを購入したことでOneDriveが1TBあるので、OneDriveにバックアップデータを同期することにした。 


が、Gentoo公式リポジトリには、OneDrive関連のツールがなかった。 

# emerge -s onedrive
[ Results for search key : onedrive ]
Searching...

[ Applications found : 0 ]


ということで、3rd partyのリポジトリ(overlay)で探したらみつかったので、それを使う。。

https://gpo.zugaina.org/net-misc/onedrive



dlangというoverlayなので、overlayを登録。(この後、dmdのインストールでハマり、それは別記事にしたので、overlayの登録はそちらを参照)

# layman -a dlang

 * Adding overlay...
 * Overlay "dlang" is not official. Continue installing? [y/n]: y
 * Running Git... # ( cd /var/lib/layman  && /usr/bin/git clone https://github.com/gentoo/dlang.git /var/lib/layman/dlang )
Cloning into '/var/lib/layman/dlang'...
remote: Enumerating objects: 5285, done.
remote: Counting objects: 100% (514/514), done.
remote: Compressing objects: 100% (212/212), done.
remote: Total 5285 (delta 288), reused 501 (delta 280), pack-reused 4771
Receiving objects: 100% (5285/5285), 1.14 MiB | 6.39 MiB/s, done.
Resolving deltas: 100% (3108/3108), done.
 * Running Git... # ( cd /var/lib/layman/dlang  && /usr/bin/git config user.name "layman" )
 * Running Git... # ( cd /var/lib/layman/dlang  && /usr/bin/git config user.email "layman@localhost" )
 * Successfully added overlay(s) dlang.


OneDriveのクライアントが見つかるようになった。

# emerge -s onedrive

Performing Global Updates
(Could take a couple of minutes if you have a lot of binary packages.)
  .='update pass'  *='binary update'  #='/var/db update'  @='/var/db move'
  s='/var/db SLOT move'  %='binary move'  S='binary SLOT move'
  p='update /etc/portage/package.*'
/var/lib/layman/dlang/profiles/updates/2Q-2016..


[ Results for search key : onedrive ]
Searching...

*  net-misc/onedrive [ Masked ]
      Latest version available: 2.4.23
      Latest version installed: [ Not Installed ]
      Size of files: 1206 KiB
      Homepage:      https://abraunegg.github.io/
      Description:   Free Client for OneDrive on Linux
      License:       GPL-3

[ Applications found : 1 ]




Maskされているので、/etc/portage/package.accept_keywordsに「net-misc/onedrive ~amd64」を追加する。

# emerge -uDNtpv onedrive

These are the packages that would be merged, in reverse order:

Calculating dependencies -

!!! Problem resolving dependencies for net-misc/onedrive                                                                                                                                                                       ... done!
Dependency resolution took 0.82 s.


!!! The ebuild selected to satisfy "onedrive" has unmet requirements.
- net-misc/onedrive-2.4.23::dlang USE="-debug -dmd-2_087 -dmd-2_088 -dmd-2_089 -dmd-2_090 -dmd-2_091 -dmd-2_092 -dmd-2_093 -dmd-2_094 -dmd-2_095 -dmd-2_096 -dmd-2_097 -dmd-2_098 -dmd-2_099 -ldc2-1_29 -ldc2-1_30 -libnotify"

  The following REQUIRED_USE flag constraints are unsatisfied:
    exactly-one-of ( dmd-2_089 dmd-2_088 dmd-2_087 dmd-2_098 dmd-2_099 dmd-2_090 dmd-2_091 dmd-2_092 dmd-2_093 dmd-2_094 dmd-2_095 dmd-2_096 dmd-2_097 ldc2-1_29 ldc2-1_30 )


dmdとかldcとかで始まる何かのUSEフラグ(D言語のコンパイラだと後でわかった)を追加しないといけない。そこで、/etc/portage/package.useに「net-misc/onedrive dmd-2_099」を追加

# emerge -uDNtpv onedrive

These are the packages that would be merged, in reverse order:

Calculating dependencies... done!
Dependency resolution took 5.83 s.

[ebuild  N    ~] net-misc/onedrive-2.4.23::dlang  USE="dmd-2_099 -debug -dmd-2_087 -dmd-2_088 -dmd-2_089 -dmd-2_090 -dmd-2_091 -dmd-2_092 -dmd-2_093 -dmd-2_094 -dmd-2_095 -dmd-2_096 -dmd-2_097 -dmd-2_098 -ldc2-1_29 -ldc2-1_30 -libnotify" 0 KiB
[ebuild  N     ]  dev-lang/dmd-2.099.1:2.099::dlang  USE="selfhost -dmd-2_076 -dmd-2_077 -dmd-2_078 -dmd-2_079 -dmd-2_080 -dmd-2_082 -dmd-2_084 -dmd-2_085 -dmd-2_086 -dmd-2_087 -dmd-2_088 -dmd-2_089 -dmd-2_090 -dmd-2_091 -dmd-2_092 -dmd-2_093 -dmd-2_094 -dmd-2_095 -dmd-2_096 -dmd-2_097 -dmd-2_098 -dmd-2_099 -doc -examples -gdc-11_2_1 -gdc-11_3_0 -ldc2-1_29 -ldc2-1_30 -static-libs -tools" 21,335 KiB
[ebuild  N     ]   app-eselect/eselect-dlang-20190608::dlang  0 KiB

Total: 3 packages (3 new), Size of downloads: 21,335 KiB


# emerge -uDN onedrive


で、dmdのインストールも行われるはずだが、コケてしまい、試行錯誤した。最終的には、別記事で記載した通り導入ができ、onedriveも導入できた。


# emerge -uDN onedrive

 <中略>
 * Messages for package net-misc/onedrive-2.4.23:

 * OneDrive Free Client needs to be authorized to access your data before the
 * first use. To do so, run onedrive in a terminal for the user in question and
 * follow the steps on screen.
 *
 * When upgrading from 2.3 you are required to reauthorise your client.
 * This is due to changing the client identifier to assist with resolving
 * the correct handling of 429 error responses (activityLimitReached)

 * GNU info directory index is up-to-date.


このメッセージにあるように、認証を通してやる必要がある。onedriveコマンドを実行すると、URLが表示されるので、それをブラウザで開く


$ onedrive
Configuring Global Azure AD Endpoints
Authorize this app visiting:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&scope=Files.ReadWrite%20Files.ReadWrite.All%20Sites.ReadWrite.All%20offline_access&response_type=code&prompt=login&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient

Enter the response uri: 


ユーザIDの入力、パスワードの入力、(必要であれば)二段階認証、権限付与の承認、をやり通すと、真っ白な画面になる。




この際のURL全体(URLのパラメータ部分だけではないので要注意)を、入力待ちのonedriveコマンドに貼り付ける


$ onedrive
Configuring Global Azure AD Endpoints
Authorize this app visiting:

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&scope=Files.ReadWrite%20Files.ReadWrite.All%20Sites.ReadWrite.All%20offline_access&response_type=code&prompt=login&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient

Enter the response uri: https://login.microsoftonline.com/common/oauth2/nativeclient?code=xxxxxxxxxxxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Application has been successfully authorised, however no additional command switches were provided.

Please use 'onedrive --help' for further assistance in regards to running this application.




onedrive --display-config で設定が見える。使い方の詳細は、こちら。https://github.com/abraunegg/onedrive.git


まず、configファイルを作りたいので、ダウンロード


$ wget https://raw.githubusercontent.com/abraunegg/onedrive/master/config -O ~/.config/onedrive/config


少し編集して、こんな感じにした。ログは/var/log/onedrive/に吐き出されるので、実行権限がrootでない場合はエラーが出る。そこで、onedriveというグループを作成し、そこにonedriveコマンドを実行するユーザを入れた。そして、/var/log/onedriveの所有グループをonedriveグループにした上で、書き込み権限を与えてやることにした。

$ cat ~/.config/onedrive/config
# Configuration for OneDrive Linux Client
# This file contains the list of supported configuration fields
# with their default values.
# All values need to be enclosed in quotes
# When changing a config option below, remove the '#' from the start of the line
# For explanations of all config options below see docs/USAGE.md or the man page.
#
# sync_dir = "~/OneDrive"
sync_dir = "/data2/OneDrive"
# skip_file = "~*|.~*|*.tmp"
# monitor_interval = "300"
# skip_dir = ""
# log_dir = "/var/log/onedrive/"
# drive_id = ""
# upload_only = "false"
upload_only = "true"
# check_nomount = "false"
# check_nosync = "false"
# download_only = "false"
# disable_notifications = "false"
# disable_upload_validation = "false"
enable_logging = "true"
# force_http_11 = "false"
# local_first = "false"
# no_remote_delete = "false"
# skip_symlinks = "false"
# debug_https = "false"
# skip_dotfiles = "false"
# skip_size = "1000"
# dry_run = "false"
# min_notify_changes = "5"
# monitor_log_frequency = "6"
# monitor_fullscan_frequency = "12"
# sync_root_files = "false"
# classify_as_big_delete = "1000"
# user_agent = ""
# remove_source_files = "false"
# skip_dir_strict_match = "false"
# application_id = ""
# resync = "false"
# resync_auth = "false"
# bypass_data_preservation = "false"
# azure_ad_endpoint = ""
# azure_tenant_id = "common"
# sync_business_shared_folders = "false"
# sync_dir_permissions = "700"
# sync_file_permissions = "600"
# rate_limit = "131072"
# operation_timeout = "3600"
# webhook_enabled = "false"
# webhook_public_url = ""
# webhook_listening_host = ""
# webhook_listening_port = "8888"
# webhook_expiration_interval = "86400"
# webhook_renewal_interval = "43200"
# space_reservation = "50"
# display_running_config = "false"
# read_only_auth_scope = "false"
# cleanup_local_files = "false"


このまま実行すると、OneDriveに入っている全フォルダを同期してしまうので、バックアップデータを保存するフォルダだけ同期する。そのためには、sync_listファイルを作成する。この中に、同期したいフォルダをOneDriveのトップフォルダからの相対パスで記入する。行頭に「/」を入れていないと、OneDriveフォルダの中で、同じフォルダ名があれば、そこを同期してしまうので注意。

$ cat ~/.config/onedrive/sync_list
/Home_Server/edo/


onedriveコマンドの実行はこんな感じ。--upload-onlyはconfigファイルに記入しているので不要ではあるが、サーバからOneDriveへのアップロードのみ(及び、サーバにあったファイルが消された場合は、OneDrive上からも削除)にするために念のために指定している。


$ onedrive --synchronize --upload-only


これをサーバのバックアップ用スクリプトの実行後に実施することにした。また、sambaのデータのバックアップも合わせて実施することにした。バックアップ用のパーティションはLVMでストライピング(RAID-0)しており、sambaのデータはRAID-1の別パーティションにあるので、rsyncでバックアップ用パーティションにコピーさせることにした。


#!/bin/bash
/etc/cron.hourly/modules/hourly_backup.sh
rsync --delete-after -vas /data/samba1/ /data2/OneDrive/Home_Server/edo/samba/ > /dev/null 2>&1
sudo -u my_name onedrive -- --synchronize --upload-only > /dev/null 2>&1


本来、onedriveコマンドは、--monitoringでディレクトリの変更監視ができるのだが、パッケージに同梱されていた/etc/init.d/onedriveスクリプトが上手く動かないので、(それを直すべきではあるが、ちょっと時間がかかりそうだったので)とりあえず上記のスクリプトを1時間毎に回すことで問題回避した。


 



0 件のコメント:

コメントを投稿