WEBKT

Use Raspberry Pi to build a local file backup and synchronization system

71 0 0 0

Use Raspberry Pi to build a local file backup and synchronization system

Do you want to back up your important files at home? Do you want to synchronize files between multiple devices without relying on cloud services? Using Raspberry Pi to build a local file backup and synchronization system is a cost-effective and reliable solution. This article will guide you step by step through the entire process.

1. Hardware Preparation

First, you need to prepare the following hardware:

  • Raspberry Pi: Any model of Raspberry Pi will do, but it is recommended to use Raspberry Pi 4 or later for better performance.
  • MicroSD card: At least 16GB, used to install the operating system.
  • External hard drive: Used to store backup files, the capacity depends on the amount of data you need to back up. It is recommended to use a USB 3.0 interface for faster transfer speeds.
  • Raspberry Pi power supply: Make sure the power supply can provide enough power.
  • Network cable (optional): It is recommended to use a wired network connection for a more stable and faster connection.

2. Install the Operating System

  1. Download the Raspberry Pi Imager tool from the official website: https://www.raspberrypi.com/software/
  2. Install and run the Raspberry Pi Imager tool.
  3. Insert the MicroSD card into the computer.
  4. In the Raspberry Pi Imager tool, select the operating system. It is recommended to use Raspberry Pi OS (formerly Raspbian).
  5. Select the MicroSD card as the target device.
  6. Click "Write" to start burning the operating system to the MicroSD card.
  7. After the burning is complete, insert the MicroSD card into the Raspberry Pi.

3. Configure Raspberry Pi

  1. Connect the Raspberry Pi to the monitor, keyboard, and mouse.
  2. Connect the Raspberry Pi to the network cable (or connect to Wi-Fi).
  3. Power on the Raspberry Pi.
  4. Follow the on-screen prompts to complete the initial configuration, including setting the user name, password, and network.
  5. Enable SSH for remote access. Open the terminal and enter the following command:
    
    

sudo raspi-config
Select "Interface Options" -> "SSH" -> "Yes" to enable SSH. 6. Update the system. Open the terminal and enter the following commands:
sudo apt update
sudo apt upgrade
```

4. Install and Configure the Backup Software

There are many backup software options available, here are two commonly used options:

  • rsync: A powerful command-line tool for file synchronization and backup. It is suitable for users who are familiar with the command line.
  • Duplicati: A graphical interface backup software that supports multiple backup destinations and encryption. It is suitable for users who prefer a graphical interface.

4.1 Using rsync

  1. Install rsync. Open the terminal and enter the following command:
    
    

sudo apt install rsync
2. Connect the external hard drive to the Raspberry Pi. 3. Create a mount point for the external hard drive. First, create a directory:
sudo mkdir /mnt/backup
Then, find the UUID of the external hard drive:
sudo blkid
You will see output similar to the following:
/dev/sda1: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" TYPE="ext4"
Replace `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx` with the actual UUID of your external hard drive. Edit the `/etc/fstab` file:
sudo nano /etc/fstab
Add the following line to the end of the file:
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /mnt/backup ext4 defaults,auto,users,rw 0 0
Save and close the file. Then mount the external hard drive:
sudo mount -a
4. Create a backup script. Create a new file named `backup.sh`:
sudo nano backup.sh
Add the following content to the file:
#!/bin/bash

Source directory (the directory you want to back up)

SOURCE_DIR=/home/pi/Documents

Destination directory (the directory on the external hard drive to store backups)

DEST_DIR=/mnt/backup/Documents_backup

rsync options

OPTIONS="-avz --delete"

Perform the backup

rsync $OPTIONS $SOURCE_DIR $DEST_DIR

Output the backup completion message

echo "Backup completed at $(date)"
Modify `SOURCE_DIR` and `DEST_DIR` to the actual directories you want to back up. Save and close the file. 5. Give the script execution permissions:
sudo chmod +x backup.sh
6. Run the backup script manually:
./backup.sh
7. Set up automatic backups using cron. Edit the cron table:
crontab -e
Add the following line to the end of the file to perform backups every day at 2:00 AM:
0 2 * * * /home/pi/backup.sh
```
Save and close the file.

4.2 Using Duplicati

  1. Install Duplicati. Download the latest version of Duplicati from the official website: https://www.duplicati.com/download/
  2. Install Duplicati using the following commands:
    
    

sudo apt install ./duplicati_2.0.x.x-x_all.deb
```
Replace duplicati_2.0.x.x-x_all.deb with the actual file name.
3. Run Duplicati. Open the browser and enter http://localhost:8200.
4. Follow the on-screen prompts to create a new backup task.
5. Select the external hard drive as the backup destination.
6. Select the directories you want to back up.
7. Set up the backup schedule.
8. Configure encryption (optional).
9. Start the backup.

5. Best Practices

  • Regularly check the backup status: Make sure the backup is running correctly and the backup files are complete.
  • Test the backup: Regularly restore some files from the backup to ensure that the backup is working properly.
  • Use a reliable external hard drive: Choose a high-quality external hard drive to ensure data security.
  • Keep the Raspberry Pi secure: Change the default password and enable firewall to prevent unauthorized access.
  • Consider offsite backups: In addition to local backups, consider backing up your data to an offsite location, such as a cloud service or another physical location, for added protection.

Conclusion

Using Raspberry Pi to build a local file backup and synchronization system is a practical and cost-effective solution. By following the steps in this article, you can easily set up a reliable backup system to protect your important files. Whether you choose rsync or Duplicati, remember to follow the best practices to ensure data security and reliability.

Tech DIYer Raspberry PiFile BackupSynchronization

评论点评