LoginSignup
2
2

More than 5 years have passed since last update.

rcloneを使ってみた(GoogleDrive連携 on CentOS)

Posted at

前回の続編。サーバーサイドです。

其の二:CentOSサーバーへ転送

1.準備

インストール、confファイル作成までしておく。
https://rclone.org/install/#linux-installation-from-precompiled-binary

2.同期

rclone rsync を使ってGoogleDrive→サーバーへファイル同期。
cronで3分ごとに実行してみる。
※プロセスが重複起動しないようにした。

*/3 * * * * if [ "$$" = "`pgrep -fo /usr/sbin/rclone`" ]; then /usr/sbin/rclone sync DRIVE:/hogefrom /var/www/html/hogeto/ >> /var/log/rclone.log 2>&1; fi
3.PHPバッチで処理

同期したファイルをPHPアプリケーションでごにょごにょする必要があったため、色々処理。
そして処理済みのファイルはGoogleDrive上も別フォルダに移動させたい。
そこでPHP上からrcloneを呼ぶことにする。

まず、Apacheから起動できるようにvisudoを変更。
(参考)http://www.dahlbeck.net/works/posix/50-2014012304

Defaults   requiretty
# 追記
Defaults:apache   !requiretty
...
Defaults   !visiblepw
# 追記
Defaults:apache   visiblepw
...
root    ALL=(ALL)       ALL
# 追記
apache  ALL=(ALL)  NOPASSWD: ALL

そしてPHPからrcloneを実行。

rclone.php
$src_path = 'DRIVE:/hogefrom/';
$dest_path = 'DRIVE:/hogeto/';
$rc_conf = '/home/hogehoge/.rclone.conf';
...
$file = ループ回しながらファイル名を取得;
$subdir = ファイル名で階層をつくったり;
exec('sudo /usr/sbin/rclone --config="' . $rc_conf . '" move ' . $src_path . $file . ' ' . $dest_path . $subdir . ' 2>&1', $output, $res);

こんな感じで、GoogleDrive上のファイルを取り込み、何らかの処理後、GoogleDrive上のファイルも移動するというバッチができました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2