Monday, April 29, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 569  / 1 Year ago, sun, january 29, 2023, 3:47:25

I had a disk with a lot of surface errors that I used until a couple days ago when I got my new ssd. I want to know how I could completely copy over my home partition but ignore all errors while doing so, as it probably has a few hundred bad clusters in the beginning and it would literally take hours just clicking "ignore" on gparted. Please guys, I have like 85gb in there that would be an absolute pain to recreate or manually copy all files over and correct the permissions of.


More From » partitioning

 Answers
6

A. To copy the files into a "flat" directory



If it is possible for the script to create at least a file list, but it should ignore the problems that might occur in copying them, it should work.



In case of duplicates, it will save the duplicates as duplicate_1_<filename>, duplicate_2_` etc.



I used:



try
<copy_command>
except Exception
pass


Which covers the widest possible range of errors.



The script:



#!/usr/bin/env python3

import shutil
import os

sourcedir = "/path/to/source"
dest_dir = "/path/to/destination"

for root, dirs, files in os.walk(sourcedir):
for name in files:
subject = root+"/"+name
n = 1; name_orig = name
while os.path.exists(dest_dir+"/"+name):
name = "duplicate_"+str(n)+"_"+name_orig
n = n+1
try:
newfile = dest_dir+"/"+name
shutil.copy(subject, newfile)
except Exception:
pass


Copy it into an empty file, save it as giveit_ashot.py and set the sourcedir and destination.



Run it by:



python3 /path/to/giveit_ashot.py


B. If the directory structure is important



then use the script below, it first tries to (re-)create the directories (top- down), then copies the files into the structure as much as possible. Again: it must at least be possible to create a file / directory list.



#!/usr/bin/env python3

import shutil
import os

sourcedir = "/path/to/source"
dest_dir = "/path/to/destination"

dirdata = []
filedata = []

for root, dirs, files in os.walk(sourcedir):
for dir in dirs:
absolute = root+"/"+dir
relative = absolute.replace(sourcedir, "")
dirdata.append([absolute, dest_dir+relative, len(absolute.split("/"))])
for file in files:
abs_file = root+"/"+file
rel_file = abs_file.replace(sourcedir, "") # source
filedata.append((abs_file, dest_dir+rel_file)) # destination

dirdata.sort(key=lambda x: x[2])

for item in dirdata:
try:
if not os.path.exists(item[1]):
os.mkdir(item[1])
except Exception:
pass

for item in filedata:
try:
shutil.copy(item[0], item[1])
except Exception:
pass

[#23161] Monday, January 30, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
exceeeelh

Total Points: 21
Total Questions: 109
Total Answers: 120

Location: Marshall Islands
Member since Wed, Jan 5, 2022
2 Years ago
exceeeelh questions
Sun, Nov 20, 22, 17:08, 1 Year ago
Sat, Jan 1, 22, 08:04, 2 Years ago
Wed, May 12, 21, 05:11, 3 Years ago
;