WSL icon indicating copy to clipboard operation
WSL copied to clipboard

WSL2 - Any suggestions on how to set up a reliably backed-up environment in WSL?

Open ad-on-is opened this issue 7 years ago • 12 comments

Prior to my current setup, I used to have my project-files stored on a different partition, and mount them inside WSL. But the I/O speed bothered me so much, that I had to reconsider my file structure. I've Easeus Backup running a file-backup every hour, and a full system-backup every week.

Now, I've moved all my project files to the WSL2 VM and the I/O performance is amazing when running Docker inside the WSL2 container.

But... here's my biggest concern. I'm a bit scared to keep this setup not properly backed up in case of a system crash, which could happen anytime.

My attempt was to use restic inside WSL to sync /home/user to mnt/b where my backup-drive is mounted, but unfortunatelly crontab doesn't work within WSL2, so I cannot have an hourly file-backup like I used to have with easus.

Do you guys have any suggestions how to properly backup my WSL instance, especially the projects residing in the VM? Is there a way to have the *.vhdx file stored on a different partition?

ad-on-is avatar Aug 05 '19 11:08 ad-on-is

Is there a way to have the *.vhdx file stored on a different partition?

I did this since the beginning of WSL. If you want to go with "documented" way, just use export and import option with wsl.exe.

Biswa96 avatar Aug 05 '19 12:08 Biswa96

Is there a way to have the *.vhdx file stored on a different partition?

I did this since the beginning of WSL. If you want to go with "documented" way, just use export and import option with wsl.exe.

Thanks! The "documented" way worked so far. I'm glad to have the *.vhdx file on a different partition.

Now, that I've D:\WSL\Ubuntu\ext4.vhdx how would I restore this file in case of a system-crash?

Could something like this work?

  1. Rename D:\WSL\Ubuntu to D:\WSL\Ubuntu_bak
  2. Create fresh Ubuntu distro
  3. Export fresh distro
  4. Import fresh distro to D:\WSL\Ubuntu
  5. Move D:\WSL\Ubuntu_bak\ext4.vhdx to D:\WSL\Ubuntu\ext4.vhdx and overwrite the existing one

ad-on-is avatar Aug 06 '19 19:08 ad-on-is

Ref #2955 #4268 #3974 #3536

therealkenc avatar Aug 06 '19 20:08 therealkenc

Is there a way to specify a new vhdx file to attach to the distro's container in WSL2 ? In this way, We can mount the vhdx to anywhere we want ( eg: HOME, projects's dir...). Then we just backup the vhdx file.

cosmoslx avatar Aug 07 '19 07:08 cosmoslx

What do you mean by "new vhdx file"? The VHDX file need to be created from the ext4.vhdx file which is done by LxssManager service.

Biswa96 avatar Aug 07 '19 08:08 Biswa96

What do you mean by "new vhdx file"? The VHDX file need to be created from the ext4.vhdx file which is done by LxssManager service.

I know that the distro's rootfs is stored in the ext4.vhdx file.

By using a "new vhdx file", I want to specify another vhdx file, call it "user vhdx", which I created as storage block device that can attach to the WSL2 VM. In this way, I can mount this storage device to anywhere I want in distro's rootfs and storage my data in it.

This isolate my data from the distro's ext4.vhdx file and my data is keep in the "user vhdx" I specify. I can easily backup the "user vhdx" file to save all my own data, also I can share all my own data to another distro I use by attaching the "user vhdx" file to the distro.

Just like the build 18945 introduce the .wslconfig file for tweaking WSL2 settings which can set a new vhdx file as a swap space.

cosmoslx avatar Aug 08 '19 02:08 cosmoslx

I want to specify another vhdx file, call it "user vhdx"

First, did you read my mind? 😵 I have been trying to do it since birth of WSL2. AFAIK, there is no fully documented procedure to do that. But LxssManager uses HcsGrantVmAccess() and HcsModifyComputeSystem() to add a VHD file and /sys/bus/scsi/devices to access it.

Biswa96 avatar Aug 08 '19 04:08 Biswa96

What do you mean by "new vhdx file"? The VHDX file need to be created from the ext4.vhdx file which is done by LxssManager service.

I know that the distro's rootfs is stored in the ext4.vhdx file.

By using a "new vhdx file", I want to specify another vhdx file, call it "user vhdx", which I created as storage block device that can attach to the WSL2 VM. In this way, I can mount this storage device to anywhere I want in distro's rootfs and storage my data in it.

This isolate my data from the distro's ext4.vhdx file and my data is keep in the "user vhdx" I specify. I can easily backup the "user vhdx" file to save all my own data, also I can share all my own data to another distro I use by attaching the "user vhdx" file to the distro.

Just like the build 18945 introduce the .wslconfig file for tweaking WSL2 settings which can set a new vhdx file as a swap space.

I want to specify another vhdx file, call it "user vhdx"

First, did you read my mind? 😵 I have been trying to do it since birth of WSL2. AFAIK, there is no fully documented procedure to do that.

This is a bit off topic, but I ran into a similar requirement, here is what I did.

On Windows: fsutil file createNew <call-this-file-whatever-you-want> <size-in-bytes>

For me, the file I used was: development.ext4

In WSL, format this 'virtual disk' as ext4 (block size of 1024): mkfs.ext4 -b 1024 <location-for-development.ext4>

(Optional) Remove reserved blocks: tune2fs -m 0 <location-for-development.ext4>

(Optional) Increase throughput: tune2fs -o journal_data_writeback <location-for-development.ext4>

Mount the 'virtual disk' inside WSL sudo mount -t ext4 <location-for-development.ext4> <mount-point>

This works for me. However, I can't get this to automount via fstab. I haven't had time to look into why I'm unable to and for the time being I've resorted to auto-mounting using task scheduler.

Hope this helps someone.

kodeclan avatar Aug 12 '19 05:08 kodeclan

I want to specify another vhdx file, call it "user vhdx"

First, did you read my mind? 😵 I have been trying to do it since birth of WSL2. AFAIK, there is no fully documented procedure to do that. But LxssManager uses HcsGrantVmAccess() and HcsModifyComputeSystem() to add a VHD file and /sys/bus/scsi/devices to access it.

Thanks for sharing the API that do the work! I will try to figure out. Hope that WSL2 can have a feature to achieve that easily in the funture.

cosmoslx avatar Aug 12 '19 06:08 cosmoslx

Closing since ran course. SOP for backing up data in a distro is tar (my personal preference) or wsl --export. Mucking around with WSL internal implementation details is fun too, but not actionable.

therealkenc avatar Sep 21 '19 17:09 therealkenc

@therealkenc so how did you restore it?

airtonix avatar Oct 27 '20 03:10 airtonix

With tar -x inside a new distro installation for the former, or wsl --import for the latter.

therealkenc avatar Oct 27 '20 19:10 therealkenc