Suppose you have several computers on an LAN, where DHCP is enabled. On Machine B, you want to routinely back up files from Machine A, whose IP address may change. This can be done using SSH and a local DNS server. The following experiment is done on two Mac OS X 10.8 machines.
- Set up DNS server (optional):named and rndc utility are installed by default on Mac OS X 10.8. For references on setup, see this link.
- Use rndc-confgen to generate configuration and secret key:
$ sudo bash -c "rndc-confgen -b 256 > /etc/rndc.conf" $ sudo bash -c " head -n5 /etc/rndc.conf | tail -n4 > /etc/rndc.key"
- Edit /etc/named.conf and /etc/rndc.conf to ensure the port number are the same
- Start named server, run “rndc status” to check whether it is started:
$ launchctl load -w /System/Library/LaunchDaemons/org.isc.named.plist $ launchctl start org.isc.named $ rndc status
- Create a zone file for the target machine (Machine A in this case):
$ cd ~/Documents/ $ mkdir named $ cd named $ vi machine-a.zone
Copy the following text in to machine-a.zone, the IP address 192.168.0.120 is irrelevant at this time:$TTL 86400 $ORIGIN machine-a. @ IN SOA @ root ( 2013091701 ; serial number YYMMDDNN 28800 ; Refresh 7200 ; Retry 864000 ; Expire 86400 ; Min TTL ) IN NS @ IN A 192.168.0.120 ; lan-sync
- Create symbolic link at /var/named/machine-a.zone (/private/var/named/machine-a.zone):
$ ln -s /Users/yourname/Documents/named/machine-a.zone /private/var/named/machine-a.zone
- Edit /etc/named.conf, insert the following lines after the existing zone configurations:
zone "machine-a" IN { type master; file "machine-a.zone"; allow-update { none; }; };
- Edit /etc/resolv.conf, replace existing nameserver with 127.0.0.1. This file should look like this:
# # This file is automatically generated. # nameserver 127.0.0.1
- Download the script and set the following variables in ssh_sync.sh:
$ git clone https://github.com/moleculea/lan-sync-over-ssh $ cd lan-sync-over-ssh $ vi ssh_sync.sh user_home="/Users/yourname" # Remote hostname (LAN) and MAC address hostname="machine-a" # User name on the remote host username="yourname" mac_address="11:11:11:11:11:11"
- Setup SSH connection from Machine B to Machine A:
- On Machine A, open port 22.System Preferences -> Sharing -> Remote Login -> Allow access for (yourname)
- On Machine B, generate key pair:
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/Users/shichao/.ssh/id_rsa): machine-a-sshkey Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in machine-a-sshkey. Your public key has been saved in machine-a-sshkey.pub. The key fingerprint is: dd:bf:aa:6c:90:f5:fc:dc:18:68:1e:f4:77:7c:4c:2e yourname@machine-b.local The key's randomart image is: ...
- Create SSH config file on Machine B:
$ mv machine-a-sshkey ~/.ssh/ $ cd ~/.ssh $ chmod 600 machine-a-sshkey $ vi config
Add the following lines to config:Host machine-b IdentityFile ~/.ssh/machine-b-sshkey User yourname
- Copy the public key to Machine A: Append the content of machine-a-sshkey.pub it to ~/.ssh/authorized_keys. You can ssh to Machine A or do it directly on Machine A.
- Test the SSH connection:Suppose the IP address of Machine A is 192.168.1.109. On Machine B:
$ ssh 192.168.1.109
You should access Machine B now.
- Sync the files from Machine A to Machine B as backup:
- Test the script:
$ cd lan-sync-over-ssh $ ./ssh_sync test
If the script does not retrieve the IP address of Machine A, run the following instead if you know the IP address, saying 192.168.1.102:$ ./ssh_sync test 192.168.1.102
- Sync using DNS resolution:
$ ./ssh_sync dns /path/to/machine/a/source /path/to/machine/b/dest/backup
This sync all contents in directory /path/to/machine/a/source on Machine A into the /path/to/machine/b/dest/backup/source directory on Machine B. - Sync using /etc/hosts:If you do not setup DNS server on Machine B, an alternative way is to use /etc/hosts:
$ ./ssh_sync hosts /path/to/machine/a/source /path/to/machine/b/dest/backup
However, this need administrative privilege when executing the script, because it needs write data to /etc/hosts.
UPDATE
If you encounter issues that you cannot ssh or ping machine-a (with errors like “ping: cannot resolve abc: Unknown host”), you need to change the Search Domains on Mac OS X. You can do this by System Preferences -> Network -> Advanced -> DNS -> Override the default Search Domains with ”.local”, or you can edit /etc/resolv.conf by adding a line “search .local” before “nameserver 127.0.0.1″.
No comments:
Post a Comment