Configure backup retention to prevent storage exhaustion:
// Keep last 10 backups
client.set_backup_retention_policy(&10, &0, &true);
// Keep backups for 30 days
client.set_backup_retention_policy(&0, &2592000, &true);
// Combined: 5 backups OR 7 days
client.set_backup_retention_policy(&5, &604800, &true);Without configuration, the system uses:
- max_backups: 5
- max_age_seconds: 0 (unlimited)
- auto_cleanup_enabled: true
This means: Keep last 5 backups, no age limit, automatic cleanup enabled.
Keep 10 backups or 30 days (whichever is more restrictive):
let admin = Address::generate(&env);
env.mock_all_auths();
client.set_admin(&admin);
// Configure retention
client.set_backup_retention_policy(&10, &2592000, &true);
// Create backups normally
let backup_id = client.create_backup(&String::from_str(&env, "Daily backup"));
// Automatic cleanup happens hereUnlimited backups, 7-day age limit:
client.set_backup_retention_policy(&0, &604800, &true);Keep only 3 most recent backups:
client.set_backup_retention_policy(&3, &0, &true);Disable automatic cleanup, trigger manually:
// Disable auto cleanup
client.set_backup_retention_policy(&5, &0, &false);
// Create multiple backups
for i in 0..10 {
client.create_backup(&String::from_str(&env, "Backup"));
}
// Manually trigger cleanup when ready
client.set_backup_retention_policy(&5, &0, &true);
let removed = client.cleanup_backups(); // Returns number removedArchive backups to protect from automatic cleanup:
// Create backup before major operation
let backup_id = client.create_backup(&String::from_str(&env, "Pre-upgrade v2.0"));
// Archive to protect from cleanup
client.archive_backup(&backup_id);
// This backup will never be automatically cleaned uppub fn set_backup_retention_policy(
env: Env,
max_backups: u32, // 0 = unlimited
max_age_seconds: u64, // 0 = unlimited
auto_cleanup_enabled: bool,
) -> Result<(), QuickLendXError>Requires: Admin authorization
pub fn get_backup_retention_policy(env: Env) -> BackupRetentionPolicyReturns:
BackupRetentionPolicy {
max_backups: u32,
max_age_seconds: u64,
auto_cleanup_enabled: bool,
}pub fn cleanup_backups(env: Env) -> Result<u32, QuickLendXError>Requires: Admin authorization
Returns: Number of backups removed
// Common time periods in seconds
let one_hour = 3600;
let one_day = 86400;
let one_week = 604800;
let one_month = 2592000; // 30 days
let three_months = 7776000; // 90 days- ✅ Active backups older than
max_age_seconds(if configured) - ✅ Oldest active backups beyond
max_backups(if configured)
- ❌ Archived backups (never cleaned automatically)
- ❌ Backups within age limit
- ❌ Backups within count limit
- Automatically: After each
create_backup()call (ifauto_cleanup_enabled = true) - Manually: When admin calls
cleanup_backups()
Monitor these events for audit trail:
// Policy updated
(symbol_short!("ret_pol"), max_backups, max_age_seconds, auto_cleanup_enabled, timestamp)
// Backups cleaned
(symbol_short!("bkup_cln"), removed_count, timestamp)match client.try_set_backup_retention_policy(&10, &0, &true) {
Ok(_) => {
// Policy updated successfully
}
Err(QuickLendXError::NotAdmin) => {
// Caller is not admin
}
Err(e) => {
// Other error
}
}- Set reasonable limits based on storage capacity
- Archive important backups before major operations
- Monitor backup count regularly
- Test restoration procedures
- Document backup IDs for critical backups
- Set unlimited retention without monitoring
- Disable cleanup without a plan
- Forget to archive pre-upgrade backups
- Ignore cleanup events
- Assume backups are always valid (validate first)
#[test]
fn test_my_retention_policy() {
let env = Env::default();
let contract_id = env.register(QuickLendXContract, ());
let client = QuickLendXContractClient::new(&env, &contract_id);
// Set up admin
let admin = Address::generate(&env);
env.mock_all_auths();
client.set_admin(&admin);
// Configure retention
client.set_backup_retention_policy(&3, &0, &true);
// Create backups
for i in 0..5 {
client.create_backup(&String::from_str(&env, "Backup"));
}
// Verify only 3 remain
let backups = client.get_backups();
assert_eq!(backups.len(), 3);
}Check:
- Is
auto_cleanup_enabled = true? - Are backups archived?
- Are limits configured (not 0)?
let policy = client.get_backup_retention_policy();
println!("max_backups: {}", policy.max_backups);
println!("max_age_seconds: {}", policy.max_age_seconds);
println!("auto_cleanup_enabled: {}", policy.auto_cleanup_enabled);Solution: Adjust limits or archive important backups
// Increase limits
client.set_backup_retention_policy(&20, &0, &true);
// Or archive specific backups
client.archive_backup(&important_backup_id);Check: Are you admin?
let admin = client.get_admin();
// Verify you're using the correct admin addressIf you have existing code using the old cleanup function:
// Old way (deprecated)
BackupStorage::cleanup_old_backups(&env, 5)?;
// New way
client.set_backup_retention_policy(&5, &0, &true);
// Cleanup happens automatically on create_backup()
// Or manually:
client.cleanup_backups()?;-
Batch Operations: If creating many backups, disable auto-cleanup and trigger manually:
client.set_backup_retention_policy(&5, &0, &false); // Create many backups... client.set_backup_retention_policy(&5, &0, &true); client.cleanup_backups()?;
-
Monitor Counts: Check backup count before creating new ones:
let count = client.get_backups().len(); if count > 100 { // Consider manual cleanup or adjusting policy }
-
Archive Strategically: Archive only truly critical backups to avoid accumulation
- Only admin can configure retention policy
- Only admin can trigger manual cleanup
- Archived backups are protected from automatic cleanup
- All operations emit events for audit trail
- Validation occurs before backup restoration
- Full Documentation:
docs/contracts/backup.md - Implementation Details:
BACKUP_RETENTION_IMPLEMENTATION.md - Security Analysis:
BACKUP_RETENTION_SECURITY.md - Feature Summary:
FEATURE_331_SUMMARY.md
For issues or questions:
- Check the full documentation
- Review test cases in
src/test.rs - Examine event logs for audit trail
- Verify admin authorization
Quick Start Version: 1.0
Last Updated: February 23, 2024