Fun with Linux/Mac

Doing passwords in sh files to automate the basics with scp…

It’s things like this that, while certainly not very intuitive, make nix so much better than windows.

I wanted to back up my PostgreSql db on my production box everyday to my local dev box and do it via a one command. With a bit of google’ing this was pretty simple.

From this post and this post I hobbled together this procedure. Read those posts if you want to understand this stuff better – this is mostly just notes for me – because I’ll forget and those posts might go down.

We’ll allow our local box (called wiseleyb@mybox) to connect to our production box (called wiseleyb@prodbox.com)
In /home/wiseleyb on the computer mybox – generate a key:

wiseleyb@mybox:~>ssh-keygen -t rsa

Hit enter for all the prompts
It should have created a file /home/wiseleyb/.ssh/id_rsa.pub

Now – copy that up to wiseleyb@prodbox.com

wiseleyb@mybox:~>scp .ssh/id_rsa.pub wiseleyb@prodbox.com:/home/wiseleyb/.ssh/wiseleyb.mybox.id_rsa.pub

SSH into prodbox.com

wisleeyb@mybox:~>ssh wiseleyb@prodbox.com
password:
wiseleyb@prodbox.com:~>cd .ssh
wiseleyb@prodbox.com:~>cat wiseleyb.mybox.id_rsa.pub >> authorized_keys
wiseleyb@prodbox.com:~>exit

You should now be able to ssh and scp without passwords… give it a try…

wiseleyb@mybox:~>ssh wiseleyb@prodbox.com ls

You should see the directory listing.

Now – you just need to write a simple sh file to automate this…

wiseleyb@mybox:~>nano downloaddb.sh
   DATE=$(date +%b%d)
   ssh wiseleyb@prodbox.com pg_dump -Fc -f prodboxdb_$DATE.db prodboxdb_production
   scp wiseleyb@prodbox.com:/home/wiseleyb/prodboxdb_$DATE.db /home/wiseleyb/prodbox_databases/.
wiseleyb@mybox:~>chmod 755 downloaddb.sh
wiseleyb@mybox:~>./downloaddb.sh

Pretty simple once you get the key stuff working…

Try doing that in windows with SqlServer and vbscript and posting anything less than a book about how you did it. :)

1 Comment

mattNovember 15th, 2006 at 11:31 am

A stickler will point out that Mac OS isn’t based on Linux – it’s a BSD based operating system.