This time, on Bash on Ubuntu on Windows, we'll monitor file changes in /mnt/c/note recursively using gulp, execute rsync command using expect when gulp activates, and upload updated files to VPS. I want to program this behavior.
Background
When remote desktopping, there's a bug where copying text from atom on PC1 and pasting on PC2 results in strange formatting. As a solution, we can synchronize files between two PCs.
The easy way is to use Windows' standard feature, file sharing using samba. When you change and save a file on one side, the content switches on the other side without needing to update, which is convenient. However, this only works locally. It can only be used between PCs on the same wifi.
It might be possible to do remotely, but making it work remotely means exposing your computer's global IP address to the world. In essence, your computer becomes easily targeted and becomes a target for hacking.
I don't like this, so I thought of a method where I rent a VPS server in addition to my own computer and edit files there.
When you want to edit the same file on two PCs, you work through VPS.
Specific Steps
sudo su -
cd /mnt/c/note
apt install -y nodejs npm nodejs-legacy expect rsync
npm install -g gulp
npm install -D gulp
npm init -y
npm install child_process
gulpfile.js
var gulp = require("gulp");
var ps = require('child_process').exec;
gulp.task('exec_file', function() {
var command = "'/mnt/c/pg/expect/rsync_mnt_c_note'";
ps(command , function (err, stdout, stderr) {
console.log(stdout);
});
});
gulp.task("watch", function() {
var targets = [
'./**'
];
gulp.watch(targets, ['exec_file']);
});
One thing to note here is not to do . /mnt/c/pg/expect/rsync_mnt_c_note. /mnt/c/pg/expect/rsync_mnt_c_note itself must be executed by the expect interpreter, so doing . hogehoge means executing with Bash(sh/zsh), which has the same meaning as bash hogehoge or source hogehoge, so be careful not to do it out of habit.
Create an expect file with touch /mnt/pg/expect/rsync_mnt_c_note, with content like the following. (Change YOUR_OWN for your own use)
#!/usr/bin/expect
# C:\pg\expect\rsync_mnt_c_note
set PW "YOUR_OWN"
set Prompt "\[#$%>\]"
# set RemoteHost [lindex $argv 0]
set timeout 5
spawn rsync -av -e "ssh" -r "/mnt/c/note" root@YOUR_OWN:/root/lib/DESKTOP-PS5DVT5
expect {
-glob "(yes/no)?" {
send "yes\n"
exp_continue
}
-glob "password:" {
send -- "${PW}\n"
}
}
expect {
-glob "${Prompt}" {
interact
exit 0
}
}
Now, let's run gulp watch in the /mnt/c/note directory.
Then,
create a file with cat > /mnt/c/note/everything_note.md,
and update the file with cat >> /mnt/c/note/everything_note.md.
Now, let's check if it's properly uploaded to the VPS server.
ssh root@YOUR_OWN
cat DESKTOP-PS5DVT5/note/everything_note.md
If the characters you entered with cat are output, the upload was successful.
The upload takes about 3 seconds. Depending on the weight of the entire folder, it's not a differential upload that only uploads the updated parts, but a full upload that uploads all files.
Notes
Also, as a note, images are fine, but video files should not be placed in the target folder... in this example, /mnt/c/note, while gulp watch is running.
Why? Videos are also files with double-byte characters, so when a file is being updated, it means characters are being updated.
In other words, while uploading a video, characters are increasing at tremendous speed, and if you're running gulp watch at this time, it will execute commands to access VPS every time characters increase. Therefore, it puts considerable load on the server.
Be careful.