Skip to content

Instantly share code, notes, and snippets.

@oltodosel
Created March 9, 2020 17:30
Show Gist options
  • Save oltodosel/321703db41efcfd4a6d6226cee7489e2 to your computer and use it in GitHub Desktop.
Save oltodosel/321703db41efcfd4a6d6226cee7489e2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os, shlex, subprocess
from tqdm import tqdm
# ddrescue directories instead of a whole drive
# creates directories recursively and ddrescues files into them
def fs (line):
import subprocess
PIPE = subprocess.PIPE
return subprocess.Popen(line, shell=isinstance('', str),bufsize=-1, stdin=PIPE, stdout=PIPE,stderr=subprocess.STDOUT, close_fds=True).stdout.read().decode("utf8", "ignore")
#############################################################
# copy here
pth_dest = '/mnt/1/2/'
# list of directories
pths = '''
/mnt/2/Music
file:///mnt/2/video/
file:///mnt/2/2see
file:///mnt/2/abooks
/mnt/2/backup/
'''
# don't scrap or retry. Best for a first run
first_run = True
# save list of successfully copies files to skip on successive runs
done_files_fn = '/mnt/1/2/done_files_null_seped'
#############################################################
try:
done_files_data = [ x for x in open(done_files_fn).read().split('\0') if len(x) ]
except:
done_files_data = []
if pth_dest[-1] != '/':
pth_dest += '/'
pths = [ x.strip() for x in pths.split('\n') if len(x.strip()) ]
for pth in pths:
pth = pth.replace('file://', '')
if pth[-1] != '/':
pth += '/'
file_list = fs('find %s -type f -print0' % shlex.quote(pth))
# for errors when using `find`
if file_list[0:5] == 'find:':
print('Error getting file-list in %s' % pth)
continue
print(pth)
file_list = [ x for x in file_list.split('\0') if len(x) ]
tmp_count = len(file_list)
file_list = [ x for x in file_list if x not in done_files_data ]
tmp_count = '%d(%d)' % (len(file_list), tmp_count)
if not len(file_list):
continue
for f in tqdm(file_list, desc=tmp_count):
# ~print('\n\n\n', f)
file_path = f.rsplit('/', 1)[0]
os.makedirs(pth_dest + file_path, exist_ok=True)
if first_run:
subprocess.call(['ddrescue', '-q', '--max-read-errors=10', '-n', f, pth_dest + f, pth_dest + f + '_map_ddrescue'])
else:
subprocess.call(['ddrescue', '-vvvv', '-r', '5', f, pth_dest + f, pth_dest + f + '_map_ddrescue'])
subprocess.call(['ddrescue', '-vvvv', '-r', '5', '-R', f, pth_dest + f, pth_dest + f + '_map_ddrescue'])
# log fully copied files
if not subprocess.call(['ddrescuelog', '-D', pth_dest + f + '_map_ddrescue']):
with open(done_files_fn, 'a') as done_files_tmp:
done_files_tmp.write(f + '\0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment