How to backup Windows folders to Linux (Raspberry Pi version)

This is a rather basic description about the steps required to automatically backup any Windows folder to linux systems. In this first version I'm using Raspberry Pi (but the idea should be the same on any Linux systems), with two external harddrive configured as software raid 1 system, but later I noticed it is not really practical, because the content of the harddrive can only be accessed from the Raspberry. *Updated version comes soon, using raid 1 hardware enclosure attached to Windows system where the linux part runs as virtual machine.* Details about how to perform each steps - for example to install an SSH or Samba server - can be found on the internet.

1. Preconditions

- Raspberry Pi is installed.
- User added.
- SSH enabled.
- Harddrives mounted as software raid 1 (or whatever method)


Optional steps follows, only required if we want to reach the backed up data from Windows, compromising the SSH security over Samba:

- Samba server installed and configured.
- Samba users added as normal system users. This enables Samba connection and SSH connections later. 

2.

Install Cygwin on the Windows computer with the following packages:

- Bash
- OpenSSH
- rsync

3.

Generate private/public SSH keys on the Cygwin console.

4.

Set up Raspberry Pi to allow passwordless SSH connection from Cygwin - together with normal password connection, to avoid locking out ourselves from the system if the passwordless SSH is not working. 

5.

Test the SSH connection from Cygwin bash prompt.

- The SSH connection might work differently from bash and with the rsync option. 

6.

Create a .bat file under C:\cygwin64\ called backup.bat with the following single line as content:

C:\cygwin64\bin\bash -l C:\cygwin64\home\\backup.sh

- This file will be scheduled to run from Windows’ Task Scheduler.
- The script will call Cygwin’s bash console and run the backup.sh script. All further configurations regarding the SSH login and backup folders are defined in the script file.
- <user> will depend on the logged in user. 

- The SSH connection might work differently from bash and with the rsync option. 

7.

Create a .sh file under

C:\cygwin64\home\<user>\

called backup.sh with the following content:

1) #!/bin/bash

2) fileName=backuplog_$(date -I).txt

3) echo $‘Backup started:\n’$(date -R) > $fileName (ENTER)

4) rsync -azhe "ssh -i .ssh/id_rsa" --stats /cygdrive/c/Users/<user>/<folder_to_backup> <user>@<hostname>:/<folder_at_backup_location>/ >> $fileName (ENTER)

5) echo $’Backup ended:\n’$(date -R) >> $fileName (ENTER)

- Do not type 1) 2) … and (ENTER), these are just showing what belongs to one line in the script file.

- There is one > in line 3) and double >> elsewhere. One > will create a new file (deleting the file content if already existing), two >> will append the information to the existing file.

- Replace <user> with the corresponding username on the Windows and Linux system, <hostname> with the host name of Raspberry Pi and <folder_to_backup> and <folder_at_backup_location>with the folder you would like to back up and where you would like to store the backup.

- If the <folder_to_backup> do not contain / at the end, then the folder itself together with its content will be created on the backup side. If there is / at the end, only the content of the folder will be created in the backup, but not the folder itself.

- The whole backup process is in step 4). Here the rsync command does the SSH login to the Linux system, coping the specified folder(s), creates the content of the log file etc.

- After every run the script will create a new backuplog_YYYY-MM-DD.txt file with some information about the backup process. 

8.

Run Windows’ Task Scheduler app and create a new basic task which can be triggered periodically at a certain time when the backup is to be performed. The program to run shall be the backup.bat file created in step 6)

9.

Every modification of the script can be done conveniently by modifying the backup.sh file. To test the operation, the script can be manually run from Cygwin’s bash prompt, the .bat file can be started manually from the file explorer in Windows or manually triggered from Task Scheduler.

No Code Website Builder