Thursday, August 14, 2008

TrixBox Backup

Mondoarchive is a backup solution for *nix systems. It creates .iso files with cd or dvd sizes containing the entire image of disk (like ghost for Windows). It's an extremely powerful solution 'cause, when burned, images are bootable and autorestoring. In this way it's possible to recover the system installation without the reinstallation of all sofware and drivers, for example in case of disk crash.
Moreover, the startup/config script allow to choose where to create image files: on disk, on usb devices, on samba/nfs shares or on ftp.
For any other information, please visit developers site: http://www.mondorescue.org/
The procedure below is optimized for backupping Trixbox versions 2.4 and 2.6 and Asterisk-based systems.

Installation
As root user, do:
cd ~
wget http://www.dbtek.it/Repository/install-mondo.tgz
(or from http://www.astusers.org/install-mondo.tgz)
tar -xzvf install-mondo.tgz
chmod +x install-mondo.sh
./install-mondo.sh

This script downloads all dependencies, install mondo backup program and create a custom configuration for trixbox.
At the installation end, it's necessary to reboot the system.

Next we have to edit the /etc/cron.weekly/mondobackup.cron file to activate the desired backup options and to configure the program. Every instruction needed are written directly into this config file.

To test if the procedure is working as we want: /etc/cron.weekly/mondobackup.cron

WARNING: if there are errors on mounting samba file systems (Netowrk Sharing on Microsoft OS) and yours username or password used for accessing shares contains symbols you must write a ' before and after it.


Restore a backup
To restore a backup we can choose from diffrent ways:

Nuke (formats the destination disk and automatically restores all)
Interactive
Expert

It's also possible to try to restore only the bootloader (if the disk didn't crash):
• Boot from backup CD/DVD and choose Expert Mode
• Type: mondorestore --mbr
• Choose option 28 and then
bash# mount-me
bash# chroot /mnt/RESTORING
bash# lilo or grub-install ’(hd0)’
bash# exit
bash# unmount-me

Nuke Restore
As said, this option completely overwrites the system hard disk, automatically restoring the backup

• Boot from first backsup CD/DVD
• Choose RESTORE
• When asked, insert next CD/DVD (if exists)
• Check for errors

If there are errors, check /tmp/mondorestore.log file.

If you wanna see what the program is doing, press + to analyse the log file “on-the-fly”.

Interactive Restore
The Interactive Mode allow to restore a subset of files from backup or we can use it to restore all files without formatting hard drive.

The interactive restore give the “Editing mountlist screen” that allow to set diffrent sidk geometries. To move through the partitions use up and down arrow keys. To move on buttons use left and right arrow keys.

To restore a subset of files, the procedure is:
• Boot from first backup CD/DVD
• Type interactive
• Answer to questions:
Do you want to partition your devices? no
Do you want to format them? no
Do you want to restore everything? no
Do you want to restore something? yes
Which path do you want to restore? /mydata (for example)
Do you want to run LILO to setup your boot sectors? Yes

Expert Restore
To manually restore:
• Boot from first backup CD/DVD
• Type expert
• Type mondorestore

Saturday, July 12, 2008

Error management on ASP.Net pages

How are managed errors ASP.Net pages?

A way is to modify the event "onError", in Global.asax, and make a server transfer to errors-management pages, tipically for error 500 and error 404.
We can also insert here the code to send a mail in case of error.

But the best thing is to create an HttpModule to intercept error events:

C#
public class ErrorModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.Error += new System.EventHandler(context_Error);
    }

    private void context_Error(object sender, System.EventArgs e)
    {
        Exception exception = HttpContext.Current.Server.GetLastError();
    }

    public void Dispose()
    {
    ...
    }
}

VB.Net
Public Class ErrorModule
    Inherits IHttpModule

    Public Sub Init(ByVal context As HttpApplication)
        AddHandler context.Error, AddressOf Me.context_Error
    End Sub

    Private Sub context_Error(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim exception As Exception = HttpContext.Current.Server.GetLastError
    End Sub

    Public Sub Dispose()
        ...
    End Sub
End Class

and then register it in the Web.config:

<system.web>
    <httpModules>
        <add name="ErrorModule" type="ErrorModule" />
    </httpModules>
</system.web>



HttpModules are "the better ways", 'cause permit reusability and its possible to enable or disable them from Web.config without recompile.

Friday, June 20, 2008

String replacement with Tex or nText fields

Problem: How can I replace a string with another one in a field of Text or nText datatype?

Considerations: If the field was of VarChar or nVarChar type, there is no problem, 'cause in SqlServer exists a function (like in T-Sql) that do it in native mode: the "Replace" function. But this function doesn't work on Text and nText fields. So, we have to use a script builded with another function: UpdateText

Solution:
declare @OldText varchar(1000)
set @OltText= 'Old'

declare curs cursor local fast_forward
for
select
    rowid,
    textptr(Field),
    charindex(@OldText, Field)-1
from
    Table
where
    Field
like
    '%' + @OldText +'%'


declare @NewText varchar(1000)
set @NewText= 'New'

declare @txtlen int
set @txtlen = len(@OldText)


declare @ptr binary(16)
declare @pos int
declare @id int


open curs

fetch next from curs into @id, @ptr, @pos

while @@fetch_status = 0
begin
    updatetext Table.Field @ptr @pos @txtlen @NewText

    fetch next from curs into @id, @ptr, @pos
end

close curs
deallocate curs

Notes:
Table is the reference table
Field is the field where the text will be replaced
rowid is the table id (in this example it's of int type, so the @id variable are set like int, for other datatypes change the declaration of @id variable)

Tuesday, June 10, 2008

Database Connection String for Web.Config

Access Database
<connectionstrings>
  <add name="AccessConnectionString"
   connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
   Data Source=|DataDirectory|dbname.mdb"
   providerName="System.Data.OleDb" />
</connectionstrings>


ODBC Database
<connectionstrings>
  <add name="ODBCConnectionString"
   connectionString="DSN=dsnname;Uid=sa;Pwd=pwd;
   providerName="System.Data.Odbc” />
</connectionstrings>


SQLServer database
<connectionstrings>
  <add name="OLEDBConnectionString"
   connectionString="Server=servername;Integrated Security=SSPI;Database=dbname;"
   providerName="System.Data.SqlClient" />
</connectionstrings>


Excel Connection
<connectionstrings>
  <add name="OLEDBConnectionString"
   connectionString="Provider=Microsoft.Jet.OLEDB.4.0;
   Data Source= filename.xls; Extended Properties=Excel 8.0;"; />
</connectionstrings>


.csv file Connection
<connectionstrings>
  <add name="OLEDBConnectionString"
   connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=filename.csv;
   ExtendedProperties=""Text;HDR=Yes;FMT=Delimited""";/>
</connectionstrings>



Moreover you can use native connection methods for Oracle.

Wednesday, June 4, 2008

Ajax - Response.Write with UpdatePanel

Usually, the Response.Write() method inside an UpdatePanel doesn't work.
To solve this problem, a workaround is to insert a LinkButton out of the UpdatePanel and to associate this control with the postback generated from the object inside the panel.


HTML
<asp:UpdatePanel ID="upd" runat="server">
    …
    <asp:Button runat="server" ID="BUTTON" Text="Click" />
    …
    </asp:UpdatePanel>
<asp:LinkButton ID="ALinkButton" runat="server"></asp:LinkButton>



CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    …
    AddHandler Me.ALinkButton.Click, AddressOf FUNCTION
    Me.BUTTON.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(Me.ALinkButton, ""))

    If Not Page.IsPostBack Then
        …
    End If
    …
End Sub

Protected Sub FUNCTION(ByVal sender As Object, ByVal e As System.EventArgs)
    …
    Response.Write("Text")
    …
End Sub