Linux Backup to USB Drive using rsync: Western Digital 4TB Example
In this article, we will discuss how to backup data on a Linux system to a Western Digital 4TB external USB drive using the rsync command. We will cover key concepts, provide detailed context, and use subtitles, paragraphs, and code blocks to explain the process.
Why use rsync for Backup?
Rsync is a powerful command-line tool for Linux that can be used to synchronize files and directories between two locations. It is efficient, as it only transfers the differences between the source and destination, and it can preserve file attributes such as ownership, permissions, and timestamps. Rsync can also be used to backup data to a remote server or a local storage device such as an external USB drive.
Preparing the USB Drive
Before we can use the Western Digital 4TB USB drive for backup, we need to prepare it by formatting it to a Linux-compatible file system such as ext4 or NTFS. We can use the fdisk command to partition the drive and the mkfs command to format the partition.
sudo fdisk /dev/sdb
# create a new partition
n
# select the default partition type (primary)
p
# select the default first sector
1
# select the default last sector (use the default value or specify the size)
# format the partition
sudo mkfs.ext4 /dev/sdb1
Once the drive is formatted, we can mount it to a directory in the Linux file system. For example, we can create a directory called /mnt/usb and mount the drive to it:
sudo mkdir /mnt/usb
sudo mount /dev/sdb1 /mnt/usb
Using rsync for Backup
Now that the USB drive is prepared, we can use rsync to backup data to it. The basic syntax of the rsync command is:
rsync [OPTIONS] SOURCE DESTINATION
In our case, the source is the directory we want to backup, and the destination is the USB drive. We can use the following command to backup the /home directory to the USB drive:
rsync -aAXv --exclude={"/home/*/.cache/*","/home/*/.thumbnails/*"} /home /mnt/usb
Here, the -aAX options tell rsync to preserve the file attributes, symbolic links, and permissions. The --exclude option is used to exclude certain directories from the backup. In this case, we exclude the .cache and .thumbnails directories in each user's home directory to save space on the USB drive.
Automating the Backup
To automate the backup process, we can create a script that runs the rsync command at regular intervals. For example, we can create a script called backup.sh with the following content:
#!/bin/bash
rsync -aAXv --exclude={"/home/*/.cache/*","/home/*/.thumbnails/*"} /home /mnt/usb
We can then make the script executable and schedule it to run at a specific time using the cron command.
References
- Rsync manual page: https://linux.die.net/man/1/rsync
- Formatting a USB drive for Linux: https://www.linux.com/training-tutorials/how-format-usb-flash-drive-linux/
- Scheduling tasks with cron: https://www.linode.com/docs/guides/scheduling-tasks-with-cron/
This article has provided a detailed explanation of how to backup data on a Linux system to a Western Digital 4TB external USB drive using the rsync command. By following the steps outlined in this article, you can ensure that your data is safely backed up and easily recoverable in case of a system failure or data loss.