Skip to content

Commit

Permalink
Merge pull request #1157 from deepthivenkat/issues/1145/1
Browse files Browse the repository at this point in the history
Issue #1145 - Move issues.db to backup location configured in secrets.py
  • Loading branch information
Mike Taylor authored Aug 24, 2016
2 parents e48404c + 7ea106f commit 9736949
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ uploads/
.idea/
config/secrets.py

# backup folder contains the issues.db backup file with issues dump
# for the previous versions
backups/*.db

# these are our 'dist' files
# checking them into the repo is silly
webcompat/static/js/webcompat.js
Expand Down
7 changes: 7 additions & 0 deletions config/secrets.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ elif LOCALHOST:
UPLOADS_DEFAULT_DEST = BASE_DIR + '/uploads/'
UPLOADS_DEFAULT_URL = 'http://localhost:5000/uploads/'


# Database backup path.
if LOCALHOST:
BACKUP_DEFAULT_DEST = BASE_DIR + '/backups/'
else:
BACKUP_DEFAULT_DEST = ''

# Production GiHub Issues repo URI. Can be ignored for local testing.
if PRODUCTION:
ISSUES_REPO_URI = ''
Expand Down
27 changes: 27 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import argparse
import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
import time
import os
import sys

IMPORT_ERROR = '''
Expand All @@ -27,6 +32,7 @@
==============================================
'''


try:
from webcompat import app
except ImportError, e:
Expand Down Expand Up @@ -97,6 +103,8 @@ def config_validator():
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--testmode', action='store_true',
help='Run server in "test mode".')
parser.add_argument('--backup', action='store_true',
help='backup existing issues.db.')
args = parser.parse_args()

if args.testmode:
Expand All @@ -106,6 +114,25 @@ def config_validator():
app.config['TESTING'] = True
print("Starting server in ~*TEST MODE*~")
app.run(host='localhost')
elif args.backup:
issue_engine = create_engine('sqlite:///' + os.path.join(app.config['BASE_DIR'], 'issues.db'))
issue_session_maker = sessionmaker(autocommit=False,
autoflush=False,
bind=issue_engine)
issue_db = scoped_session(issue_session_maker)
# Take a backup if issues.db has data dump.
if issue_db().execute('select count(*) from webcompat_issues').fetchall()[0][0] > 0:
if not os.path.exists(app.config['BACKUP_DEFAULT_DEST']):
print ('Creating backup directory at {}').format(os.path.join(app.config['BASE_DIR'], 'issues.db'))
os.makedirs(app.config['BACKUP_DEFAULT_DEST'])
time_stamp = time.strftime('%Y-%m-%dT%H-%M-%S', time.localtime())
issue_backup_db = 'issues_' + time_stamp + '.db'
os.rename(os.getcwd() + '/issues.db', app.config['BACKUP_DEFAULT_DEST'] + issue_backup_db)
# Retain last 3 recent backup files
backup_files = os.listdir(app.config['BACKUP_DEFAULT_DEST'])
backup_files.sort()
for old_file in backup_files[:-3]:
os.remove(app.config['BACKUP_DEFAULT_DEST'] + old_file)
else:
if check_pip_deps():
app.run(host='localhost')

0 comments on commit 9736949

Please sign in to comment.