LoginSignup
1
1

More than 5 years have passed since last update.

GnuPGで暗号化されたファイルをcronで定期的に復号する

Posted at

GnuPGとは

GnuPG の機能については The GNU Privacy Guard に書いてある。

GnuPG allows to encrypt and sign your data and communication, features a versatile key management system as well as access modules for all kinds of public key directories.

GnuPGはデータの暗号化・署名そして通信を可能にします、また色々な種類の公開鍵のための接続モジュールであるとともに多目的の鍵管理システムをその特徴としています。

これもネット上では何度も言われていそうだが、GnuPGには元となる規格PGP(Open PGP: RFC4880 )が存在する。GPGはPGPの実装である。

暗号化・復号

暗号化・復号の方法は他にいい記事があるので、それをチェックする

Cronでの復号

/etc/crontab を編集

  • HOME を実行ユーザのホームに変えると、パスが通って楽になる
  • Cronのログは何もしないと残らないので、標準および標準エラー出力を適当なところにリダイレクトするといいようです
# vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
-HOME=/
+HOME=/root

# あとは以下のように適当に実行するシェルを指定
# 毎日10時30にrootユーザで/root/decrypt.sh以降のコマンド実行
+30 10 * * * root /root/decrypt.sh /root/input.gpg >>/tmp/gpg.log 2>>/tmp/gpg-err.log

復号用のコマンドをシェル化

オプションのおおよその意味

  • -vv 出力の詳細化
  • --no-tty 端末で開くわけじゃないので設定する
  • --batch 対話型のコマンドだと途中で処理が止まってしまうので、これを設定して何も聞かれないようにする
  • --passphrase-file GPGの秘密鍵のパスフレーズをここに記載するする
+#!/bin/bash
+INPUT=${1}
+OUTPUT=${INPUT%.*}.zip
+echo "Input file: ${INPUT}"
+echo "Output file: ${OUTPUT}"
+/usr/bin/gpg -vv --no-tty --batch --passphrase-file /root/.gnupg/passphrase.txt --output ${OUTPUT} --decrypt ${INPUT}
1
1
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
1
1