Backup Linux files to Windows 11 Desktop using WinSCP and PowerShell over IPv6

Environment:

  • VPS running RockLinux (CentOS compat)
  • Network interface: ens192
  • Windows 11 (Task Scheduler)
  • WinSCP 5.17.10 (5.18 didn’t work, I forget why).
  • IPv6 network provided by host
    • I’ll be using a sample CIDR fd09:1b8e:675a:219c:: for this tutorial.
  • One IPv6 was provisioned to server over DHCP.
    • fd09:1b8e:675a:219c::1
  • I created two new IPv6 addresses at host.
    • fd09:1b8e:675a:219c::2
    • fd09:1b8e:675a:219c::3

Add IPv6 address to server:

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-ens192

Add the following line to the end of the file:

IPV6ADDR_SECONDARIES="fd09:1b8e:675a:219c::2/64   fd09:1b8e:675a:219c::3/64"

Reboot host.

You should now see the networks available:

$ ip addr | grep inet6
...
inet6 2607:f1c0:1800:802a::2/64 scope global noprefixroute 
inet6 2607:f1c0:1800:802a::3/64 scope global noprefixroute
...

Create a folder where you want to store your data (C:\LinuxBackup).

Use notepad or your favorite editor to create a PowerShell file backup_files.ps1 in C:\LinuxBackup

On the Linux hosts, get host RSA key:

$ sudo ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key
3072       SHA256:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1EM8 root@localhost (RSA)

I configured a new firewall rule to allow port 22 access on the 2607:f1c0:1800:802a::2 IP, as I plan to lock down the whole server on port 22 except IP 2607:f1c0:1800:802a::2.

Since I want to use a hostname to connect to my server, I had to add an entry in Windows hosts file:

Edit C:\Windows\system32\drivers\etc\hosts and add the following line:

[2607:f1c0:1800:802a::2] scp.mydomain.com

Contents of the backup_files.ps1 file (I included my default environment because I’m not sure how that’s handled in Task Scheduler):

$ENV:PATH = 'C:\Program Files (x86)\VMware\VMware Player\bin\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\dotnet\;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\Users\Tom\AppData\Local\Microsoft\WindowsApp

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

$SessionOptions = New-Object WinSCP.SessionOptions -Property @{
UserName = 'server_username'
Password = 'server_password'
HostName = 'scp.mydomain.com'
SshHostKeyFingerprint = 'ssh-rsa 2048 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1EM8'
PortNumber = 22
}

$transferOptions = New-Object WinSCP.TransferOptions

# FileMask = Newer than 2 days
$transferOptions.FileMask = "*>=2D"

$session = New-WinSCPSession -SessionOption $SessionOptions

Receive-WinSCPItem -Path '/home/tom/files_for_backup/' -Destination 'C:\LinuxBackup\' -TransferOptions $transferOptions

Remove-WinSCPSession -WinSCPSession $session

The next step is to create the task in Task Scheduler:

  • Open Windows Task Scheduler
  • Right click “Task Scheduler Library” and select “New Folder…”. I called mine Tom.
  • Right click new folder and choose “Create Task…”.
  • In the popup window, add Name “Backup Linux”
  • Under “Security Options”, choose “Run whether user is logged in or not” and “Run with highest privileges”. 
  • Go to the “Actions” tab, type Powershell.exe, and put the path to the script in the Arguments box: 
-command & 'C:\LinuxBackup\backup_files.ps1'
  • Go to “Trigger” tab and create a New trigger. I chose “At 12:00 PM every day”.
  • Click “OK”. This will prompt you to enter your credentials.

Comments

Popular posts from this blog