Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the cache db and cache files optional for inclusion in backup #10475

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions medusa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,9 @@ def initialize(self, console_logging=True):
app.CACHE_RECOMMENDED_TRAKT_LISTS = check_setting_list(app.CFG, 'Recommended', 'trakt_lists', app.CACHE_RECOMMENDED_TRAKT_LISTS)
app.CACHE_RECOMMENDED_PURGE_AFTER_DAYS = check_setting_int(app.CFG, 'Recommended', 'purge_after_days', 180)

app.BACKUP_CACHE_DB = check_setting_int(app.CFG, 'Backup', 'cache_db', 1)
app.BACKUP_CACHE_FILES = check_setting_int(app.CFG, 'Backup', 'cache_files', 1)

# Initialize trakt config path.
trakt.core.CONFIG_PATH = os.path.join(app.CACHE_DIR, '.pytrakt.json')
trakt.core.load_config()
Expand Down Expand Up @@ -1757,6 +1760,10 @@ def save_config():
new_config['Blackhole']['nzb_dir'] = app.NZB_DIR
new_config['Blackhole']['torrent_dir'] = app.TORRENT_DIR

new_config['Backup'] = {}
new_config['Backup']['cache_db'] = int(app.BACKUP_CACHE_DB)
new_config['Backup']['cache_files'] = int(app.BACKUP_CACHE_FILES)

# dynamically save provider settings
all_providers = providers.sorted_provider_list()
for provider in all_providers:
Expand Down
5 changes: 5 additions & 0 deletions medusa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ def __init__(self):
self.EXT3_FOLDER = 'ext3'
self.STATIC_FOLDER = 'static'
self.UNKNOWN_RELEASE_GROUP = 'Medusa'

# Backup related
self.BACKUP_DIR = 'backup'
self.BACKUP_FILENAME_PREFIX = 'backup'
self.BACKUP_FILENAME = self.BACKUP_FILENAME_PREFIX + '-{timestamp}.zip'
self.BACKUP_CACHE_DB = None
self.BACKUP_CACHE_FILES = None

self.LEGACY_DB = 'sickbeard.db'
self.APPLICATION_DB = 'main.db'
self.FAILED_DB = 'failed.db'
Expand Down
7 changes: 7 additions & 0 deletions medusa/server/api/v2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class ConfigHandler(BaseRequestHandler):
'webInterface.httpsKey': StringField(app, 'HTTPS_KEY'),
'webInterface.handleReverseProxy': BooleanField(app, 'HANDLE_REVERSE_PROXY'),

'backup.cacheDb': BooleanField(app, 'BACKUP_CACHE_DB'),
'backup.cacheFiles': BooleanField(app, 'BACKUP_CACHE_FILES'),

'webRoot': StringField(app, 'WEB_ROOT'),
'cpuPreset': StringField(app, 'CPU_PRESET'),
'sslVerify': BooleanField(app, 'SSL_VERIFY'),
Expand Down Expand Up @@ -740,6 +743,10 @@ def data_main():
section_data['webInterface']['httpsKey'] = app.HTTPS_KEY
section_data['webInterface']['handleReverseProxy'] = bool(app.HANDLE_REVERSE_PROXY)

section_data['backup'] = {}
section_data['backup']['cacheDb'] = bool(app.BACKUP_CACHE_DB)
section_data['backup']['cacheFiles'] = bool(app.BACKUP_CACHE_FILES)

section_data['webRoot'] = app.WEB_ROOT
section_data['cpuPreset'] = app.CPU_PRESET
section_data['sslVerify'] = bool(app.SSL_VERIFY)
Expand Down
25 changes: 15 additions & 10 deletions medusa/server/api/v2/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,25 @@ def _backup_to_zip(self, backup_dir):

if backup_dir:
source = [
os.path.join(app.DATA_DIR, app.APPLICATION_DB), app.CONFIG_FILE,
os.path.join(app.DATA_DIR, app.FAILED_DB),
os.path.join(app.DATA_DIR, app.CACHE_DB),
os.path.join(app.DATA_DIR, app.RECOMMENDED_DB)
os.path.join(app.DATA_DIR, app.APPLICATION_DB), app.CONFIG_FILE
]

if app.BACKUP_CACHE_DB:
source += [
os.path.join(app.DATA_DIR, app.FAILED_DB),
os.path.join(app.DATA_DIR, app.CACHE_DB),
os.path.join(app.DATA_DIR, app.RECOMMENDED_DB)
]
target = os.path.join(backup_dir, 'medusa-{date}.zip'.format(date=time.strftime('%Y%m%d%H%M%S')))
log.info(u'Starting backup to location: {location} ', {'location': target})

for (path, dirs, files) in os.walk(app.CACHE_DIR, topdown=True):
for dirname in dirs:
if path == app.CACHE_DIR and dirname not in ['images']:
dirs.remove(dirname)
for filename in files:
source.append(os.path.join(path, filename))
if app.BACKUP_CACHE_FILES:
for (path, dirs, files) in os.walk(app.CACHE_DIR, topdown=True):
for dirname in dirs:
if path == app.CACHE_DIR and dirname not in ['images']:
dirs.remove(dirname)
for filename in files:
source.append(os.path.join(path, filename))

if helpers.backup_config_zip(source, target, app.DATA_DIR):
final_result += 'Successful backup to {location}'.format(location=target)
Expand Down
49 changes: 28 additions & 21 deletions medusa/updater/version_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def _runbackup(self):
log.info(u'Config backup in progress...')
ui.notifications.message('Backup', 'Config backup in progress...')
try:
backupDir = os.path.join(app.DATA_DIR, app.BACKUP_DIR)
if not os.path.isdir(backupDir):
os.mkdir(backupDir)
backup_dir = os.path.join(app.DATA_DIR, app.BACKUP_DIR)
if not os.path.isdir(backup_dir):
os.mkdir(backup_dir)

if self._keeplatestbackup(backupDir) and self._backup(backupDir):
if self._keeplatestbackup(backup_dir) and self._backup(backup_dir):
log.info(u'Config backup successful')
ui.notifications.message('Backup', 'Config backup successful')
return True
Expand All @@ -85,12 +85,12 @@ def _runbackup(self):
return False

@staticmethod
def _keeplatestbackup(backupDir=None):
if not backupDir:
def _keeplatestbackup(backup_dir=None):
if not backup_dir:
return False

import glob
files = glob.glob(os.path.join(backupDir, '*.zip'))
files = glob.glob(os.path.join(backup_dir, '*.zip'))
if not files:
return True

Expand All @@ -109,29 +109,36 @@ def _keeplatestbackup(backupDir=None):

# TODO: Merge with backup in helpers
@staticmethod
def _backup(backupDir=None):
if not backupDir:
def _backup(backup_dir=None):
if not backup_dir:
return False

source = [
os.path.join(app.DATA_DIR, app.APPLICATION_DB),
app.CONFIG_FILE,
os.path.join(app.DATA_DIR, app.FAILED_DB),
os.path.join(app.DATA_DIR, app.CACHE_DB),
os.path.join(app.DATA_DIR, app.RECOMMENDED_DB)
app.CONFIG_FILE
]
target = os.path.join(backupDir, app.BACKUP_FILENAME.format(timestamp=time.strftime('%Y%m%d%H%M%S')))

for (path, dirs, files) in os.walk(app.CACHE_DIR, topdown=True):
for dirname in dirs:
if path == app.CACHE_DIR and dirname not in ['images']:
dirs.remove(dirname)
for filename in files:
source.append(os.path.join(path, filename))
if app.BACKUP_CACHE_DB:
source += [
os.path.join(app.DATA_DIR, app.FAILED_DB),
os.path.join(app.DATA_DIR, app.CACHE_DB),
os.path.join(app.DATA_DIR, app.RECOMMENDED_DB)
]

target = os.path.join(backup_dir, app.BACKUP_FILENAME.format(timestamp=time.strftime('%Y%m%d%H%M%S')))

if app.BACKUP_CACHE_FILES:
for (path, dirs, files) in os.walk(app.CACHE_DIR, topdown=True):
for dirname in dirs:
if path == app.CACHE_DIR and dirname not in ['images']:
dirs.remove(dirname)
for filename in files:
source.append(os.path.join(path, filename))

return helpers.backup_config_zip(source, target, app.DATA_DIR)

def safe_to_update(self):

"""Verify if it's safe to update."""
def db_safe(self):
message = {
'equal': {
Expand Down
4 changes: 4 additions & 0 deletions tests/apiv2/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def config_main(monkeypatch, app_config):
section_data['providers']['prowlarr']['url'] = app.PROWLARR_URL
section_data['providers']['prowlarr']['apikey'] = app.PROWLARR_APIKEY

section_data['backup'] = {}
section_data['backup']['cacheDb'] = bool(app.BACKUP_CACHE_DB)
section_data['backup']['cacheFiles'] = bool(app.BACKUP_CACHE_FILES)

return section_data


Expand Down
22 changes: 22 additions & 0 deletions themes-default/slim/src/components/config-general.vue
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,28 @@
</fieldset>
</div><!-- /col -->
</div>

<div class="row component-group">
<div class="component-group-desc col-xs-12 col-md-2">
<h3>Backup</h3>
</div>
<div class="col-xs-12 col-md-10">
<fieldset class="component-group-list">
<config-toggle-slider v-model="general.backup.cacheDb" label="Backup cache related databases" id="cache_db">
<p>Include cache.db, failed.db, and recommended.db to the backup.</p>
<p><span style="color: red">Note!</span> These files are not mandatory for a proper restore, but could potentially cause timeouts when backing up or trying to update medusa.</p>
</config-toggle-slider>

<config-toggle-slider v-model="general.backup.cacheFiles" label="Backup the cache folder (excluding the images)" id="cache_files">
<p>Include everything in the cache folder to the backup.</p>
<p><span style="color: red">Note!</span> These files are not mandatory for a proper restore, but could potentially cause timeouts when backing up or trying to update medusa.</p>
</config-toggle-slider>

<input type="submit" class="btn-medusa config_submitter" value="Save Changes">
</fieldset>
</div><!-- /col -->
</div>

</div><!-- /component-group3 //-->
<br>
<h6 class="pull-right"><b>All non-absolute folder locations are relative to <span class="path">{{system.dataDir}}</span></b> </h6>
Expand Down
8 changes: 4 additions & 4 deletions themes-default/slim/src/store/modules/config/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const state = {
namingForceFolders: null,
sourceUrl: null,
rootDirs: [],
subtitles: {
enabled: null
},
brokenProviders: [],
logs: {
debug: null,
Expand All @@ -26,7 +23,6 @@ const state = {
custom: {}
},
cpuPreset: null,
subtitlesMulti: null,
anonRedirect: null,
recentShows: [],
randomShowSlug: null, // @TODO: Recreate this in Vue when the webapp has a reliable list of shows to choose from.
Expand Down Expand Up @@ -126,6 +122,10 @@ const state = {
url: null,
apikey: null
}
},
backup: {
cacheDb: null,
cacheFiles: null
}
};

Expand Down
Loading