Archive for November, 2011

Using Rsync to backup a directory

Monday, November 21st, 2011

Recently I was moving data between an old server and new web server. I had some 250 GB of data and thought about just using Secure Copy (scp). When I started the process, I walked away
thinking “No Problem”, but after a few files, I got an error due to file permissions and I did not want to figure out what files had not been copied yet or where to try to restart.
So I used rsync to mirror the drive. I was able to set it up and due to my slow network speed, it still took several days, but I was certain that I had gotten all the data.

rsync -azuv -e ssh  user@originserver:public_html/* public_html/

The options I used were for :

Option Description
-a Archive Mode
-z Compress File Data
-u Update Mode
-v Verbose
-e specify command to run

This took some time to run, but I did not have to babysit the command and it duplicated the data from my old server to the new one.

XML: Characters that require replacement

Sunday, November 20th, 2011

I was generating some html based e-mails and due to some changes I made in the text, my mail stopped being sent. I found that it was not liking some of the characters that were being placed in my generated emails. I tried some of the standard replacemnt methods, but found that the XML file was the issue. The Greater than(>) and less than (<) and the Amersand(&) and the apostrophe(') and the quote(") all have special meaning to XML and can cause these issues. Once I replaced the following characters in my email variable, my e-mails began to flow again. Here is a table of the characters and their XML representations that can be used for replacement:

Name Character XML replacement
Ampersand & &
Apostrophe ' &apos;
Greater Than >r &gt;
Less than < &lt;
Quote " &quot;

This is by no means the complete list of replacements. This is only that characters that were needed by the calls to Java Mail that my program was using.

Tags: , ,

JAVA: XMLGregorianCalendar Dates

Sunday, November 20th, 2011

I was updating some plugins for a project and came to a query for an e-mail program I was writing, I had to send out recently introduced defects to their respective owners and insure that they would only get the defects from the a given offset. It turned out that my API had a XMLGregorianCalendar argument. So I went out to look for a way to offset the date object so I could retrieve the correct information.

I used the following to convert my date:

package com.etechtips;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class UpdateDate {

	public static void main(String[] args) {
		XMLGregorianCalendar xgcstart;
		GregorianCalendar gc;
		int daycount = 0;
		if (args.length != 1)
		{
			System.out.println("Need to specify offset");
			System.exit(1);
		}
		
		daycount = Integer.parseInt(args[0]);
		
		// This will get the current date based on System time of an object
		gc = new GregorianCalendar();
		System.out.println("CURRENT:" + gc.toString());

		// This will offset the date by daycount days
		gc.add(Calendar.DATE, daycount);
		System.out.println("OFFSET: "+ gc.toString());
		
		try
		{
			// Now the XMLGregorianCalendar object has an 
                        instance that is based on the time set in the gc object
			xgcstart = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
		}
		catch(DatatypeConfigurationException dce)
		{
			dce.printStackTrace();
			System.exit(1);
		}
		
		// Code omitted 	
	}
}

Output:

CURRENT:java.util.GregorianCalendar[time=1321793315934,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Chicago",offset=-21600000,
dstSavings=3600000,useDaylight=true,transitions=235,
lastRule=java.util.SimpleTimeZone[id=America/Chicago,offset=-21600000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,
startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,
endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=10,
WEEK_OF_YEAR=48,WEEK_OF_MONTH=4,DAY_OF_MONTH=20,DAY_OF_YEAR=324,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,
AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=48,SECOND=35,MILLISECOND=934,ZONE_OFFSET=-21600000,
DST_OFFSET=0]
OFFSET: java.util.GregorianCalendar[time=1321706915934,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Chicago",offset=-21600000,
dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/Chicago,
offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,
startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,
endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,
YEAR=2011,MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=323,
DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=48,SECOND=35,
MILLISECOND=934,ZONE_OFFSET=-21600000,DST_OFFSET=0]

The printouts above show the current date and the new date based on the offset. I made the day of the month bold in the printouts to show what the offset has updated the date. You could put a much larger offset than 1 and Calendar object will take care of updating the day of week, week of year and more. You will not need to deal with checking for leap year or dealing with updating all the month and years, this is all taken care of for you.

Tags: ,

DOS: Check for empty environment variable

Wednesday, November 16th, 2011

I was writing a batch file the other day and needed to change the argumets to a command based on if the user had specified his password on the command line.

SET PASS=somepassword
: or
:SET PASS=
if x%PASS%= X (
  SET PASSARG=--password somepassword
) else
(
  SET PASSARG=
)

commandtorun %PASSARG%

Now if the user comments out his password, the script will make the PASSARG environment variable be empty or if they set a password the passarg variable will be filled in.

Tags: ,

Java: Command line argument processing

Sunday, November 6th, 2011

In each language that I have learned I have found it useful to know how to process command line arguments and here is a simple framework that I use for Java. I usually wrap the java command line in a bat or shell file to make it easier to run so that the user does not have to worry about classpath or path issues.

The following is the basic code to handle the command line arguments.

class CmdArgs
{

  public static void main(String[] args)
  {
      int argsize = args.length;
      String curarg;

      if (argsize > 0 )
      {
       
      }
      
      for(int i = 0; i < argsize; i++)
      {
          if (args[i].equals("--host"))
          {
             i++;
             host = args[i];
           }
          else if (args[i].equals("--debug"))
          {
               debug = 1;
          }
          else
          {
              System.out.println("Unknown option: " + args[i]);
              System.exit(1);
          }
       }

   }
}

The bat file for Dos/Windows might look like this:

@echo off

java -cp c:javabin;somejar.jar  com.etechtips.CmdArgs %*

The shell based file might look like:

java -cp /home/ecdown/javabin:somejar.jar com.etechtips.CmdArgs $*

Tags: ,

Java: Split Strings

Sunday, November 6th, 2011

I was using Java to add users to a system by parsing a comma seperated value (CSV) file of user entries and I needed to split the file to gather the separate entries. There is also the StringTokenizer class that can seperate the entries as well.

So the following code snippet was used:

String fileline;

FileInputStream fis = new FileInputStream("filename.txt");
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));

while((fileline = br.readline()) != null) {
  String[] entries = fileline.split(",");

  for(int i=0;i < entries.length;i++)
  {
    System.out.println(entries[i]);
  }
}

Linux:Script to use find to search contents of files

Thursday, November 3rd, 2011

I use find and grep quite a bit. I found that I was regularly searching for strings in file and was typing commands like:

find . -exec grep "somestring" {} ; --print

This find command will search from the current directory and for each file will run grep on it and if it finds a matching string, it will print the string and then the filename. So I took this and moved it into a script.

#!/bin/sh

if [ $# -lt 1 ]; then
  echo 1>&2 Usage: $0 ""
  exit 127
fi
icase=
search=

while [ $# -ge 1 ]; do
  case $1 in
     -i) icase=$1;;
      *) search=$1 ;;
  esac
  shift
done

find . -type f -print0 | xargs -0 grep $icase $search

This allows me to run a command such as

ffind -i Super

This command will search all file names starting from the current directory and recurse directories and the “-i” will tell it to ignore case.

Tags: