Home » Infrastructure » Unix » Resurrect a backup corrupted by FTP (ASCII) (Unix -> Windows)
Resurrect a backup corrupted by FTP (ASCII) [message #312852] |
Wed, 09 April 2008 22:55  |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
It's a reasonably common occurrence to corrupt a binary file when pulling it from Unix to Windows with the Windows FTP client.
The reason is because the Windows FTP client (and some others) default to ASCII mode, which translates Unix linefeed characters (CHR(10)) to Linefeed / Carriage Return ( CHR(10)+CHR(13) ).
Typically, you don't know you've done it until you try to use the file. Not so tragic if it's a JPEG photo of your mum, not so good if its a database backup.
This happend to us (backup, not photo of Mum).
Since the action of ASCII FTP is deterministic, it is actually reversible. Sadly you cannot just FTP it back in ASCII mode - I think that just blindly strips AND CHR(13) - which could occur naturally in a binary file. But below is a nice Perl script that does the job.
#!/usr/local/bin/perl -w
# File: unftp.pl
# Description: Restores a binary file corrupted by ASCII FTP
# from Unix to Windoze
# Syntax: unftp.pl infilename > outfilename
use strict;
my $chr;
my $prv;
my $cr = chr(13);
my $lf = chr(10);
my $crlf = chr(13) . chr(10);
my $iscr = 0;
my $fname = shift @ARGV || die "Expecting file name";
open (INP, "<$fname") || die $!;
while (read(INP, $chr, 65536) ) {
$chr = $cr . $chr if $iscr;
$iscr = 0;
if (substr($chr, length($chr)-1, 1) eq $cr) {
$chr = substr($chr, 0, length($chr)-1);
$iscr = 1;
}
$chr =~ s/$crlf/$lf/g;
print $chr;
}
|
|
|
|
|
|
|
|
|
|
|
Re: Resurrect a backup corrupted by FTP (ASCII) [message #390495 is a reply to message #378928] |
Fri, 06 March 2009 10:43  |
gregbahun
Messages: 1 Registered: March 2009 Location: McMaster University
|
Junior Member |
|
|
dgdot - THANK YOU!!!!!!!!!!!
to reiterate what dgdot said:
Hopefully this helps the next person searching for help on this issue: Simple solution. You'll need a decent hex editor. I used Hex Editor Neo. It has a good batch find/replace feature.
Search for the following string (hex bytes): "0d 0a" and replace it with "0a". Replace every instance in your file.
i'm a chemistry grad student who inadvertently transferred my raw NMR data via FTP and didn't realized the FTP client transferred the binary data as ASCII files. i was trying various NMR programs to process the data, and at first the programs all sucked. but, it was pointed the problem was the binary to ASCII conversion. i applied the method above suggested by dgdot and it worked like a charm. the only thing - there are text files in the folders from the NMR data. so, I did a search for those txt files in those folders, and made them all read only. after this, the hex editor neo did not change those files - but it did change everything else (in a bulk (or batch) method) and i am now able to open those nmr data files and process them and get good spectra.
holy crap...thank you!!!!!!!!!!!!!
|
|
|
Goto Forum:
Current Time: Mon Apr 07 14:42:55 CDT 2025
|