Wsl与rsync

安装WSL

参考: https://learn.microsoft.com/zh-cn/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package

https://learn.microsoft.com/zh-cn/windows/wsl/setup/environment?source=recommendations

https://superuser.com/questions/1111591/how-can-i-ssh-into-bash-on-ubuntu-on-windows-10

https://zhuanlan.zhihu.com/p/103578899

https://zhuanlan.zhihu.com/p/47733615

https://github.com/microsoft/vscode-remote-release/issues/1896

https://askubuntu.com/questions/1379425/system-has-not-been-booted-with-systemd-as-init-system-pid-1-cant-operate

wsl --install -d Ubuntu

# 编辑/etc/init.wsl
#! /bin/sh
/etc/init.d/cron $1
/etc/init.d/ssh $1
/etc/init.d/supervisor $1

# windows运行 shell:startup

#  Ubuntu.vbs
Set ws = CreateObject("Wscript.Shell")
ws.run "wsl -d Ubuntu-18.04 -u root /etc/init.wsl start", vbhide

# 重启 wsl 服务
服务 -> LxssManager
# 设置 LxssManager 自启
# 注册表 -> HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourServiceName
# 修改 Start 的值
# 自动(延迟启动): 2
# 自动: 2
# 手动: 3
# 已禁用: 4


# 保持wsl一开机启动
# wsl-startup.vbs
# change '<Distro>' to the distro name you are using.
set ws=wscript.CreateObject("wscript.shell")
ws.run "wsl -d <Distro>", 0

# 隐藏界面运行程序 windows
# programxxx.vbs
Set WshShell = CreateObject("WScript.Shell")

' Change the current directory
WshShell.CurrentDirectory = "D:\data\proxy"

' Run your program
WshShell.Run "sing-box.exe run", 0

Set WshShell = Nothing

# 杀掉该程序
taskkill /IM sing-box.exe /F

使用rsync

参考: https://www.ruanyifeng.com/blog/2020/08/rsync.html

sudo apt update
sudo apt install rsync openssh-client
# 查看C盘D盘等
ls /mnt
# 挂载D盘某个目录 或者直接访问/mnt/c, /mnt/d等
sudo mkdir /mnt/vscode-files
sudo mount -t drvfs D:/test /mnt/vscode-files

# 将远程服务器的文件同步到本地
rsync -av name@server:source/ /mnt/vscode-files/
# 将本地文件同步到远程服务器
rsync -av source/ username@remote_host:destination

用于执行备份的脚本 pull-remote-files.sh

#!/usr/bin/bash

# A script to perform pulling files

set -o errexit
set -o nounset
set -o pipefail

readonly SOURCE_DIR="...."
readonly BACKUP_DIR="..."
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest"

rsync -av --delete \
  -e "ssh -i ~..." \
  name@server:"${SOURCE_DIR}/" \
  --exclude=".vscode" --exclude=".ipynb_checkpoints" \
  --chmod=a+rwx,g+rwx,o+rwx \
  "${BACKUP_DIR}"

设置定时运行脚本

vim /etc/wsl.conf
# 添加下述两行, 使得cron自启
[boot]
command="service cron start"

crontab -e
# 每小时执行脚本
0 * * * * bash ~/.../pull-remote-files.sh >> ~/.../pull-files.log

用于执行推送文件的脚本 remote-files-push.sh

#!/usr/bin/bash

# A script to perform pushing files
set -o errexit
set -o nounset
set -o pipefail

readonly SOURCE_DIR="~/ddd"
readonly DES_DIR="~/xxx"

rsync -av  \
  "${SOURCE_DIR}/" \
  --exclude=".vscode" --exclude=".ipynb_checkpoints" \
  -e "ssh -i ~/.ssh/id_rsa" \
  yy@1.1.1.1:"${DES_DIR}"

传输文件

https://unix.stackexchange.com/questions/238152/why-is-scp-so-slow-and-how-to-make-it-faster

rsync -avP cap_* user@host:dir

tar czf - [files] | ssh user@host tar xvzfC - [dir]
tar cf - [files] | gzip | ssh user@host 'cd [dir] && gzip -d | tar xvf -'
tar cf - cap_* | gzip -c - | ssh user@host 'cd /dest/dir && { gzip -dc - | tar xf - ; }

ssh user@host "cd dir && tar czf - file" | tar xzfC - ./

cloudflared and autossh

sudo apt install autossh

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_sziit

AUTOSSH_POLL=30 autossh -M 0 -f -N -R 10000:localhost:10000 1114

# crontab -e in root user!
* * * * * /usr/sbin/service cloudflared start

最后修改于 2023-07-04