Monday, March 24, 2014

Backups to/from a Linux machine from Windows

Intro
I'm currently using Windows 7 on a laptop and Linux at home on my PC.  I plan on switching the laptop to Linux at some point, but for now I wanted to be able to back files up to my home PC over the internet.  Now if you're a normal person there are plenty of services that do this for you (eg, Dropbox), but if you're me, then you insist upon doing it manually and having total control.

Linux on Windows
Step one is to get a Linux like environment on Windows.  This means installing Cygwin.  You will want to install openssh, rsync, git, and whatever languages you may want to program in. 

With Cygwin you should be able to directly use linux commands in the Windows command prompt.  Use 'ls' to test this.

SSH
SSH (Secure SHell) allows you to log in to a remote machine over an encrypted connection.  Once logged in you have the same control as you would in a shell prompt on that machine locally.  It supports public key encryption so that we can login without a password prompt.

As it is, you can log in with just the password using ssh dale@192.168.1.123.  Replace dale with your account on the remote machine.  You can use an actual IP to log in over the internet, but first you have to forward port 22 on your router.

Generate Keys
Now it is time to generate some ssh keys to allow you to log into your home computer remotely.  There are two ways to use ssh keys.  You can generate keys that require a password to use, or you can generate keys that do not require a password.  If you use passwordless keys, then you will have a private key file that anyone could use to login to any computer where you've set it up to allow logins with that key.  Of course, you can keep the key on an encrypted laptop, and if they laptop is stolen or you have reason to believe the key was stolen it's as easy as deleting the key from the remote computer.

Passwordless keys are nice, and I use one to admin my site, but for my home PC I didn't want to allow access with just the keyfile.  This complicated the commands slightly, but I will get to that later.

Generating keys is easy.  Just run ssh-keygen, and answer the questions.  You now have the .ssh directory in your home directory.  Inside, should be (at least) two files.  First is id_rsa, which is your private key and must be protected.  The second file is id_rsa.pub, and that is your pubic key.  As the name implies there is no need to keep this a secret.  You will place this file on any machine you want to be able to log in to. To allow you to log in to a remote machine, find the .ssh directory in your home directory, and then the authorized_keys file in it.  Copy and paste the public key text string into that file. You may need to restart some processes, but you should now be able to log in from the laptop to the remote machine.  You use the same command as above, but with the username you put in the ssh key (which can be the same as you use on different machines, Linux will pair it with @host to make it unique).  

rsync
Now that you have the ability to log in to the remote machine you can make some scripts to move files between them.  rsync is nice for this because it will automatically use ssh to tunnel the connections to hide the data on public networks, and it does a good job of only sending the data that has changed. Because I used a password with my key I wanted to combine the rsync commands into one, so I only had to enter my password once.  I made 4 batch files.  Two each to receive files, and to send files, and then one set for going over the local network, and one for going over the internet. My local receive command looks like this:
rsync -ahvz ---progress --stats dale@192.168.1.123:/home/dale/Documents/ /cygdrive/C/Users/Dale/Documents/
pause
You can check the flags if you want, but I found those work well for me.  Note that it takes all the contents of the Documents folder on the remote machine and dumps it into the Documents folder on the laptop.  Also notice how cygwin handles drives.  You use /cygdrive/C/ to represent C: all forward slashes. For sending files it was a little harder:
rsync -ahvz --progress --stats Other Programming School dale@192.168.1.123:/home/dale/Documents/
pause
I can't remember exactly why, but I couldn't just use the whole folder.  I had send those individual folders.  It took a little while to get it, but this syntax works.  Note that 'Other' 'Programming', and 'School' are each files on the laptop that need to be sent to Documents on the remote machine. For over the internet versions, just replace the IP with your home IP, and make sure you set up port forwarding.  If you plan on accessing the remote machine over the internet, it makes sense use the host file to create a shortcut for the IP.  The host file is at: C:\windows\system32\drivers\etc\hosts,  Simply add a line with the IP and then a name, like: 123.456.78.90 home .  Then you can replace that IP with the name in scripts or on the command line.  

Closing
These are all pretty rudimentary, but they have been working well for me for a few months.   As it stands, they never delete a file, so if you move something you must manually delete it's old location on both the remote machine and local between syncs.  There are flags to do this automatically, but I kind of like it as is.

No comments:

Post a Comment