Skip to content

Instantly share code, notes, and snippets.

@enkore
Last active August 23, 2023 18:02
Show Gist options
  • Save enkore/14f7bd9f56d6cc17914a73345fd30fc4 to your computer and use it in GitHub Desktop.
Save enkore/14f7bd9f56d6cc17914a73345fd30fc4 to your computer and use it in GitHub Desktop.
"""
show-odd-cache.py
-----------------
Usage:
python show-odd-cache.py <repository> [<column to sort on>] [<number of entries to show>]
Prints a list of the twenty cache entries with the highest reference count.
This makes certain kinds of cache corruption (by bad drives, RAM etc) easy.
Columns: **refcount**, size (payload size), csize (size in repo)
Prepend a '-' to see the smallest values.
Tested on Borg 1.0.x, Borg 1.1.x; works with remote repos.
"""
import heapq
import sys
from binascii import hexlify
from borg.archiver import Archiver, with_repository, UMASK_DEFAULT
from borg.helpers import location_validator
class XArchiver(Archiver):
@with_repository(cache=True)
def show_odd_cache_entries(_, args, repository, cache, manifest, key, count, column):
fun = heapq.nsmallest if column.startswith('-') else heapq.nlargest
column = column.strip('-')
index = {
'refcount': 0,
'size': 1,
'csize': 2,
}[column]
contents = fun(count, cache.chunks.iteritems(), key=lambda k_r_s_c: k_r_s_c[1][index])
fmt = '%-64s %-16s %-16s %-16s'
print(fmt % ('Object ID', 'refcount', 'size', 'csize'))
for key, (refcount, size, csize) in contents:
print(fmt % (hexlify(key).decode(), refcount, size, csize))
class Args:
location = location_validator(archive=False)(sys.argv[1])
umask = UMASK_DEFAULT
remote_path = 'borg'
if __name__ == '__main__':
try:
column = sys.argv[2]
except IndexError:
column = 'refcount'
try:
count = int(sys.argv[3])
except IndexError:
count = 20
XArchiver().show_odd_cache_entries(Args, count=count, column=column)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment