Created
December 5, 2025 17:05
-
-
Save manuelep/3a6b3eb0f70c19b3dd8d89cf3006055a to your computer and use it in GitHub Desktop.
Creates a snapshot backup of a GeoPackage that stores the QGIS project and data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import shutil | |
| from datetime import datetime | |
| from qgis.core import QgsProject | |
| project_path = QgsProject.instance().fileName() | |
| if not project_path: | |
| print("⚠ Nessun progetto aperto.") | |
| else: | |
| gpkg_path = None | |
| # verifica se progetto è contenuto in un geopackage | |
| print(f'Path del progetto: {project_path}') | |
| if project_path.startswith("geopackage:"): | |
| # rimuove "geopackage:" e tutto dopo ? | |
| stripped = project_path.replace("geopackage:", "") | |
| gpkg_path = stripped.split("?")[0] | |
| else: | |
| print("❗ Il progetto non sembra essere salvato in un GeoPackage.") | |
| if gpkg_path: | |
| print(f'Path del geopackage: {gpkg_path}') | |
| base_dir = os.path.dirname(gpkg_path) | |
| print(base_dir) | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| backup_dir = os.path.join(base_dir, f"backup_{timestamp}") | |
| os.makedirs(backup_dir, exist_ok=True) | |
| backup_path = os.path.join(backup_dir, os.path.basename(gpkg_path)) | |
| shutil.copy2(gpkg_path, backup_path) | |
| print(f"✔ Backup creato:\n{backup_path}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment