さくらのレンタルサーバにpipとawsのコマンドを使えるようにしたい

さくらのレンタルサーバーは安くて信頼性も高く、SSH接続もスタンダード以上のプランだと使えたりと、もの凄く良いサービスだと思うけど、サーバー環境は結構癖が強いです(笑)

好んで利用ているサーバーのひとつだけど、凝った事をしようとすると、結構ハマります。
今回の件だと、OSがFreeBSDで、標準のシェルがcshだったり、Pythonのバージョンが古かったりとか……。

という訳で、今回はさくらのレンタルサーバーでaws-cliを設定してS3を外部バックアップ先として送信できるようにできるまでの作業メモを整理してみました。
ちゃんとやれば、これと言ったハマりポイントもなく、スムーズに設定できるはずです。

Pythonのpipをインストールする

以下、さくらのレンタルサーバーにSSH接続している事が前提のコマンド操作になります。

Pythonのバージョン確認

執筆時のPythonの最新バージョンは3.6.4なので、それよりバージョンは古いけど、今回の用途では問題なく動きます。

% python -V

Python 2.7.6

~/.pydistutils.cfgを作る

Pythonの設定ファイル「~/.pydistutils.cfg」を用意し、インストールできる権限を付与しておきます。
中身はこんな感じです。

[install]
user=1

~/.cshrc を編集し「set path」の行に「$HOME/.local/bin」を追加します。

たぶん、最初はこんな感じなので……

set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin)

……こんな感じにしておきます。

set path = (/sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin $HOME/bin $HOME/.local/bin)

pipをインストール

aws-cliをインストールするために、eash_installコマンドでpipをインストールします。 警告がでるけど、今回の用途では特に問題ありませんでした。

% easy_install pip

Searching for pip
Reading https://pypi.python.org/simple/pip/
Best match: pip 9.0.1
Downloading https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
Processing pip-9.0.1.tar.gz
Writing /tmp/easy_install-pMsKpD/pip-9.0.1/setup.cfg
Running pip-9.0.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-pMsKpD/pip-9.0.1/egg-dist-tmp-HDw3FZ
/usr/local/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'python_requires'
  warnings.warn(msg)
warning: no previously-included files found matching '.coveragerc'
warning: no previously-included files found matching '.mailmap'
warning: no previously-included files found matching '.travis.yml'
warning: no previously-included files found matching '.landscape.yml'
warning: no previously-included files found matching 'pip/_vendor/Makefile'
warning: no previously-included files found matching 'tox.ini'
warning: no previously-included files found matching 'dev-requirements.txt'
warning: no previously-included files found matching 'appveyor.yml'
no previously-included directories found matching '.github'
no previously-included directories found matching '.travis'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
Adding pip 9.0.1 to easy-install.pth file
Installing pip script to /home/[アカウントID]/.local/bin
Installing pip2.7 script to /home/[アカウントID]/.local/bin
Installing pip2 script to /home/[アカウントID]/.local/bin

Installed /home/[アカウントID]/.local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip

この状態でpipコマンドを打ってみても、たぶんそんなコマンドねーよと怒られます。

% pip -V

pip: Command not found.

……なので、sourceコマンドで再読込します。

% source ~/.cshrc

いけましたね。

% pip -V

pip 9.0.1 from /home/[アカウントID]/.local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)

aws-cliをインストール

あとはAWSのドキュメント 「AWS Command Line Interface のインストール」の手順にしたがって、インストールします。

% pip install awscli --upgrade --user

Collecting awscli
/home/[アカウントID]/.local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/home/[アカウントID]/.local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading awscli-1.14.32-py2.py3-none-any.whl (1.2MB)
    100% |████████████████████████████████| 1.2MB 619kB/s
Collecting docutils>=0.10 (from awscli)
  Downloading docutils-0.14-py2-none-any.whl (543kB)
    100% |████████████████████████████████| 552kB 1.4MB/s
Collecting colorama<=0.3.7,>=0.2.5 (from awscli)
  Downloading colorama-0.3.7-py2.py3-none-any.whl
Collecting PyYAML<=3.12,>=3.10 (from awscli)
  Downloading PyYAML-3.12.tar.gz (253kB)
    100% |████████████████████████████████| 256kB 3.6MB/s
Collecting botocore==1.8.36 (from awscli)
  Downloading botocore-1.8.36-py2.py3-none-any.whl (4.1MB)
    100% |████████████████████████████████| 4.1MB 215kB/s
Collecting s3transfer<0.2.0,>=0.1.12 (from awscli)
  Downloading s3transfer-0.1.12-py2.py3-none-any.whl (59kB)
    100% |████████████████████████████████| 61kB 4.4MB/s
Collecting rsa<=3.5.0,>=3.1.2 (from awscli)
  Downloading rsa-3.4.2-py2.py3-none-any.whl (46kB)
    100% |████████████████████████████████| 51kB 6.9MB/s
Collecting python-dateutil<3.0.0,>=2.1 (from botocore==1.8.36->awscli)
  Downloading python_dateutil-2.6.1-py2.py3-none-any.whl (194kB)
    100% |████████████████████████████████| 194kB 3.8MB/s
Collecting jmespath<1.0.0,>=0.7.1 (from botocore==1.8.36->awscli)
  Downloading jmespath-0.9.3-py2.py3-none-any.whl
Collecting futures<4.0.0,>=2.2.0; python_version == "2.6" or python_version == "2.7" (from s3transfer<0.2.0,>=0.1.12->awscli)
  Downloading futures-3.2.0-py2-none-any.whl
Collecting pyasn1>=0.1.3 (from rsa<=3.5.0,>=3.1.2->awscli)
  Downloading pyasn1-0.4.2-py2.py3-none-any.whl (71kB)
    100% |████████████████████████████████| 71kB 6.6MB/s
Collecting six>=1.5 (from python-dateutil<3.0.0,>=2.1->botocore==1.8.36->awscli)
  Downloading six-1.11.0-py2.py3-none-any.whl
Installing collected packages: docutils, colorama, PyYAML, six, python-dateutil, jmespath, botocore, futures, s3transfer, pyasn1, rsa, awscli
  Running setup.py install for PyYAML ... done
Successfully installed PyYAML-3.12 awscli-1.14.32 botocore-1.8.36 colorama-0.3.7 docutils-0.14 futures-3.2.0 jmespath-0.9.3 pyasn1-0.4.2 python-dateutil-2.6.1 rsa-3.4.2 s3transfer-0.1.12 six-1.11.0
/home/[アカウントID]/.local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

もう一回sourceコマンドを入れます。

% source ~/.cshrc

aws-cliのコマンドが使えようになりました。

% aws --version

aws-cli/1.14.32 Python/2.7.6 FreeBSD/9.1-RELEASE-p24 botocore/1.8.36

ついでにaws-cliでS3を操作してみる

MacやLinuxにインストールしたaws-cli同様に、さくらのレンタルサーバーでも問題なく動きました。

ついでに初期設定とS3を操作する様子をちょっと紹介します。

設定

あらかじめ「AWS アカウントのアクセスキー管理」の記事を参考にアクセスキーIDとシークレットアクセスキーを用意しておきます。

用意できたら、 「AWS CLI の設定」の記事を見ながら入力します。

% aws configure

AWS Access Key ID [None]: [アクセスキーIDを入力]
AWS Secret Access Key [None]: [シークレットアクセスキーを入力]
Default region name [None]: [リージョン名を入力。東京ならap-northeast-1]
Default output format [None]: [出力フォーマットを入力。jsonとかtextとか]

S3のバケットを作る

今回はwwwディレクトリをS3にまるごと送ってバックアップがしたかったんです。
S3は格安でバージョニングまで対応されてるのでこういった用途にぴったりですね。

バケットの制約と制限」の記事を参考にバケットの作成をします。

% aws s3 mb s3://[バケット名を入力]

バケットの作成に成功していたら、lsで表示されます。

% aws s3 ls

2018-01-23 12:34:56 [バケット名]

バケットをバージョニングに対応させる

バージョニング対応もコマンドでできます。
put-bucket-versioning(英語のみ)」を見ながら、こんなコマンドで設定しました。

% aws s3api put-bucket-versioning --bucket [バケット名を入力。"s3://"は付けない] --versioning-configuration Status=Enabled

S3にファイル送信

最後にファイルを送信します。今回はバージョニングをしているバケットなので、sync(同期するコマンド)でいきます。
wwwディレクトリ以下をどかんと送ってましいます。

% aws s3 sync ~/www/ s3://[バケット名]

wwwディレクトリの大きさによっては、しばらく待つ必要があるけど、結構早いはずです。
syncで送っているので、もう一度実行したら、差分だけの送信になります。

–deleteオプションをつけると、wwwディレクトリで削除したファイルをs3内でも削除することができます。

% aws s3 sync ~/www/ s3://[バケット名] --delete

バージョニングをしていると、間違った変更をしてしまったり、必要なファイルを削除してしまった時も安心ですね。

このコマンドを定期実行させれば、手軽にさくらのレンタルサーバー外に送る事が出来ますね。 「crontab -e」のコマンドでも、サーバーコントロールパネルからでも設定しておきましょう。

こんな感じで、一回準備してしまえば、あとはいつでもawsの機能をさくらのレンタルサーバーで使えるので楽ちんです。

こうやってAWSとさくらのレンタルサーバーを上手く組み合わせれば、おこづかいレベルのランニングコストで結構凝ったサービスとか作れそうですね。(たぶんやらない)