Saturday, October 25, 2003

Using Cron and SCP for Backups and Such

Introduction
This document will explain the steps required to use scp in cron. This is generally useful to do automated backups on Linux and other nix variations.
Instructions
These instructions will walk you through what it takes.

1. Generate a private/public key pair

Simple command to do this:

ssh-keygen -t rsa
Leave the passphrase empty so that cron can use it passwordless. Just be sure nobody gets your private key.

2. Copy the public key to the remote server

scp ~/.ssh/id_rsa.pub remote_host:

3. Add local key to remote servers trusted key

Log on to the remote server and if there has never been a key created for this user on the remote machine, run the ssh-keygen-t rsa just to get the key directory and stuff set up.

Then concatenate the new key to your authorized_keys file:

cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

Now for some reason, you may have to do this to the keys file:

chmod 644 ~/.ssh/authorized_keys

4. Now try logging into the remote machine again from local

ssh REMOTE_USERNAME@remote_host

This should log you in without asking for a password. If it doesn't, then something must be wrong at this point and you should go through the steps above again. This should also mean that scp will work the same way and you might want to test that scp works by copying a file from local to remote usingscp. If no password, then we're all good.

5. Now lets test the cron script

My cron script for this example is simply going to copy a directoryof files using scp to the remote server.

scp -r /PATH_TO_FILES/ REMOTE_USER@remote_host:BACKUPS/

And that's about all she wrote. Save that in a file called backup.sh (or whatever you want to call it), chmod 755 i it and then try running it. ./backup.sh to run it. If it works, then next stop is to cron it.

6. Add script to cron jobs

Easiest way is to simply copy the backup.sh file into /etc/cron.daily/ . This will make it run every day. Choose another cron.SOMETIME to make it run more or less often. If you want more control on when it's run, read up on adding it to the crontab.

Wednesday, June 18, 2003

Fixing the Tomcat 302 Redirect Issue

Introduction

This problem has to do with Tomcat redirecting people to a different
page when entering a url like:


http://www.spaceprogram.com or http://www.spaceprogram.com/



Tomcat sends a redirect to the browser to make them go to:


http://www.spaceprogram.com/index.jsp or http://www.spaceprogram.com/index.html
or whatever the default page is.


When what should happen is an internal forward so the browser doesn't
get redirected. This affects search engine crawling (some will stop at
the first sign of a redirect on a sites main page).


Intended Audience

This document is for programmers that can look through source code and
compile. If there is enough need, we may be able to post already compiled
downloads on this page, but we'll see in time.


Instructions


There are 2 ways we can accomplish this, one is to subscribe to the tomcat-dev
mailing list
and beg for them to add this very simple fix. The other
is to do it yourself and follow the instructions below.


NOTE: This was done in Tomcat 4.1.18 so may need to be modified a bit
for different versions.

Also tried in 4.1.27 and it works fine - Aug. 18, 2003.


You will need to download the tomcat source distribution or get it from
cvs. The Tomcat homepage is here.



These steps apply to org.apache.catalina.servlets.DefaultServlet. After
you're done making changes, you can simply take the newly compiled version
of DefaultServlet and put it in a non source tomcat release under the
CATALINA_HOME/server/classes directory.


1. Add New Variable


/**

* for forwarding instead of redirecting on 300 errors ie: when www.x.com
is sent, instead

* of doing redirect, it will just change the response.

* TR


*/

private boolean redir301 = true;


2. Then down in init(), added this:


/**

* added this too

* TR


*/

try {

value = getServletConfig().getInitParameter("redir301");

redir301 = (new Boolean(value)).booleanValue();

} catch (Throwable t) {


;

}


3. Then in serveResource() changed to this, find where the first
line is then see what I did.




ResourceInfo welcomeFileInfo = checkWelcomeFiles(path, resources);
if (welcomeFileInfo != null) {
String redirectPath = welcomeFileInfo.path;

if(redir301){ // added TR
String contextPath = request.getContextPath();
if ((contextPath != null) && (!contextPath.equals("/"))) {
redirectPath = contextPath + redirectPath;
}
}

redirectPath = appendParameters(request, redirectPath);

/**
* Changed this for 301 stuff
* TR
*/
if(redir301){
response.sendRedirect(redirectPath);
}
else{
request.getRequestDispatcher(redirectPath).forward(request, response);
}
return;
}


4. Add new param to web.xml in catalina_home/conf/web.xml under
default servlet.



<init-param>

<param-name>redir301</param-name>

<param-value>false</param-value>

</init-param>



You must set this to false or else it will just act the same as before.


Now restart tomcat and that should be it.