Can I use rsync -a --delete /var/cache/apt ~/apt_backup to backup that directory? The problem I'm encountering is that it contains lock and partial with bad permissions, which confuses rsync. How can I tell rsync to ignore those?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Yes, you can use rsync to back up /var/cache/apt, but you should exclude the problematic directories (lock and partial) to avoid permission issues. Here’s how to do it properly:
Solution: Exclude lock and partial with --exclude
bash
rsync -a --delete --exclude='lock' --exclude='partial' /var/cache/apt/ ~/apt_backup/
-a → Archive mode (preserves permissions, ownership, timestamps, etc.)
--delete → Removes files in the destination that no longer exist in the source
--exclude='lock' → Skips the lock directory
--exclude='partial' → Skips the partial directory
Alternative: Use --exclude={pattern1,pattern2}
If you prefer a more compact syntax:
bash
rsync -a --delete --exclude={'lock','partial'} /var/cache/apt/ ~/apt_backup/
Why Exclude lock and partial?
lock → Contains lock files that prevent concurrent APT operations. These are temporary and not needed for a backup.
partial → Stores incomplete downloads. Since these are transient files, they don’t need to be backed up.
Bonus: Restore the Backup Later
If you ever need to restore the cache (e.g., after a system reinstall), you can reverse the process:
bash
sudo rsync -a --delete ~/apt_backup/ /var/cache/apt/
(Note: You may need sudo to write to /var/cache/apt.)
Alternative: Use apt-offline for a Cleaner Backup
If you want a more structured way to back up APT metadata, consider using apt-offline:
bash
sudo apt-get install apt-offline
apt-offline set offline.sig --update
apt-offline get offline.sig --bundle ~/apt_offline_backup.zip
This creates a compressed bundle of the package lists without temporary files.
Conclusion
Your original approach works, but excluding lock and partial makes it cleaner and avoids permission issues. The rsync method is efficient and widely used for backups.