Title photo
frugal technology, simple living and guerrilla large-appliance repair

Regular blog here, 'microblog' there

Many of my traditional blog post live on this site, but a great majority of my social-style posts can be found on my much-busier microbloging site at updates.passthejoe.net. It's busier because my BlogPoster "microblogging" script generates short, Twitter-style posts from the Linux or Windows (or anywhere you can run Ruby with too many Gems) command line, uploads them to the web server and send them out on my Twitter and Mastodon feeds.

I used to post to this blog via scripts and Unix/Linux utilities (curl and Unison) that helped me mirror the files locally and on the server. Since this site recently moved hosts, none of that is set up. I'm just using SFTP and SSH to write posts and manage the site.

Disqus comments are not live just yet because I'm not sure about what I'm going to do for the domain on this site. I'll probably restore the old domain at first just to have some continuity, but for now I like using the "free" domain from this site's new host, NearlyFreeSpeech.net.

Fri, 13 Oct 2017

How to back up your /home directory in the Windows Subsystem for Linux without losing permissions before killing your old WSL and installing a new one from the Windows Store

The Windows Subsystem for Linux - which is no longer being referred to officially as "Bash on Windows" - has grown up.

You no longer need to put your Windows 10 system into Developer Mode to use the WSL.

And now there are three different WSLs: Ubuntu, openSUSE and SUSE Linux Enterprise Server. Fedora is supposed to be on the way, but I haven't heard anything about progress toward that happening in a long time.

According to Microsoft, you can copy your current Ubuntu files to /mnt/c/tmp/WSL-backup, or a similar directory, and then use lxrun /install from the Windows command line to remove the old WSL. Then you can install a new WSL and move your files back after that.

Fellow Redditors suggested that I create a tar archive of my files, stow it anywhere, and then unpack it in my new /home directory when I remove the old WSL and install the new one via the Windows Store.

The only thing keeping me from doing it is what always keeps me from switching Linux distros: The need to re-install all of my packages.

The other thing keeping me from making this move is that my laptop hasn't yet received the Windows 10 Fall Creators Update. To successfully install the new WSL, your Windows build needs to be 16215.0 or higher. According to my About Windows 10 page, I'm on 15063.674. So not only can I wait, unless I opt to get the updates early (and I'm sure there is a way to do this), I must wait. And honestly, that doesn't bother me one bit.

Update: (July 4, 2018): Here is my Independence Day update to these instructions. The "original" instructions should work fine, but the "new" ones are better:

The original instructions

Originally I created the tar archive of my home directory in my home directory:

$ tar -zcvf steven.tar.gz /home/steven
$ cp steven.tar.gz /mnt/c/Users/steve/OneDrive/Documents/linux_backup/

And to open it up when your new WSL is set up, go to your home directory:

$ tar -zxvf /path/to/steven.tar.gz

Then you'll have a home directory within your home directory (i.e. for me /home/steven/steven, and you can copy what you want from one to the other). It's better not to overwrite everything in your "new" Linux /home directory because it might handle things in your dot files differently, and I can see conflicts arising from npm modules, ruby gems and whatever else you happened to install in the course of working in your Windows box's Linux side.

I'm not sure how much of a problem it is, but creating the tar archive while in your home directory causes tar to throw the following error:

tar: Removing leading `/' from member names
... (lots of output)
tar: Exiting with failure status due to previous errors

There are explanations of what this error message means on the Internet, but they didn't really help me understand it.

So I did things a different way. If you just want to know how to make the backup and restore it, start here:

The new instructions

First, on the Windows side, create a directory in which to hold this backup. I created one called linux_backup in my OneDrive Documents directory.

Then go into your WSL, which will put you in your home directory on the Linux side (which for me is /home/steven).

From there, to avoid the error message I go up one directory and do the tar operation from there. I use pwd a couple of times to confirm where I am in the filesystem, and I use chown to make sure the restored archive belongs to my Linux user and group.

Note: in all of these instructions, things after a $ are things you type at the command line. Lines that don't begin with a $ represent output in the terminal (and you don't have to type them).

Here is what my terminal session looks like:

$ pwd
/home/steven
$ cd ..
$ pwd
/home
$ sudo tar -zcvf steven.tar.gz steven
$ ls
steven steven.tar.gz
$ chown steven:steven steven.tar.gz
$ sudo cp steven.tar.gz /mnt/c/Users/steve/OneDrive/Documents/linux_backup/

Now you have your tar archive on the Windows side.

After you set up your "new" WSL, you can copy the tar archive from the Windows side into your new Linux /home directory. As before, I use a couple of pwd command to confirm where I am in the Linux filesystem. (And remember, use YOUR home directory and Windows path to your tar archive, not mine):

$ pwd
/home/steven
$ sudo cp /mnt/c/Users/steve/OneDrive/Documents/linux_backup/steven.tar.gz steven.tar.gz
$ sudo chown steven:steven steven.tar.gz
$ tar -zxvf steven.tar.gz
$ ls
steven
$ cd steven
$ pwd
/home/steven/steven

Now you have your "new" home directory at /home/steven, and your "old" files at /home/steven/steven. Copy what you want from old to new, and you should be ready to go.

Once you confirm that all your files are in there, you can blow away the old WSL and use your new one. And you should still have a copy of the tar archive on your Windows filesystem if anything goes wrong.

Before going forward, it's a good idea to read up on how to make a tar.gz archive.

My question (and maybe yours): Why do this from the Ubuntu/WSL shell when you could just copy/paste from the Windows file manager, or why not just copy the files via the Ubuntu shell to the Windows portion of the disk?

Answer (and I did test this): In both cases, even when using Bash to do the recursive cp of all the files, you lose your Linux file permissions. In my case, all of my rw-r--r-- files turned into rwxrwxrwx, which is NOT what you (or I) want.

Update on 4/23/18: This entry was written last October but not published until now. It kind of got lost, and I just found and finished it. Though the new WSL is out, and you can now install Debian (which I would) or SUSE (probably not), I'm leaning toward sticking with Ubuntu because it's working so well, though I'm a longtime Debian fan and user (though not so much lately).

Whatever I do, I probably need to move to the "new" WSL, though I haven't done it yet. Everything is working, so it's hard to muster the motivation to mess with what until now has been a successful installation of Ubuntu in Windows 10.

Update on 7/4/18: The new instructions for making an archive of the /home directory are more detailed and a little more roundabout. But they should work, and tar won't throw any errors.

Also, I still haven't moved to a new WSL. The current Ubuntu WSL is still on 16.04 LTS, and that's the same version as my "old" WSL. When the Ubuntu WSL moves to 18.04 LTS, then it'll be time to make the change.