From 914c58175727a2937463b1b03a845c38b8a0d73b Mon Sep 17 00:00:00 2001 From: mistakia <1823355+mistakia@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:15:17 -0500 Subject: [PATCH] refactor: update `archive-mysql` script --- config.sample.mjs | 15 +++++++++++++++ scripts/archive-mysql.mjs | 21 ++++++++++++++++++++- server/crontab | 1 - 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/config.sample.mjs b/config.sample.mjs index 18010652..8ba1814b 100644 --- a/config.sample.mjs +++ b/config.sample.mjs @@ -32,6 +32,21 @@ export const mysql = { } } +export const storage_mysql = { + client: 'mysql', + connection: { + host: 'localhost', + user: 'root', + password: '', + database: 'nano_development', + charset: 'utf8mb4' + }, + pool: { + min: 2, + max: 20 + } +} + export const nanodbAPI = '' export const trustedAddresses = [] export const rpcAddresses = [] diff --git a/scripts/archive-mysql.mjs b/scripts/archive-mysql.mjs index 474b7730..08dde129 100644 --- a/scripts/archive-mysql.mjs +++ b/scripts/archive-mysql.mjs @@ -4,14 +4,18 @@ import fs from 'fs' import { exec } from 'child_process' import { createObjectCsvWriter as createCsvWriter } from 'csv-writer' import dayjs from 'dayjs' +import Knex from 'knex' import db from '#db' import { isMain } from '#common' +import * as config from '#config' + +const storage_mysql = Knex(config.storage_mysql) const execp = util.promisify(exec) const logger = debug('archive') -const dir = '/root/archives' +const dir = '/home/user/nano-community-archives' const zip = async ({ gzFilename, csvFilename }) => { logger(`creating zip of ${csvFilename}`) @@ -48,6 +52,12 @@ const archiveRepresentativesUptime = async () => { return } + await storage_mysql('representatives_uptime') + .insert(rows) + .onConflict() + .merge() + logger(`copied ${rows.length} rows to storage_mysql`) + logger(`archving ${rows.length} representatives_uptime entries`) const filename = `representatives-uptime-archive_${timestamp}` const csvFilename = `${filename}.csv` @@ -85,6 +95,12 @@ const archiveRepresentativesTelemetry = async () => { return } + await storage_mysql('representatives_telemetry') + .insert(rows) + .onConflict() + .merge() + logger(`copied ${rows.length} rows to storage_mysql`) + logger(`archving ${rows.length} representatives_telemetry entries`) const filename = `representatives-telemetry-archive_${timestamp}` const csvFilename = `${filename}.csv` @@ -142,6 +158,9 @@ const archivePosts = async () => { return } + await storage_mysql('posts').insert(posts).onConflict().merge() + logger(`copied ${posts.length} rows to storage_mysql`) + logger(`archving ${posts.length} posts`) const filename = `posts-archive_${timestamp}` const csvFilename = `${filename}.csv` diff --git a/server/crontab b/server/crontab index 4032c939..9d79b07e 100644 --- a/server/crontab +++ b/server/crontab @@ -42,7 +42,6 @@ ## backup mysql 0 0 * * 0 /bin/bash /root/nano-community/scripts/backup-mysql.sh -f &>> /var/log/nano-community/backup-mysql-full.log -0 1 * * 0 NODE_ENV=production /root/.nvm/versions/node/v14.17.0/bin/node /root/nano-community/scripts/archive-mysql.mjs &>> /var/log/nano-community/archive-mysql.log ## rollups 0 0 * * * NODE_ENV=production /root/.nvm/versions/node/v14.17.0/bin/node /root/nano-community/scripts/rollup-uptime.mjs --days 2 &>> /var/log/nano-community/rollup-uptime.log