Fragments of verbose memory

冗長な記憶の断片 - Web技術のメモをほぼ毎日更新(準備中)

Feb 1, 2021 - コメント - 日記

n8nでHackMDのメモをGoogleDriveにバックアップする

今日は以前の記事HackMD CLIをJSON-RPC経由で使う でで作ったhackmd-cli-apin8n に組み込んでHackMDの日次バックアップを実装してみました。

できあがったのがこちら。 Backup HackMD to Google Drive

n8nからHackMDを使うために、n8nを起動しているdocker-compose.ymlに以前作ったhackmd-cli-apiを組み込みます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
version: '3'
services:
  n8n:
    image: n8n-io/n8n
    volumes:
      - n8n-data:/home/node/.n8n
    environment:
      - NODE_ENV=production
      - GENERIC_TIMEZONE=Asia/Tokyo
      - TZ=Asia/Tokyo
  hackmd-cli:
    image: tumf/hackmd-cli-api
    environment:
      HMD_CLI_ID: ${HMD_CLI_ID}
      HMD_CLI_PASSWORD: ${HMD_CLI_PASSWORD}

ちょっと長いですが、n8nのコードも載せておきます。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{
  "name": "hackmd-backup",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {}
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        250,
        150
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "http://hackmd-cli:8999",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "{\"jsonrpc\":\"2.0\", \"id\": \"1\", \"method\":\"shell\", \"params\": [{ \"command\": \"hackmd-cli login\" }, {\"command\": \"hackmd-cli history\"}] }"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        480,
        220
      ]
    },
    {
      "parameters": {
        "functionCode": "var ids = [];\nitems.map((item) => {\n  var ids_text = item.json.result[1].stdout;\n  ids = ids.concat(ids_text.split(\"\\n\"));\n});\nvar result = [];\nids.map((id) => {\n  result.push({ \"json\": { \"id\": id } });\n});\nreturn result;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        680,
        220
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "http://hackmd-cli:8999",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={\"jsonrpc\":\"2.0\", \"id\": \"1\", \"method\":\"shell\", \"params\": [{ \"command\": \"hackmd-cli export {{$json['id']}}\" }] }"
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        870,
        220
      ]
    },
    {
      "parameters": {
        "fileContent": "={{$json[\"result\"][0][\"stdout\"]}}",
        "name": "={{$json[\"id\"]}}.md",
        "parents": [
          "<Folder ID>"
        ],
        "options": {}
      },
      "name": "Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        1240,
        360
      ],
      "credentials": {
        "googleApi": "<google api service id>"
      }
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1030,
        360
      ]
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {},
  "id": "11"
}

GoogleDriveに書き込むために、GCPでサービスアカウントを作成しバックアップ先のフォルダーを共有する必要がありますのでお忘れなく。