The Input/output error (5) in rsync typically indicates physical media failure, corrupted filesystem metadata, or a loose USB connection rather than a software bug. Resolving this requires verifying the drive’s health with dmesg, checking for bad sectors using badblocks on an unmounted partition, and ensuring the filesystem is clean with e2fsck. If the hardware is intact, retrying the transfer with specific flags often bypasses transient I/O glitches.
Understanding the Error Code
When rsync encounters an Input/Output error, it means the operating system cannot read from or write to the specified path. This is distinct from permission errors or missing files. On Linux systems, this error code (errno 5) is usually fatal for the current file being processed. It suggests that the kernel has detected a problem at the block device level or the filesystem driver level. Ignoring this error by simply restarting rsync without diagnosis often leads to partial transfers or data corruption, especially if the underlying issue is physical damage to the USB drive.
Physical Connection Issues
Before diving into complex diagnostics, rule out simple connectivity problems. USB drives are prone to disconnecting due to insufficient power, especially when connected via hubs or front-panel ports on desktops. A loose cable can cause intermittent drops that manifest as I/O errors during large file transfers. Try connecting the drive directly to a rear USB port on the motherboard, which typically provides more stable power delivery. Additionally, inspect the USB connector for bent pins or debris. If the drive makes clicking noises or spins up and down repeatedly, the hardware is likely failing, and no software fix will recover the data safely.
Diagnostics and Verification
If the connection seems stable, you must verify the state of the filesystem. The first step is to check the kernel ring buffer for hardware-related messages. Run the following command immediately after the error occurs:
dmesg | tail -n 20
Look for messages containing I/O error, sector, or remounting. If you see repeated sector errors, the drive has physical bad blocks. In this scenario, attempting further writes can destroy data permanently. You should stop all operations and consider professional data recovery services if the data is critical. If the logs show only transient disconnects or permission issues, the filesystem structure might be corrupted but the media itself may be salvageable.
Checking Filesystem Integrity
Once you have confirmed that the hardware is not physically failing, check the filesystem integrity. This must be done on an unmounted partition to prevent further corruption. Identify your USB drive’s partition name using lsblk or fdisk -l. For example, if the partition is /dev/sdb1, run:
sudo e2fsck -f /dev/sdb1
This command forces a full check even if the filesystem appears mounted. Follow the prompts to fix any inconsistencies. If e2fsck reports numerous errors, it may repair the directory structures, allowing rsync to proceed. However, if the errors are widespread, the drive is nearing end-of-life. After fixing the filesystem, remount the drive and try the sync again.
Safe Recovery Strategies
If the drive passes basic checks but rsync still fails on specific files, you can use strategies to isolate and skip problematic data. Standard rsync stops on the first error, which can halt entire backups. Using the --ignore-errors flag allows the process to continue despite individual file failures. While this risks incomplete data, it ensures that healthy files are preserved. Combine this with --log-file to track exactly which files caused the errors.
rsync --ignore-errors --log-file=sync_log.txt -avz /source/ /destination/
Another effective technique is to limit the transfer size or use a different protocol. Sometimes, the USB controller struggles with large sequential writes. Breaking the job into smaller chunks by syncing directories individually can help identify if a specific folder is causing the bottleneck. Additionally, ensure that the destination drive has sufficient free space and is not nearly full, as low disk space can sometimes trigger unexpected I/O behaviors.
Alternative Tools for Unstable Media
For heavily damaged drives, standard tools like cp or rsync may be too aggressive. Consider using ddrescue, which is designed to recover data from failing media. It maps out bad sectors and skips them, creating an image file that can later be analyzed. This approach prioritizes data extraction over speed, making it ideal for critical recovery scenarios. Always create a backup image before attempting repairs.
If you need to manage timestamps carefully during these difficult transfers, you might also find value in understanding how rsync handles sub-directories and modification times, as detailed in our guide on rsync: Not Iterating Sub-Directories - Modify Timestamp?. Furthermore, ensuring your destination environment is properly configured, similar to troubleshooting Unable to Submit Files via Rsync on Ubuntu VPS, can sometimes resolve ambiguous connectivity issues.
Frequently Asked Questions
Can I use badblocks on a mounted drive?
No, running badblocks on a mounted partition is dangerous and can lead to severe data corruption. The filesystem caches data, and writing random patterns over active blocks can overwrite valid files. Always unmount the drive before performing any destructive tests.
What does errno 5 mean in Linux?
Errno 5 corresponds to EIO, which stands for Input/Output error. It indicates that a physical device error occurred during a read or write operation. This is distinct from EACCES (permission denied) or ENOENT (file not found).
How do I skip bad files during rsync?
Use the --ignore-errors flag to allow rsync to continue processing other files even if one fails. You can also use --partial to keep partially transferred files, though this does not guarantee they are usable. Always log the output to identify skipped files later.
Is rsync --retry a valid option?
No, rsync does not have a built-in --retry flag. To automatically restart failed transfers, you should wrap the command in a shell loop or use a tool like syncthing or rclone which have native retry mechanisms. Writing a simple bash loop is the most common workaround.
Why does my USB drive disconnect randomly?
Random disconnections are often caused by insufficient power supply or faulty cables. USB 3.0 drives require more power than older ports provide. Try using a powered USB hub or connecting directly to the motherboard. Also, check for kernel updates that might address USB controller bugs.