Posts tagged with shell:

DynDns Auto Update

I know these howtos are all over the place but I wanted to keep one handy. We lost one of our dyndns domains today due to inactivity. So I setup this little script to help.

#!/bin/sh

USERNAME=yourusername
PASSWORD=yourpassword
HOSTNAME=your.domain.name

cd ~/.dyndns
if [ -f ~/.dyndns/ipcheck.dat ]; then
  /usr/sbin/ipcheck -r checkip.dyndns.org:8245 \
 $USERNAME $PASSWORD $HOSTNAME
else
  /usr/sbin/ipcheck --makedat -r checkip.dyndns.org:8245 \
 $USERNAME $PASSWORD $HOSTNAME
fi

Place that in a path hidden to your users (it stores a password in text) and then add a cron to run it every month (Dyndns will expire free accounts after 30 days).

Netcat Disk Duplicate How-to Guide

Nomad.ca Notes: I would use this in the event of an emergency. There are plenty of packages that can be used to clone a disk that would be easier than this method however in a pinch this may help.

Scope

This guide shows how to save an image of a disk drive on a server.

The ultimate snapshot, is a binary disk image, which you can make using dd (disk duplicate) and nc (netcat). This is good for saving a backup of a Windoze PC, before the user has a chance to infest it with junkware, after which you can easily fix it again.

You can also use dd and netcat to copy all data from a smaller to a larger disk drive, then use diskdrake to add the additional space to the existing partition - or make a new partition of the extra space. This way you can upgrade a notebook from a 30GB disk to a 60GB disk for example, without having to re-install everything. You can let it rip and go watch a ball game, instead of having to spend the afternoon re-installing all your schtuff.
Network Test

Here is a neat network test method using netcat:

On the server:

$ echo Hello | nc -l -p 5000

On the client:

$ echo Aloha | nc server.ip.add.ress 5000

Whatever you type on the keyboard of one terminal will be echoed on the other terminal.

Save Image

To save a backup image on a server:

On the server:

# nc -l -p 50000 | dd of=pc.img

On the PC:

# dd if=/dev/hda | nc servername 50000

Restore Image

To retrieve a backup image from a server:

On the PC:

# nc -l -p 50000 | dd of=/dev/hda

On the server:

# dd if=pc.img | nc pc.ip.addr.ess 50000

Speedups

If you are impatient (and who isn’t?), then make the dd block size bigger, with the ‘bs=1M’ parameter. It will speed things up a bit - or at least, it will make you believe that it speeded it up a bit…

Sending 30GB of zeroes accross a 100MBps network can take a long, long time. Therefore, it is a good idea to compress the data before sending it with netcat, since most of the hard disk is usually empty. Remember to unzip it before writing it back. This will make a huge improvement in the backup time.

Save image

On the server:

# nc -l -p 50000 | dd bs=1M of=pc.img

On the PC:

# dd bs=1M if=/dev/hda | gzip -9 | nc servername 50000

Restore Image

On the PC:

# nc -l -p 50000 | gunzip |  dd bs=1M of=/dev/hda

On the server:

# dd bs=1M if=pc.img | nc pc.ip.addr.ess 50000

Because of the bs parameter, dd may not stop by itself. Eventually you will notice that things stopped, but dd is still running and netcat is still waiting. To stop it cleanly, press ctrl-C on the sending machine. The other side will then shut down too.

Notes

A Knoppix CD is good for this on the PC side, since it has nc and dd.

It is best to experiment with a second disk drive until you are confident. Do not overwrite the original disk drive until you are sure that the copy is good!

I also save an image *before* I repair a PC that is infested with spyware, since the repair tools sometimes render the PC unbootable. If I have an image, then I can restore it with all the crap and try again.

Note that the parameters of nc is a little inconsistent. You sometimes have to supply a -p before the port number and sometimes not - so save the above for future reference.

Also note that the image send may hang once done. You will get a message from dd on screen saying how many bytes were read/written but the program will not terminate and netcat will keep waiting for more data. Press Ctrl-C to quit at this point.

You can watch the progress of dd, by opening another console and sending it a signal using kill. First ps to get the numeric PID of dd:

ps | grep dd
kill -USR1 PID

Dd will then print a progress report on the first console and immediately carry on without interruption.
Using SSH as a Secure Transport

You could also use SSH instead of netcat, but the additional overhead of SSH will make things even slower, so use a fast cypher such as Blowfish. The neat thing with SSH, is that since the server is usually running by default already, you only need to type a command on the client side:

dd if=/dev/hda |gzip -9 |ssh -c blowfish username@domain.tld “dd of=image.gz”

I think that instead of dd, you can also use cat, though I have never tried cat for low level disk I/O - try it for fun.

Slow, but secure…

Source: http://www.aerospacesoftware.com/netcat-howto.html

Fake DFS

Fake DFS is a small script that mimics DFS folder grouping without the hassle or overhead of running any kind of server. While the downside of this is that changes to the FS aren’t replicated in real time, you can adjust things (cron) or use it in conjunction with other programs to make things synchronize quickly.

What does that mean?

Let’s say you have 3 machines on your home network. Each has a 200 Gig drive. Say you created a folder structure on Machine A as follows:

/mp3s
/tv
/movies

Now inside /tv you create a folder called "The Office" and in there you create another folder called "Season 3". So you put episodes 1,2 and 3 of Season 3 into the folder but by the 4th episode have filled the capacity of the drive in Machine A (with Mp3s and movies let's say). However you still have 2 other machines with plenty of room on the drives. So now on Machine B you create an identical folder structure:

/mp3s
/tv
    /The Office
        /Season 3
/movies

And you place episode 4 in the directory.

Now how are you going to see ALL of Season 4 as if they are in the same directory on the same drive?

FakeDFS.

So to use this script logon to the machine you are going to make the "master" server (IE where the FS’s will be merged). Mount the remote drives as CIFS/SAMBA or NFS. Create a "Master" directory, this is where you will see on single file structure for all of you drives. Then run the script with something like:

$ fdfs parent destination

Where parent is a the top part of the filesystem you want to be centralized and destination is the folder you created as the "Master" folder or a folder within.

Then run the command again for as many sub-structures as you wish to centralize. I create a small shell script that has all of my mappings in it so that re-syncing the FS is simple:

#!/bin/sh fdfs /media/Archive/TV/ /media/mythtv/TV/ fdfs /media/zxc/TV/ /media/mythtv/TV/ fdfs /media/Archive/Documentary/ /media/mythtv/Documentary/ fdfs /media/zxc/Documentary/ /media/mythtv/Documentary/ fdfs /media/Archive/Movies/ /media/mythtv/Movies/ fdfs /media/zxc/Movies/ /media/mythtv/Movies/ fdfs /media/zxc/Music/ /media/mythtv/Music/

Now you just run the shell script and it will "re-sync" the file system (IE if you change a file on one of the "child" filesystems).

fdfs - download