How to backup Windows folders to Linux
Updated description using a Windows system as host, the harddrive is attached to this with a hardware raid 1 enclosure and the Linux part is running as virtual machine under Windows.
- Raspberry Pi or Ubuntu is installed
- Optional, if we want to access the backup location with Samba. Create normal system users for Samba. This enables Samba connection and SSH connections later with the same username
• sudo useradd -m -s /bin/bash user1
• sudo passwd user1 - SSH is installed and enabled. Allow passwordless SSH connection together with normal password connection - this is to avoid locking out ourselves from the system if the passwordless SSH is not working. Can be changed later. (https://help.ubuntu.com/community/SSH/OpenSSH/Configuring#disable-password-authentication) Relevant changes in the default config file:
• PubkeyAuthentication yes
• RSAAuthentication yes
• AuthorizedKeysFile .ssh/authorized_keys /etc/ssh/%u/authorized_keys
• PasswordAuthentication yes - Samba server installed and configured. Users added already, smbuseradd is not used.
• Follow (https://magpi.raspberrypi.com/articles/samba-file-server) for details. Relevant changes in the default config file:
• #### Debugging/Accounting ####
• client min protocol = SMB2
• client max protocol = SMB2
• ############ Misc ############
• usershare allow guests = no
• #==================== Share Definitions ====================
• ;[homes]
• ;comment = Home Directories
• ;browseable = no
• ;read only = yes
• ;create mask = 0777
• ;directory mask = 0777
• ;valid users = user1, user2
• [user1Backup]
• Comment = User1 Backup folder
• path = /mnt/raid1/0_User1_backup
• browseable = yes
• writeable = yes
• only guest = no
• create mask = 0777
• directory mask = 0777
• guest ok = no
• valid users = user1, user2 - Harddrive mounted as raid 1 via software or hw raid enclosure
• For software raid with mdadm follow (https://magpi.raspberrypi.com/articles/build-a-raspberry-pi-nas) or (https://www.studiopieters.nl/raspberry-pi-raid-storage/). - Install Cygwin on the Windows computer with the following packages:
• Bash
• OpenSSH
• rsync - Generate private/public SSH keys on the Cygwin console. Follow (https://help.ubuntu.com/community/SSH/OpenSSH/Keys) for details.
• Transfer the public key to the linux system with "ssh-copy-id user@server" - Test the SSH connection from Cygwin bash prompt
• The SSH connection might work differently from bash and with the rsync option - 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\<user>\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 on Windows - Create a .sh file under C:\cygwin64\home\<user>\ called backup.sh with the following content:
1) #!/bin/bash (ENTER)
2) fileName=backuplog_$(date -I).txt (ENTER)
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_of_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 actual user name, <hostname> with the host name of Raspberry Pi or Ubuntu and <folder_to_backup…> with the folder you would like to back up and where you would like to store the backup file
• 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.
• After every run the script will create a new backuplog_YYYY-MM-DD.txt file with some information about the backup process - Run Windows’ Task Scheduler app and create a new basic task which will be triggered at a certain time period when the backup is to be performed. The program to to run shall be the backup.bat file, created in step 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 or the .bat file execution can be manually triggered from Task Scheduler.
- If using VirtualBox, the following DOS script can autostart the VM as a startup task:
@echo off
if "%~2" == "" goto ERROR
if NOT "%~3" == "" goto ERROR
:STARTVM
echo ---Shutting down "%~1" ...
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm %1 poweroff
rem This might return an error if the VM is not running.
echo ---Restoring snapshot %2 on %1 ...
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" snapshot %1 restore %2
if NOT %errorlevel%==0 goto VMERROR
echo ---Starting VirtualBox...
rem start "VM starter" /d "C:\Program Files\Oracle\VirtualBox\" VBoxHeadless.exe -s %1
rem VBoxHeadless.exe keeps running until the VM is running. Better to use VBoxManage.exe
start "VM Starter" /d "C:\Program Files\Oracle\VirtualBox\" /wait VBoxManage.exe startvm %1 --type headless
rem "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm %1 --type headless
rem Starting without start "WindowName" /d "folder" /wait [command] also work.
if NOT %errorlevel%==0 goto VMERROR
echo ---VirtualBox status:
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showvminfo %1 | findstr /i "state *running"
echo --- ... script %0 completed.
goto END
:ERROR
echo.
echo %0 [virtualbox-name] [snapshot-name]
echo The script must be called with two arguments.
echo.
echo The VirtualBox will be shut down first, then restarted
echo with the Snapsot restored.
goto END
:VMERROR
echo --- ... script %0 FAILED to complete.
:END
-
-
-
-
-
-
-
-
-
-