From 5f40061d4800628ebd2d0a764d21e8d064f7b75e Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 5 May 2012 10:55:41 +0200 Subject: [PATCH] Backup database on start --- couchpotato/runner.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/couchpotato/runner.py b/couchpotato/runner.py index 0be3bdbc..895e87c2 100644 --- a/couchpotato/runner.py +++ b/couchpotato/runner.py @@ -9,6 +9,7 @@ import atexit import locale import logging import os.path +import shutil import sys import time import warnings @@ -58,13 +59,42 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En if not encoding or encoding in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): encoding = 'UTF-8' + # Do db stuff + db_path = os.path.join(data_dir, 'couchpotato.db') + + # Backup before start and cleanup old databases + new_backup = os.path.join(data_dir, 'db_backup', str(int(time.time()))) + + # Create path and copy + if not os.path.isdir(new_backup): os.makedirs(new_backup) + src_files = [options.config_file, db_path] + for src_file in src_files: + shutil.copy2(src_file, os.path.join(new_backup, os.path.basename(src_file))) + + # Remove older backups, keep backups 3 days or at least 3 + backups = [] + for directory in os.listdir(os.path.dirname(new_backup)): + backup = os.path.join(os.path.dirname(new_backup), directory) + if os.path.isdir(backup): + backups.append(backup) + + total_backups = len(backups) + for backup in backups: + if total_backups > 3: + if int(os.path.basename(directory)) < time.time() - 259200: + for src_file in src_files: + os.remove(os.path.join(backup, os.path.basename(src_file))) + os.rmdir(backup) + total_backups -= 1 + + # Register environment settings Env.set('encoding', encoding) Env.set('uses_git', not options.nogit) Env.set('app_dir', base_path) Env.set('data_dir', data_dir) Env.set('log_path', os.path.join(log_dir, 'CouchPotato.log')) - Env.set('db_path', 'sqlite:///' + os.path.join(data_dir, 'couchpotato.db')) + Env.set('db_path', 'sqlite:///' + db_path) Env.set('cache_dir', os.path.join(data_dir, 'cache')) Env.set('cache', FileSystemCache(os.path.join(Env.get('cache_dir'), 'python'))) Env.set('console_log', options.console_log)