The "DB::Exception: Backup entry already exists" error in ClickHouse is raised when a backup operation tries to write an entry that already exists in the target backup location. The BACKUP_ENTRY_ALREADY_EXISTS error code indicates a naming collision -- typically because a backup with the same name was previously created and not removed, or because the same object appears more than once in the backup specification.
Impact
The backup operation fails entirely when this error occurs. No partial backup is written. This can disrupt scheduled backup routines, delay point-in-time recovery preparation, and leave gaps in your backup retention chain if not addressed promptly.
Common Causes
- Running a BACKUP command with the same backup name as an existing backup without using overwrite options
- A previously failed or incomplete backup left artifacts at the target path
- Specifying the same table or database multiple times in a single BACKUP statement
- Concurrent backup jobs targeting the same backup location
- Backup storage not being cleaned up properly between backup cycles
- Using shared backup storage (e.g., S3 or disk) where multiple ClickHouse instances write to the same path
Troubleshooting and Resolution Steps
Check whether a backup with the same name already exists:
SELECT * FROM system.backups WHERE name = 'my_backup';If the previous backup is no longer needed, remove it before retrying. For disk-based backups, delete the backup directory. For S3-based backups, remove the corresponding prefix:
rm -rf /var/lib/clickhouse/backups/my_backupIf you want to keep the existing backup, use a unique name for the new one. A common pattern is to include a timestamp:
BACKUP TABLE my_db.my_table TO Disk('backups', 'my_backup_2024_01_15_1200');Verify your BACKUP statement does not list the same table or database more than once, as this can also produce duplicate entries.
If the error stems from a failed previous backup, check for leftover lock files or incomplete data at the backup path and clean them up.
For concurrent backup scenarios, implement locking or scheduling to ensure only one backup job writes to a given location at a time.
Best Practices
- Include timestamps or unique identifiers in backup names to avoid collisions.
- Implement a cleanup policy that removes old backups after the retention period expires.
- Avoid running concurrent BACKUP commands that target the same storage location.
- Monitor the
system.backupstable to track backup status and detect failed or incomplete backups. - Use dedicated backup storage paths per cluster or instance when sharing storage infrastructure.
Frequently Asked Questions
Q: Can I overwrite an existing backup in ClickHouse?
A: ClickHouse does not natively support an "overwrite" option for backups. You need to remove the existing backup first and then create a new one with the same name.
Q: Does a failed backup leave files behind that could cause this error?
A: Yes. If a backup fails partway through, it may leave partial data at the target path. Cleaning up the leftover files before retrying will resolve the BACKUP_ENTRY_ALREADY_EXISTS error.
Q: Is it safe to delete a backup directory manually?
A: Yes, backup directories are self-contained. Deleting them does not affect the running ClickHouse server or its data. Just make sure no active RESTORE operation is reading from that backup.
Q: How do I automate backup cleanup?
A: Use a cron job or orchestration tool to periodically remove backups older than your retention policy. You can list backups from system.backups or simply manage files in your backup storage directory.