Sunday, April 26, 2009

Find-Replace text in files from command line

In Linux one can easily find and replace some text in files by using the sed application. In Windows, I couldn’t find a decent build in equivalent. I ended by using the ported version of sed for Windows. I works quite well using the same syntax and doesn’t require to be installed. Just make sure sed.exe, libiconv2.dll and libintl3.dll are existing in the same folder or on the classpath.

One should know any file altered by sed for Windows will result in this file being also converted from Windows line ending (using 2 characters: Carriage Return + Line Feed) to Linux line endings (only 1 character: Line Feed). This can sometime give some strange results. To overcome this, one should make sure the last replacement in the file is set to replace the Line Feeds back to Carriage Return + Line Feed:

sed -i "s/$/\r/" "%DIR%\file.txt"


Some other useful sed commands are available from sed sourcefourge:



FILE SPACING:
 # double space a file
 sed G
 # double space a file which already has blank lines in it. Output file
 # should contain no more than one blank line between lines of text.
 sed '/^$/d;G'
 # triple space a file
 sed 'G;G'
 # undo double-spacing (assumes even-numbered lines are always blank)
 sed 'n;d'
 # insert a blank line above every line which matches "regex"
 sed '/regex/{x;p;x;}'
 # insert a blank line below every line which matches "regex"
 sed '/regex/G'
 # insert a blank line above and below every line which matches "regex"
 sed '/regex/{x;p;x;G;}'
NUMBERING:
 # number each line of a file (simple left alignment). Using a tab (see
 # note on '\t' at end of file) instead of space will preserve margins.
 sed = filename | sed 'N;s/\n/\t/'
 # number each line of a file (number on left, right-aligned)
 sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'
 # number each line of file, but only print numbers if line is not blank
 sed '/./=' filename | sed '/./N; s/\n/ /'
 # count lines (emulates "wc -l")
 sed -n '$='
TEXT CONVERSION AND SUBSTITUTION:
 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
 sed 's/.$//'               # assumes that all lines end with CR/LF
 sed 's/^M$//'              # in bash/tcsh, press Ctrl-V then Ctrl-M
 sed 's/\x0D$//'            # works on ssed, gsed 3.02.80 or higher
 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
 sed "s/$/`echo -e \\\r`/"            # command line under ksh
 sed 's/$'"/`echo \\\r`/"             # command line under bash
 sed "s/$/`echo \\\r`/"               # command line under zsh
 sed 's/$/\r/'                        # gsed 3.02.80 or higher
 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
 sed "s/$//"                          # method 1
 sed -n p                             # method 2
 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
 # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
 # UnxUtils version can be identified by the custom "--text" switch
 # which appears when you use the "--help" switch. Otherwise, changing
 # DOS newlines to Unix newlines cannot be done with sed in a DOS
 # environment. Use "tr" instead.
 sed "s/\r//" infile >outfile         # UnxUtils sed v4.0.7 or higher
 tr -d \r <infile >outfile            # GNU tr version 1.22 or higher
 # delete leading whitespace (spaces, tabs) from front of each line
 # aligns all text flush left
 sed 's/^[ \t]*//'                    # see note on '\t' at end of file
 # delete trailing whitespace (spaces, tabs) from end of each line
 sed 's/[ \t]*$//'                    # see note on '\t' at end of file
 # delete BOTH leading and trailing whitespace from each line
 sed 's/^[ \t]*//;s/[ \t]*$//'
 # insert 5 blank spaces at beginning of each line (make page offset)
 sed 's/^/     /'
 # align all text flush right on a 79-column width
 sed -e :a -e 's/^.\{1,78\}$/ &/;ta'  # set at 78 plus 1 space
 # center all text in the middle of 79-column width. In method 1,
 # spaces at the beginning of the line are significant, and trailing
 # spaces are appended at the end of the line. In method 2, spaces at
 # the beginning of the line are discarded in centering the line, and
 # no trailing spaces appear at the end of lines.
 sed  -e :a -e 's/^.\{1,77\}$/ & /;ta'                     # method 1
 sed  -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/'  # method 2
 # substitute (find and replace) "foo" with "bar" on each line
 sed 's/foo/bar/'             # replaces only 1st instance in a line
 sed 's/foo/bar/4'            # replaces only 4th instance in a line
 sed 's/foo/bar/g'            # replaces ALL instances in a line
 sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
 sed 's/\(.*\)foo/\1bar/'            # replace only the last case
 # substitute "foo" with "bar" ONLY for lines which contain "baz"
 sed '/baz/s/foo/bar/g'
 # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
 sed '/baz/!s/foo/bar/g'
 # change "scarlet" or "ruby" or "puce" to "red"
 sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'   # most seds
 gsed 's/scarlet\|ruby\|puce/red/g'                # GNU sed only
 # reverse order of lines (emulates "tac")
 # bug/feature in HHsed v1.5 causes blank lines to be deleted
 sed '1!G;h;$!d'               # method 1
 sed -n '1!G;h;$p'             # method 2
 # reverse each character on the line (emulates "rev")
 sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
 # join pairs of lines side-by-side (like "paste")
 sed '$!N;s/\n/ /'
 # if a line ends with a backslash, append the next line to it
 sed -e :a -e '/\\$/N; s/\\\n//; ta'
 # if a line begins with an equal sign, append it to the previous line
 # and replace the "=" with a single space
 sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
 # add commas to numeric strings, changing "1234567" to "1,234,567"
 gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta'                     # GNU sed
 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'  # other seds
 # add commas to numbers with decimal points and minus signs (GNU sed)
 gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'
 # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
 gsed '0~5G'                  # GNU sed only
 sed 'n;n;n;n;G;'             # other seds
SELECTIVE PRINTING OF CERTAIN LINES:
 # print first 10 lines of file (emulates behavior of "head")
 sed 10q
 # print first line of file (emulates "head -1")
 sed q
 # print the last 10 lines of a file (emulates "tail")
 sed -e :a -e '$q;N;11,$D;ba'
 # print the last 2 lines of a file (emulates "tail -2")
 sed '$!N;$!D'
 # print the last line of a file (emulates "tail -1")
 sed '$!d'                    # method 1
 sed -n '$p'                  # method 2
 # print the next-to-the-last line of a file
 sed -e '$!{h;d;}' -e x              # for 1-line files, print blank line
 sed -e '1{$q;}' -e '$!{h;d;}' -e x  # for 1-line files, print the line
 sed -e '1{$d;}' -e '$!{h;d;}' -e x  # for 1-line files, print nothing
 # print only lines which match regular expression (emulates "grep")
 sed -n '/regexp/p'           # method 1
 sed '/regexp/!d'             # method 2
 # print only lines which do NOT match regexp (emulates "grep -v")
 sed -n '/regexp/!p'          # method 1, corresponds to above
 sed '/regexp/d'              # method 2, simpler syntax
 # print the line immediately before a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{g;1!p;};h'
 # print the line immediately after a regexp, but not the line
 # containing the regexp
 sed -n '/regexp/{n;p;}'
 # print 1 line of context before and after regexp, with line number
 # indicating where the regexp occurred (similar to "grep -A1 -B1")
 sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
 # grep for AAA and BBB and CCC (in any order)
 sed '/AAA/!d; /BBB/!d; /CCC/!d'
 # grep for AAA and BBB and CCC (in that order)
 sed '/AAA.*BBB.*CCC/!d'
 # grep for AAA or BBB or CCC (emulates "egrep")
 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    # most seds
 gsed '/AAA\|BBB\|CCC/!d'                        # GNU sed only
 # print paragraph if it contains AAA (blank lines separate paragraphs)
 # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
 # print paragraph if it contains AAA and BBB and CCC (in any order)
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
 # print paragraph if it contains AAA or BBB or CCC
 sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
 gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d'         # GNU sed only
 # print only lines of 65 characters or longer
 sed -n '/^.\{65\}/p'
 # print only lines of less than 65 characters
 sed -n '/^.\{65\}/!p'        # method 1, corresponds to above
 sed '/^.\{65\}/d'            # method 2, simpler syntax
 # print section of file from regular expression to end of file
 sed -n '/regexp/,$p'
 # print section of file based on line numbers (lines 8-12, inclusive)
 sed -n '8,12p'               # method 1
 sed '8,12!d'                 # method 2
 # print line number 52
 sed -n '52p'                 # method 1
 sed '52!d'                   # method 2
 sed '52q;d'                  # method 3, efficient on large files
 # beginning at line 3, print every 7th line
 gsed -n '3~7p'               # GNU sed only
 sed -n '3,${p;n;n;n;n;n;n;}' # other seds
 # print section of file between two regular expressions (inclusive)
 sed -n '/Iowa/,/Montana/p'             # case sensitive
SELECTIVE DELETION OF CERTAIN LINES:
 # print all of file EXCEPT section between 2 regular expressions
 sed '/Iowa/,/Montana/d'
 # delete duplicate, consecutive lines from a file (emulates "uniq").
 # First line in a set of duplicate lines is kept, rest are deleted.
 sed '$!N; /^\(.*\)\n\1$/!P; D'
 # delete duplicate, nonconsecutive lines from a file. Beware not to
 # overflow the buffer size of the hold space, or else use GNU sed.
 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
 # delete all lines except duplicate lines (emulates "uniq -d").
 sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
 # delete the first 10 lines of a file
 sed '1,10d'
 # delete the last line of a file
 sed '$d'
 # delete the last 2 lines of a file
 sed 'N;$!P;$!D;$d'
 # delete the last 10 lines of a file
 sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
 sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
 # delete every 8th line
 gsed '0~8d'                           # GNU sed only
 sed 'n;n;n;n;n;n;n;d;'                # other seds
 # delete lines matching pattern
 sed '/pattern/d'
 # delete ALL blank lines from a file (same as "grep '.' ")
 sed '/^$/d'                           # method 1
 sed '/./!d'                           # method 2
 # delete all CONSECUTIVE blank lines from file except the first; also
 # deletes all blank lines from top and end of file (emulates "cat -s")
 sed '/./,/^$/!d'          # method 1, allows 0 blanks at top, 1 at EOF
 sed '/^$/N;/\n$/D'        # method 2, allows 1 blank at top, 0 at EOF
 # delete all CONSECUTIVE blank lines from file except the first 2:
 sed '/^$/N;/\n$/N;//D'
 # delete all leading blank lines at top of file
 sed '/./,$!d'
 # delete all trailing blank lines at end of file
 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'  # works on all seds
 sed -e :a -e '/^\n*$/N;/\n$/ba'        # ditto, except for gsed 3.02.*
 # delete the last line of each paragraph
 sed -n '/^$/{p;h;};/./{x;/./p;}'
SPECIAL APPLICATIONS:
 # remove nroff overstrikes (char, backspace) from man pages. The 'echo'
 # command may need an -e switch if you use Unix System V or bash shell.
 sed "s/.`echo \\\b`//g"    # double quotes required for Unix environment
 sed 's/.^H//g'             # in bash/tcsh, press Ctrl-V and then Ctrl-H
 sed 's/.\x08//g'           # hex expression for sed 1.5, GNU sed, ssed
 # get Usenet/e-mail message header
 sed '/^$/q'                # deletes everything after first blank line
 # get Usenet/e-mail message body
 sed '1,/^$/d'              # deletes everything up to first blank line
 # get Subject header, but remove initial "Subject: " portion
 sed '/^Subject: */!d; s///;q'
 # get return address header
 sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
 # parse out the address proper. Pulls out the e-mail address by itself
 # from the 1-line return address header (see preceding script)
 sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'
 # add a leading angle bracket and space to each line (quote a message)
 sed 's/^/> /'
 # delete leading angle bracket & space from each line (unquote a message)
 sed 's/^> //'
 # remove most HTML tags (accommodates multiple-line tags)
 sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
 # extract multi-part uuencoded binaries, removing extraneous header
 # info, so that only the uuencoded portion remains. Files passed to
 # sed must be passed in the proper order. Version 1 can be entered
 # from the command line; version 2 can be made into an executable
 # Unix shell script. (Modified from a script by Rahul Dhesi.)
 sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1
 sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2
 # sort paragraphs of file alphabetically. Paragraphs are separated by blank
 # lines. GNU sed uses \v for vertical tab, or any unique char will do.
 sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
 gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'
 # zip up each .TXT file individually, deleting the source file and
 # setting the name of each .ZIP file to the basename of the .TXT file
 # (under DOS: the "dir /b" switch returns bare filenames in all caps).
 echo @echo off >zipup.bat
 dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat

End (kill) some application from command line

Different methods exist in Windows to end or kill some application from command line.

The normal way to do this is by using the TASKKILL command, for example to end FireFox one can use:

TASKKILL /F /IM “firefox.exe”


When you need to stop some command window it can be dangerous to just kill any cmd.exe, so one could use the window title to kill a specific batch window, for example to kill any batch window with name starting with “SIM-” I used:



TASKKILL /FI "WINDOWTITLE eq SIM-*"


But sometimes you’ll need to be more specific, I wanted an automated way to kill some Java application but only when it was started with some specific parameter. The only way I found to do so on Windows was by using the WMIC commands. This example show how I killed our Java monitor application which started as follows:



java -Dapp.prop.file.monitor="monitor_system.properties" -classpath SOME_JARS Monitor


Kill this specific Java instance:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" DELETE


Before killing your application, you want to make sure your query will result in the correct process, one can easily check the query by executing the similar wmic command without yet killing the process:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" get processid, name, commandline /VALUE


More examples and combinations that can be used with wmic are available on this page.



The Linux equivalent we used was pkill, for example to kill our simulators we used:



pkill -f "simulator.jar"

Sunday, April 19, 2009

SVN Automated repeated backup / dump – Windows / Linux

Every project using SVN should have an automated backup system in place for their SVN repository. We decided we wanted a weekly incremental backup. But we also wanted to easily create a new initial backup. I scripted it on Windows and Linux with the Shell/Batch scripts shown below.

For both scripts, the same methods is used: we keep track of the last backed up revision in a file called svn_backup_delta.txt. Every time the script is started, an incremental dump is exported and archived, starting from the last dumped revision till the latest available revision. If the latest revision is the same as the last dumped revision, nothing will happen.

If the svn_backup_delta.txt doesn’t exist or the revision could not be read correctly, a complete initial dump from revision 0 till the latest revision will be exported and archived. The scripts are called daily or weekly with a standard OS scheduler. When we want to have a new initial complete dump, we simply remove the svn_backup_delta.txt file.

The scripts can be a good starting point to create your own automated SVN dumping mechanism.

Windows backup_svn.bat Batch script. This script now expects the svn.exe and svnrdump.exe to be in the same folder as the script. It also requires a repos.txt and backup.properties files in the same folder. The output of the script will be stored in the same folder within the file backup_svn.log.

@echo off 
  TITLE %~n0 - see log %~n0.log 
  echo All details will be logged into %~n0.log 
  CALL :STARTBATCH >> %~dp0%~n0.log 2>&1 
  GOTO :EOF

:STARTBATCH     REM All properties are set in backup.properties     REM Variables that change in a for loop, should be marked with !VARIABLE! instead of %VARIABLE%     REM Necessary to use this, otherwise SET !VARIABLE! in for loop fails     setlocal enabledelayedexpansion     ::CONFIGURATION     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% CHECKING FOR CHANGES AT %DATE%: %TIME%     REM Get properties from backup.properties and set as variables     FOR /F "tokens=1,2 delims==" %%A IN (%~dp0backup.properties) DO (     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %%A: %%B     set %%A=%%B     ) 
  :END_CONFIGURATION

REM Loop through all repos 
  for /f "delims=," %%r in (%~dp0repos.txt) DO ( 
  echo. 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Starting SVN backup 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Using following configuration: 
  SET REPO_URL=%REPO_BASE%/%%r 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -Repository URL: "%REPO_BASE%/%%r" 
  SET LAST_DELTA_FILE=%DUMP_BASE%\%%r\svn_backup_delta_%%r.txt 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -File to keep track of last revision backed up: !LAST_DELTA_FILE! 
  SET DUMP_FILE=%DUMP_BASE%\%%r 
  SET Today=%Date: =0% 
  SET Today=!Today:~-4!%Date:~-7,2%%Date:~-10,2% 
  SET Now=%Time: =0% 
  FOR /F "tokens=1,2 delims=:.," %%A IN ("!Now!") DO SET Now=%%A%%B

IF NOT EXIST "!DUMP_FILE!" mkdir "!DUMP_FILE!" 
  %~dp0svn --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache info !REPO_URL! > %TEMP%\repoinfo.tmp

SET LAST_REV= 
  for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "%TEMP%\repoinfo.tmp"`) do (     SET LAST_REV=%%j     )     REM Check if !LAST_REV! retrieved of SVN is a number. If not, exit script.     echo !LAST_REV!| findstr /r "^[0-9]*$">nul || ( ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% ERROR: Last revision number is !LAST_REV! of %%r doesn't seem to be valid, aborting.     GOTO :END )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision: !LAST_REV!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta file: !LAST_DELTA_FILE!     REM Check if full backup exists, if true, do an incremental backup     IF NOT EXIST !LAST_DELTA_FILE! (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Not delta file found, processing full export         call :FULL     ) ELSE (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Delta found, processing delta         call :DELTA     ) 
  ) 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% All repo's done, backup finalized 
  GOTO :END

REM Do an incremental backup 
  :DELTA     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% DELTA     SET LAST_DELTA=     for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "!LAST_DELTA_FILE!"`) do (         SET LAST_DELTA=%%j     )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta:!LAST_DELTA!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision:!LAST_REV!     IF "!LAST_DELTA!"=="" GOTO FULL     IF "!LAST_REV!"=="" GOTO :END     IF "!LAST_DELTA!"=="!LAST_REV!" GOTO :END     REM Check if !LAST_DELTA! is a number. If not, exit script.         echo !LAST_DELTA!| findstr /r "^[1-9][0-9]*$">nul || ( ECHO ERROR: DELTA REVISION NUMBER IN !LAST_DELTA_FILE! OF %REPO_URL% DOESN'T SEEM TO BE A NUMBER... ABORTING BACKUP...     GOTO :END )     SET /A INCRE_LAST_DELTA=LAST_DELTA+1     SET DUMP_FILE_NAME=!Today!-!Now!-INCREMENTAL-R!INCRE_LAST_DELTA!-TO-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Creating dump file name: !DUMP_FILE_NAME!, !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r !INCRE_LAST_DELTA!:!LAST_REV! --incremental > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

REM Do a full backup 
  :FULL     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% FULL     ECHO FULL > !LAST_DELTA_FILE!     SET DUMP_FILE_NAME=!Today!-!Now!-INITIAL-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file name: !DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file: !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r 0:!LAST_REV! > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

:END     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% END     GOTO :EOF


Linux svnbackup.sh Shell script:



#!/bin/sh
#SETUP
REPO_URL=https://url:8443/base/project
REPO_PATH=c:/repository/project
DUMP_DIR=/home/user/dumps
LAST_DELTA_FILE=svn_backup_delta.txt
#BEGINING OF THE SCRIPT ITSELF
date=$(date +%Y%m%d)
LOG=dump-$date.log
LAST_REV=$(svn info $REPO_URL |grep Revision |gawk ' { print $2}')
#Both files MUST exist, otherwise, wer'e going to init new "delting"
if [ -e $LAST_DELTA_FILE ] ; then
LAST_DELTA=$(cat $LAST_DELTA_FILE)
if [ $LAST_DELTA -ne $LAST_REV ] ; then
DUMP_FILE_NAME=$date-INCREMENTAL-R$((LAST_DELTA+1))-TO-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAME
svnadmin dump $REPO_PATH -r $((LAST_DELTA +1)):$LAST_REV --incremental --deltas 2>>$LOG |gzip -c > $DUMP_FILE
else
echo nothing to do : last revision number is same as last delta number, run it again later !
exit 0
fi
else
DUMP_FILE_NAME=$date-INITIAL-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAME
svnadmin dump $REPO_PATH 2>>$LOG | gzip -c > $DUMP_FILE
fi
#Is all ok ?
if [ $? -eq 0 ] ; then
echo -n $LAST_REV > $LAST_DELTA_FILE
else
echo "ERROR :" $date "Problem occured while saving dump" >>$LOG
fi

Update 02/07/2010: Script tested and working in Windows 7 (in combination with Visual SVN server). Make sure to add the bin folder of Visual SVN server to your 'path' system variable to make the 'svn' command available system wide. Run the script with right-click 'Run as administrator'.

 

Update 27/06/2016 on Windows script to use a backup_properties.txt, repo.txt files and validate the retrieved revision and delta numbers before executing the windows backup

Sunday, April 12, 2009

Run custom application or script as a Windows service

If you need your application or script to be started, whenever Windows is booted, you need to install it as a service (so  it will even be started when you didn’t login yet). Microsoft offers a little tool to easily create your own service and make it run some batch file / application as a part of their Windows Server Resource Kit Tools. From this package, you only need srvany.exe and instsrv.exe to install your custom service, but you still need to perform all this manual configuration before your service will be running.

I created the following batch script to easily install or uninstall your custom application as a service. All required information will be requested interactively to the user step by step. No further manual configuration will be needed anymore. Errors will be shown with colors and attention points will be flashed.

@echo off
echo.
echo.
echo Use this script to install / uninstall your custom applications or scripts as a service and make sure they will be started whenever Windows is booted.
SET DFAULT_BACKGROUND_COLOR=0
SET DEFAULT_TEXT_COLOR=7
SET ERROR_BACKGROUND_COLOR=4
SET ERROR_TEXT_COLOR=F
SET ATTENTION_BACKGROUND_COLOR=0
SET ATTENTION_TEXT_COLOR=F
rem 0 = Black
rem 1 = Blue
rem 2 = Green
rem 3 = Aqua
rem 4 = Red
rem 5 = Purple
rem 6 = Yellow
rem 7 = White
rem 8 = Gray
rem 9 = Light Blue
rem A = Light Green
rem B = Light Aqua
rem C = Light Red
rem D = Light Purple
rem E = Light Yellow
rem F = Bright White
:MAIN_MENU
  COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  SET RETURN_DRAW_ATTENTION_POINT=MAIN_MENU_INPUT
  GOTO DRAW_ATTENTION
  :MAIN_MENU_INPUT
    echo.
    echo.
    SET INPUT=
    echo To Install a new custom service, press I.
    echo To Uninstall a previously created custom service, press U.
    echo To open the Windows Services application, press S.
    set /p INPUT=Press E to Exit: 
    if /i "%INPUT%" == "i" goto START_INSTALL
    if /i "%INPUT%" == "u" goto START_UNINSTALL
    if /i "%INPUT%" == "s" goto START_SERVICES
    if /i "%INPUT%" == "e" goto EXIT
    GOTO MAIN_MENU
  :END_MAIN_MENU_INPUT
  echo.     
:END_MAIN_MENU
  
:START_INSTALL
  rem INTERACTIVE MODE is used, lines below kept for reference
  :HARD_CODED_PATHS_AND_NAMES
  rem APP_NAME=REPLACE_THIS_WITH_YOUR_CUSTOM_APP_NAME
  rem APP_PATH=FULL_PATH_TO_CUSTOM_APP
  rem one can use %CD% in the path, which will be replace by the current directory
  rem uncomment following 2 lines if you want to use the folder name (where this bat file is run) as service name APP_NAME
  rem SET APP_NAME=
  rem for %%* in (.) do set APP_NAME=%%~n*
  
  :INTERACTIVE_INPUT_OF_PATHS_AND_NAMES
    :INTERACTIVE_INPUT_APP_NAME
      echo.
      SET APP_NAME=
      set /p APP_NAME=Provide the name of your custom application (will be used as the name of the service): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_NAME
    
    :INTERACTIVE_INPUT_OF_APP_PATH
      echo. 
      SET APP_PATH=
      set /p APP_PATH=Provide the full path of your custom application to run as a service (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_OF_APP_PATH
    
    :CHECK_CUSTOM_APP_PATH
      IF EXIST "%APP_PATH%" GOTO END_CHECK_CUSTOM_APP_PATH
      echo.
      echo Your custom application %APP_NAME% could not be found at %APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_CUSTOM_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
      
    :INTERACTIVE_INPUT_APP_FOLDER
      echo.
      SET APP_FOLDER=
      set /p APP_FOLDER=Provide the folder to be used as working directory for your custom application (press ENTER if not required) (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_PARAM
      
    :INTERACTIVE_INPUT_APP_PARAM
      echo.
      SET APP_PARAM=
      set /p APP_PARAM=Provide the parameters of your custom application (press ENTER if no parameters are required) (don't use quotes): 
      echo. 
    :END_INTERACTIVE_INPUT_APP_PARAM
    
    :INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
      echo. 
      SET INST_SERVICE_APP_PATH=
      set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe): 
    :END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
    
    :CHECK_INST_SERVICE_APP_PATH_INPUT
      if "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH
    :END_CHECK_INST_SERVICE_APP_PATH_INPUT
    
    :CHECK_INST_SERVICE_APP_PATH
      IF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH
      echo.
      echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_INST_SERVICE_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
    
    :INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
      echo. 
      SET SERVICE_APP_PATH=
      set /p SERVICE_APP_PATH=Provide the full path to srvany.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\srvany.exe): 
    :END_INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
    
    :CHECK_SERVICE_APP_PATH_INPUT
      if "%SERVICE_APP_PATH%"=="" goto DEFAULT_SERVICE_APP_PATH
    :END_CHECK_SERVICE_APP_PATH_INPUT
    
    :CHECK_SERVICE_APP_PATH
      IF EXIST "%SERVICE_APP_PATH%" GOTO END_CHECK_SERVICE_APP_PATH
      echo.
      echo srvany.exe could not be found at %SERVICE_APP_PATH%!
      echo Please try again!
      SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_SERVICE_APP_PATH
      GOTO DRAW_ERROR_ATTENTION
    :END_CHECK_SERVICE_APP_PATH
      COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  :END_INTERACTIVE_INPUT_OF_PATHS_AND_NAMES  
  
  :SHOW_CONFIG
    echo.
    echo Ready to install your custom application, using the following configuration:
    echo.
    echo   Custom application name: %APP_NAME%
    echo   Custom application full path: %APP_PATH%
    echo   Custom application parameters: %APP_PARAM%
    echo   Custom application working directory: %APP_FOLDER%
    echo   srvany.exe full path: %SERVICE_APP_PATH%
    echo   instsrv.exe full path: %INST_SERVICE_APP_PATH%
    echo.
    SET RETURN_DRAW_ATTENTION_POINT=SHOW_CONFIG_WAIT
    GOTO DRAW_ATTENTION
    :SHOW_CONFIG_WAIT
      COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
      set INPUT=
      set /p INPUT=Press ENTER to continue installation, all other will return to main menu: 
      if /i not "%INPUT%" == "" GOTO MAIN_MENU
      COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
    :END_SHOW_CONFIG_WAIT
  :END_SHOW_CONFIG
  
  :INSTALL_SERVICE
    "%INST_SERVICE_APP_PATH%" "%APP_NAME%" "%SERVICE_APP_PATH%"
    echo.
    echo Service installed : %APP_NAME%
    echo.
  :END_INSTALL_SERVICE
  
  :UPDATE_REGISTRY
    REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "Application" /t REG_SZ /d "%APP_PATH%" /f
    IF NOT "%APP_FOLDER%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppDirectory" /t REG_SZ /d "%APP_FOLDER%" /f
    IF NOT "%APP_PARAM%"=="" REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\Parameters" /v "AppParameters" /t REG_SZ /d "%APP_PARAM%" /f
    echo.
    echo Registry updated to run %APP_PATH% with service %APP_NAME%
    echo.
  :END_UPDATE_REGISTRY
  
  :START_SERVICE
    SET RETURN_DRAW_ATTENTION_POINT=START_SERVICE_INPUT_REQUEST
    GOTO DRAW_ATTENTION
    :START_SERVICE_INPUT_REQUEST
      echo The service is now installed successfully (if no errors are logged above!)
      set INPUT=
      set /p INPUT=To start the service now, press Y. All others will return to main menu: 
      if /i not "%INPUT%" == "y" goto END_START_SERVICE
    :END_START_SERVICE_INPUT_REQUEST
    NET START "%APP_NAME%"
    echo.
    echo Service %APP_NAME% started
  :END_START_SERVICE
  
  GOTO END_START_INSTALL
  
  :DEFAULT_INST_SERVICE_APP_PATH
    echo Using default path for instsrv.exe %INST_SERVICE_APP_PATH%
    SET INST_SERVICE_APP_PATH=%CD%\instsrv.exe
    GOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT
  :END_DEFAULT_INST_SERVICE_APP_PATH
  
  :DEFAULT_SERVICE_APP_PATH
    echo Using default path for srvany.exe %SERVICE_APP_PATH%
    SET SERVICE_APP_PATH=%CD%\srvany.exe
    GOTO END_CHECK_SERVICE_APP_PATH_INPUT
  :END_DEFAULT_SERVICE_APP_PATH
:END_START_INSTALL
  GOTO MAIN_MENU
:START_SERVICES
  start "Services" services.msc
:END_START_SERVICES
  GOTO MAIN_MENU
:START_UNINSTALL
  :INTERACTIVE_INPUT_APP_NAME_UNINSTALL
    echo.
    SET APP_NAME=
    set /p APP_NAME=Provide the name of your custom application (the name of the service to uninstall): 
    echo. 
  :END_INTERACTIVE_INPUT_APP_NAME_UNINSTALL
    
  :INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
    echo. 
    SET INST_SERVICE_APP_PATH=
    set /p INST_SERVICE_APP_PATH=Provide the full path to instsrv.exe (don't use quotes). LEAVE EMPTY to use default path (%CD%\instsrv.exe): 
  :END_INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
  
  :CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
    if "%INST_SERVICE_APP_PATH%"=="" goto DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
  :END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
  
  :CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    IF EXIST "%INST_SERVICE_APP_PATH%" GOTO END_CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    echo.
    echo instsrv.exe could not be found at %INST_SERVICE_APP_PATH%!
    echo Please try again!
    SET RETURN_DRAW_ERROR_ATTENTION_POINT=INTERACTIVE_INPUT_OF_INST_SERVICE_APP_PATH_UNINSTALL
    GOTO DRAW_ERROR_ATTENTION
  :END_CHECK_INST_SERVICE_APP_PATH_UNINSTALL
    COLOR %DFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  
  :STOP_SERVICE
    NET STOP %APP_NAME%
    echo.
    echo Service %APP_NAME% stopped
    echo.
  :END_STOP_SERVICE
  
  :REMOVE_SERVICE
    "%INST_SERVICE_APP_PATH%" %APP_NAME% REMOVE
    echo.
    echo Service %APP_NAME% removed
    echo.
  :END_REMOVE_SERVICE
  
  :UPDATE_REGISTRY_UNINSTALL
    REG DELETE HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\%APP_NAME%\ /va /f
    echo.
    echo Registry cleaned up for service %APP_NAME%
    echo.
  :END_UPDATE_REGISTRY_UNINSTALL
  
  GOTO END_START_UNINSTALL
  
  :DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
    echo Using default path for instsrv.exe %INST_SERVICE_APP_PATH%
    SET INST_SERVICE_APP_PATH=%CD%\instsrv.exe
    GOTO END_CHECK_INST_SERVICE_APP_PATH_INPUT_UNINSTALL
  :END_DEFAULT_INST_SERVICE_APP_PATH_UNINSTALL
:END_START_UNINSTALL
  GOTO MAIN_MENU
  
:EXIT_WITH_PAUSE
  echo The end. Comments and updates: http://myTselection.blogspot.com, Copyright MightyMouse. Copyright srvany and instsrv Microsoft.
  pause
  GOTO EXIT
:END_EXIT_WITH_PAUSE
  
:EXIT
  exit
:END_EXIT
:DRAW_ATTENTION
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ATTENTION_BACKGROUND_COLOR%%ATTENTION_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
:END_DRAW_ATTENTION
  GOTO %RETURN_DRAW_ATTENTION_POINT%
:DRAW_ERROR_ATTENTION
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %DEFAULT_BACKGROUND_COLOR%%DEFAULT_TEXT_COLOR%
  @PING 1.1.1.1 -n 1 -w 100 >NUL
  COLOR %ERROR_BACKGROUND_COLOR%%ERROR_TEXT_COLOR%
:END_ERROR_DRAW_ATTENTION
  GOTO %RETURN_DRAW_ERROR_ATTENTION_POINT%
@echo on


A full package to easily install your own custom application as a service can be downloaded here, sources (zip).



Update (26/04/09): Added possibility to specify application working directory and command line parameters. (AppDirectory and AppParameter registry keys.)

Update (09/10/2011): New links