python
import os
import shutil
from PyDBLitewebpy import PyDBLitewebpy
import web
db_path = 'data.db'
app = web.application()
backup_dir = 'backup'
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
urls = (
'/backup', 'backup',
'/restore/(\d+)', 'restore'
)
class backup:
def GET(self):
db = PyDBLitewebpy(db_path)
backup_file = os.path.join(backup_dir, f'backup_{db.get_timestamp()}.db')
db.backup(backup_file)
return f'Backup created: {backup_file}'
class restore:
def GET(self, backup_id):
db = PyDBLitewebpy(db_path)
backup_file = os.path.join(backup_dir, f'backup_{backup_id}.db')
if not os.path.exists(backup_file):
return 'Backup file does not exist!'
db.close()
os.remove(db_path)
shutil.copy(backup_file, db_path)
return 'Database restored successfully!'
if __name__ == "__main__":
app.run(urls, globals())