From afa45119e56d53defcfd536c37ef4013f2464366 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Mon, 25 Jul 2022 00:04:13 +0900 Subject: [PATCH] =?UTF-8?q?backup=20=EC=8A=A4=ED=81=AC=EB=A6=BD=ED=8A=B8?= =?UTF-8?q?=20=EA=B5=AC=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hidche/backup/Dockerfile | 10 ++++++ hidche/backup/run_backup.py | 72 +++++++++++++++++++++++++++++++++++++ hidche/docker-compose.yml | 18 ++++++++-- 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 hidche/backup/Dockerfile create mode 100644 hidche/backup/run_backup.py diff --git a/hidche/backup/Dockerfile b/hidche/backup/Dockerfile new file mode 100644 index 0000000..ccb0f0e --- /dev/null +++ b/hidche/backup/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3-alpine +#alpine + python3 + cron(from alpine) + mariadb-client +RUN apk add --no-cache mariadb-client rsync + +COPY run_backup.py /usr/local/bin/ +RUN chmod +x /usr/local/bin/run_backup.py && \ + echo '0 5 * * * python3 /usr/local/bin/run_backup.py' > /etc/crontabs/root + +ENTRYPOINT [] +CMD ["crond", "-f" , "-L", "/dev/stdout"] \ No newline at end of file diff --git a/hidche/backup/run_backup.py b/hidche/backup/run_backup.py new file mode 100644 index 0000000..29968de --- /dev/null +++ b/hidche/backup/run_backup.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +import os +import os.path +from subprocess import Popen, PIPE, run as subprocess_run +from datetime import datetime +from multiprocessing.dummy import Pool as ThreadPool + +def backup_table(db_name, table_name, output_prefix): + sql_path = '{}/{}/{}.sql.gz'.format(output_prefix, db_name, table_name) + #print(sql_path) + with open(sql_path, 'wb') as sql_out: + sqldump = Popen(['mysqldump', '-f', '--skip-comments', '-h', 'db', '-u', 'root', '--password={}'.format(root_pw), db_name, table_name], stdout=PIPE) + gzdump = Popen(['gzip', '-c'], stdin=sqldump.stdout, stdout=sql_out) + sqldump.wait() + gzdump.wait() + os.chmod(sql_path, 0o644) + print('sql done {}, {}'.format(db_name, table_name)) + +def backup_files(src_path, output_path): + subprocess_run(['rsync', '-au', src_path, output_path]) + print('file done {}'.format(src_path)) + +now = datetime.now() +print('> backup begin', now.isoformat()) +weekday = now.weekday() +hour = now.hour + +store_path = '/var/backup' +output_prefix = '{}/hidche_backup_w{}-{:02}'.format(store_path, weekday, hour) + +os.makedirs(output_prefix, exist_ok=True) + +db_prefix = os.environ['HIDCHE_DB_PREFIX'] +server_list = os.environ['HIDCHE_SERVER_LIST'].strip(' ,').split(',') +server_list.append('root') + +board_db_name = os.environ['HIDCHE_DB_BBS_USER'] +server_list.append(board_db_name) +#print(server_list) + +root_pw = os.environ['MARIADB_ROOT_PASSWORD'] + +board_files_path = os.environ.get('HIDCHE_BOARD_FILES_PATH', '/var/www/board/files') + +with ThreadPool(4) as pool: + waiters = [] + waiters.append(pool.apply_async(backup_files, ('/var/www/html/sam/d_pic', '{}/sam_image'.format(output_prefix)))) + print(board_files_path) + if os.path.exists(board_files_path): + waiters.append(pool.apply_async(backup_files, (board_files_path, '{}/board_files'.format(output_prefix)))) + + for server_name in server_list: + db_name = '{}_{}'.format(db_prefix, server_name) + raw_tables = subprocess_run(['mysql', '-NBA' , '-h' ,'db', '-u', 'root' ,'--password={}'.format(root_pw), '-D', db_name, '-e', 'show tables'], stdout=PIPE) + raw_tables = raw_tables.stdout.decode('utf-8').strip(' \n\t') + if raw_tables == '': + continue + + tables = raw_tables.split('\n') + d_setting_path = '/var/www/html/sam/{}/d_setting'.format(server_name) + + if os.path.exists(d_setting_path): + waiters.append(pool.apply_async(backup_files, (d_setting_path, '{}/{}'.format(output_prefix, db_name)))) + + os.makedirs('{}/{}'.format(output_prefix, db_name), exist_ok=True) + for table_name in tables: + waiters.append(pool.apply_async(backup_table, (db_name, table_name, output_prefix))) + for waiter in waiters: + waiter.wait() + +print('> backup finish', datetime.now().isoformat()) \ No newline at end of file diff --git a/hidche/docker-compose.yml b/hidche/docker-compose.yml index c87ea17..5d2831d 100644 --- a/hidche/docker-compose.yml +++ b/hidche/docker-compose.yml @@ -9,7 +9,7 @@ services: env_file: - account.env - game.env - + web: build: ./web restart: always @@ -54,6 +54,19 @@ services: - db - web + backup: + build: ./backup + restart: "no" + volumes: + - hidche:/var/www/html:ro + - backup:/var/backup + env_file: + - account.env + - game.env + depends_on: + - app + - db + cron: build: ./cron restart: always @@ -65,4 +78,5 @@ services: volumes: db: hidche: - board: \ No newline at end of file + board: + backup: \ No newline at end of file