begin;

-- make temporary table with all backup ids
select backups.id into temporary all_backup_ids from files
join shares on files.shareid = shares.id
join backups on backups.hostid = shares.hostid and backups.num = files.backupnum
where files.shareid = shares.id
group by backups.id, files.shareid, files.backupnum
;

-- fix backups which are deleted and have size anyway 
update backups set inc_deleted = false where id not in (select id from all_backup_ids) and size > 0 ;

-- fix parts numbering

update backups set parts = 0 where parts > 0 and id not in (select backup_id from backup_parts) ;
