diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index 9b028c08..9d172e45 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -153,6 +153,19 @@

The interval (in hours) PlexPy will backup the database and configuration file. Minimum 1, maximum 24, default 6.

+
+ +
+
+ +
+ +
+

+ The number of days to keep scheduled backups. Minimum 1, default 3.
+ Note: Manual backups are not removed automatically. +

+

Directories

diff --git a/plexpy/config.py b/plexpy/config.py index d01669fd..1db19e2b 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -101,6 +101,7 @@ _CONFIG_DEFINITIONS = { 'BROWSER_ON_NEWDEVICE': (int, 'Browser', 0), 'BUFFER_THRESHOLD': (int, 'Monitoring', 3), 'BUFFER_WAIT': (int, 'Monitoring', 900), + 'BACKUP_DAYS': (int, 'General', 3), 'BACKUP_DIR': (str, 'General', ''), 'BACKUP_INTERVAL': (int, 'General', 6), 'CACHE_DIR': (str, 'General', ''), @@ -604,12 +605,12 @@ def make_backup(cleanup=False, scheduler=False): shutil.copyfile(plexpy.CONFIG_FILE, backup_file_fp) if cleanup: + now = time.time() # Delete all scheduled backup files except from the last 5. for root, dirs, files in os.walk(backup_folder): - db_files = [os.path.join(root, f) for f in files if f.endswith('.sched.ini')] - if len(db_files) > 5: - backups_sorted_on_age = sorted(db_files, key=os.path.getctime, reverse=True) - for file_ in backups_sorted_on_age[5:]: + ini_files = [os.path.join(root, f) for f in files if f.endswith('.sched.ini')] + for file_ in ini_files: + if os.stat(file_).st_mtime < now - plexpy.CONFIG.BACKUP_DAYS * 86400: try: os.remove(file_) except OSError as e: diff --git a/plexpy/database.py b/plexpy/database.py index b660eaa4..2bd8902c 100644 --- a/plexpy/database.py +++ b/plexpy/database.py @@ -79,12 +79,12 @@ def make_backup(cleanup=False, scheduler=False): db.connection.rollback() if cleanup: - # Delete all scheduled backup files except from the last 5. + now = time.time() + # Delete all scheduled backup older than BACKUP_DAYS. for root, dirs, files in os.walk(backup_folder): db_files = [os.path.join(root, f) for f in files if f.endswith('.sched.db')] - if len(db_files) > 5: - backups_sorted_on_age = sorted(db_files, key=os.path.getctime, reverse=True) - for file_ in backups_sorted_on_age[5:]: + for file_ in db_files: + if os.stat(file_).st_mtime < now - plexpy.CONFIG.BACKUP_DAYS * 86400: try: os.remove(file_) except OSError as e: diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 04cbc79e..a2642ea0 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -2527,6 +2527,7 @@ class WebInterface(object): "api_key": plexpy.CONFIG.API_KEY, "update_db_interval": plexpy.CONFIG.UPDATE_DB_INTERVAL, "freeze_db": checked(plexpy.CONFIG.FREEZE_DB), + "backup_days": plexpy.CONFIG.BACKUP_DAYS, "backup_dir": plexpy.CONFIG.BACKUP_DIR, "backup_interval": plexpy.CONFIG.BACKUP_INTERVAL, "cache_dir": plexpy.CONFIG.CACHE_DIR,