If you’re running Ubuntu or another linux shell in Windows 10 via WSL (Windows Subsystem for Linux), you’ve probably wondered about using cron.

Cron is super-handy for doing things, like, running a backup.

You see, as useful as WSL is, it stores all the linuxy files in a way that is not exactly easily backup-able by File History or anything else… as far as I can tell.

Worse yet, even when you try to set up a cron job to run a backup, it doesn’t work!

What to do, what to do?

Set up cron as usual

The first thing you’ll need to do is set up a script and cron job so that theoretically, the backup will happen inside linux.

I’m using Ubuntu 16.04 at the moment, so the stuff below may need to change a bit if you’re using another flavor of linux on Win 10.

For example, to back up the entire /root dir where all your git and other goodies are stored, you might have a backup script /root/backup.sh like so:

# This script makes a backup of Ubuntu WSL stuff every day
cd /root
dpkg --get-selections | grep -v deinstall >> /root/packages.txt
tar czvf /opt/backup/WSLbackup.tar.gz /root/*
cp -f /opt/backup/WSLbackup.tar.gz /mnt/e/Backups/WSLbackup.tar.gz
rm /opt/backup/WSLbackup.tar.gz
touch /var/log/backup_success

Right, so that’s pretty straightforward:

  1. Change dir to /root
  2. Save the installed packages to the file /root/packages.txt so that if WSL gets hosed, you’ll remember what you need to reinstall.
  3. Create a temporary compressed file of everything in /root
  4. Copy the archive to your E:\Backups (recall that all your Windows drives are available as /mnt/X/, where X is the Windows drive letter)
  5. Delete the temporary compressed archive
  6. Touch /var/log/backup_success so that you have a date-stamped file that indicates if the backup ran recently

Next, create a file \etc\cron.d\run_backup with something like the following contents:

# Run backup every day at 5am
# m h dom mon dow       user    command
0 5    *    *    *   root   /root/backup.sh

Grrrrrreat…  That just runs your backup script as root every morning at 5am.

All set, right? Not so fast!

So, you might think that you’re done, but you’d be wrong.

It appears that at the time of writing anyway, Microsoft “doesn’t allow” background linux tasks.

You can use Task Scheduler and run a bash command with 75,000 crazy parameters from the Windows’ side of things, and apparently that works. But holy cow, it’s crazy.

Now, when I say MS doesn’t allow background linux tasks, that’s not entirely true… It turns out you just need to run:

service cron start

Done? Nope!

You see, the problem is that if you close your WSL window, it’s like shutting down linux. So, no cron jobs.

So here we have a bit of a hack. Edit /root/.bashrc, and add the same line at the very end:

service cron start

Now, every time you fire up Ubuntu on Bash on Windows on Puter on Earth, the cron service will be automatically started.

As long as you leave the bash window open, your cron job(s) will run nicely.

So, adjust your backup (or whatever) times accordingly.

If you shut down your puter at night, for example, you could just run the backup script once every hour. While you work, the backup will run.

And you’re done

The beauty of this solution is that:

  • You get to use cron to do whatever you would normally do in linux
  • It starts automagically when you open WSL
  • The backup file is saved to a Windows drive, and is fully accessible via Explorer or the Windows command prompt

That’s it!

One final note

It seems that while you can find the linux system’s files in your Users folder, this is not recommended. In fact, the location seems to have changed yet again.

The reason for this is that modifying any of those linuxy files from Win 10 apparently causes things to break nicely on the linux end of things.

So, if you need a backup, make a compressed archive in linux, and dump that archive onto the Windows file system. DONE!

Don’t miss:

Need help? Hire me!
Get Scottie Stuff!