Merge pull request #925 from zanllp/chore/optimize-db-backup

chore(db): optimize database backup to once per day and reduce max ba…
pull/926/head
zanllp 2026-02-22 13:46:35 +08:00 committed by GitHub
commit 6a98bcb429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 4 deletions

View File

@ -12,7 +12,7 @@ IIB_SECRET_KEY=
IIB_SERVER_LANG=auto IIB_SERVER_LANG=auto
# This configuration parameter specifies the maximum number of database file backups for the IIB . # This configuration parameter specifies the maximum number of database file backups for the IIB .
IIB_DB_FILE_BACKUP_MAX=8 IIB_DB_FILE_BACKUP_MAX=4
# Set the cache directory for IIB, including image cache and video cover cache. # Set the cache directory for IIB, including image cache and video cover cache.
# The default is the system's temporary directory, but if you want to specify a custom directory, set it here. # The default is the system's temporary directory, but if you want to specify a custom directory, set it here.

View File

@ -83,14 +83,28 @@ def backup_db_file(db_file_path):
if not os.path.exists(db_file_path): if not os.path.exists(db_file_path):
return return
max_backup_count = int(os.environ.get('IIB_DB_FILE_BACKUP_MAX', '8')) max_backup_count = int(os.environ.get('IIB_DB_FILE_BACKUP_MAX', '4'))
if max_backup_count < 1: if max_backup_count < 1:
return return
backup_folder = os.path.join(cwd,'iib_db_backup') backup_folder = os.path.join(cwd,'iib_db_backup')
os.makedirs(backup_folder, exist_ok=True)
# Check if backup already exists today
current_date = datetime.now().date()
backup_files = os.listdir(backup_folder) if os.path.exists(backup_folder) else []
pattern = r"iib\.db_(\d{4}-\d{2}-\d{2})"
for f in backup_files:
match = re.search(pattern, f)
if match:
backup_date = datetime.strptime(match.group(1), '%Y-%m-%d').date()
if backup_date == current_date:
print(f"\033[93mIIB Database backup already exists for today ({current_date}). Skipping backup.\033[0m")
return
current_time = datetime.now() current_time = datetime.now()
timestamp = current_time.strftime('%Y-%m-%d %H-%M-%S') timestamp = current_time.strftime('%Y-%m-%d %H-%M-%S')
backup_filename = f"iib.db_{timestamp}" backup_filename = f"iib.db_{timestamp}"
os.makedirs(backup_folder, exist_ok=True)
backup_filepath = os.path.join(backup_folder, backup_filename) backup_filepath = os.path.join(backup_folder, backup_filename)
shutil.copy2(db_file_path, backup_filepath) shutil.copy2(db_file_path, backup_filepath)
backup_files = os.listdir(backup_folder) backup_files = os.listdir(backup_folder)
@ -103,7 +117,7 @@ def backup_db_file(db_file_path):
for i in range(files_to_remove_count): for i in range(files_to_remove_count):
file_to_remove = os.path.join(backup_folder, sorted_backup_files[i][0]) file_to_remove = os.path.join(backup_folder, sorted_backup_files[i][0])
os.remove(file_to_remove) os.remove(file_to_remove)
print(f"\033[92mIIB Database file has been successfully backed up to the backup folder.\033[0m") print(f"\033[92mIIB Database file has been successfully backed up to the backup folder.\033[0m")
def get_sd_webui_conf(**kwargs): def get_sd_webui_conf(**kwargs):