An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ file ’

I was given the task of setting up a nightly back up of files located on one Linux Machine (Red Hat) to another Linux Machine (Red Hat). All the files requiring backup are located in one folder. This folder contains both files and folders (which can contain more files and folders). Each files is only very small (couple bytes each) and once a file is created, it is never changed or edited).

Having not a very clear idea on how to approach this task, I began to experiment and look for various other methods. My searches only gave way to many applications specializing in performing backups, none of which I really wanted to implement.

I realized that if I mounted the folder containing files from the host machine to the backup machine, and then performed a simple cp command. After running man cp, I came across the following command that would accomplish what I needed. Therefore, after mounting the folder as a directory, I do all these changes the Backup Machine.


cp -upR --reply=yes /mountedDirectory /backupDirectory

The following is a breakdown of what each command means.

-u
copy only when the SOURCE file is newer than the destination file or when the destination file is missing

-p (same as –preserve=mode, ownership, timestamp)
preserve the specified attributes (default:mode,ownership,timestamp) and security context, if possible attributes: links, all

-R
copy directories recursively

–reply={yes, no, query}
specify how to handle the prompt about an existing destination file.

During the testing phase, I found that the first execution took the longest (above six minutes), but each susequent execution was shorter (about one minute). I figured that most of the time spent during the first execution was due to many copy operations where most of the time spent during each subsequent execution was comparing.

The next step was to put my single line command into a script file. This is done by creating a *.sh file. I decided to put this file where the rest of these files live, which is mainly /bin. Create a new file with the following format and give it a name. In this example, we are using nightlyBackup.sh.


#!/bin/sh
cp -upR --reply=yes /mountedDirectory /backupDirectory

Next, navigate to the files and change the chmod. We want to chmod to 777 to give the owner read, write, and execute priveliges. The group and others will get read and execute preiveliges. This can be accomplished by:


chmod777 nightlyBackup.sh

The next step to get this simple singe line script to run nightly. This I found could be accomplished by running a crontab. A crontab is a linux application that will run scripts at specified times (similiar to schedulers in Windows)

crontab -l will list all scripts set for execution.
crontab -e will bring up a prompt to add scripts.

Since I want mine to run sometime during the night each night, I set mine to by adding this to the file as a new line:


0 0 * * * /bin/nightlyBackup.sh

Save and close the file and now the script is set to run nightly. What is obvious in this setup is that both the host machine and the backup machine need to be up and running each night for this to work. Otherwise, this is a perfectly great solution that requires little setup and maintenance.

In C#, a common function in web pages containing data in a table is allowing the web user to export to excel. Through my experiences, I have uncovered that to export from a GridView (or DataGrid) to a Microsoft Excel (xls) file is messy and not easily customizable. Instead, I found that taking the underlying DataTable and instead exporting to a Microsoft Excel Comma Separated Value (CSV) file as a much better method. Below is the snippet of code for the function.

private void exportDataTableToCsv(DataTable formattedDataTable, string filename)
{
   DataTable toExcel = formattedDataTable.Copy();
   HttpContext context = HttpContext.Current;
   context.Response.Clear();

   foreach (DataColumn column in toExcel.Columns)
   {
      context.Response.Write(column.ColumnName + ",");
   }

   context.Response.Write(Environment.NewLine);
   foreach (DataRow row in toExcel.Rows)
   {
      for (int i = 0; i < toExcel.Columns.Count; i++)
      {
         context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
      }

      context.Response.Write(Environment.NewLine);
   }

   context.Response.ContentType = "text/csv";
   context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
   context.Response.End();
}

To call this function, simple create a C# LinkButton or Button Web Control. In your aspx page, your control should look similar to:

<asp:LinkButton ID="lbToExcel" runat="server">Export To Excel</asp:LinkButton>

From here, the lbToExcel_Click function will call the Export to DataTable function exportDataTableToCsv defined earlier in this post. I would also advise making edits at this point to your DataTable by removing columns that were hidden in the GridView (or DataGrid). We want to pass a formattedDataTable.

If your page uses Ajax’s Update Panel controls, it is required to set the lbToExcel web control as a PostBackTrigger as opposed to an AsyncPostBackTrigger. To add a PostBackTrigger in Visual Studio 2008’s Ajax Update Panel, find the properties of the Update Panel. Under Behavior, find Triggers and choose to edit the Collection. Next, Click Add and from the Dropdown select PostBackTrigger. Next, you need to manually (without the aid of intellisense) type in the control name. In our case, the control name was lbToExcel. The result should be the code snippet example below:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:LinkButton ID="lbToExcel" runat="server">Export To Excel</asp:LinkButton>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lbToExcel">
</asp:PostBackTrigger>
</Triggers>
</asp:UpdatePanel>

Overall, this has worked flawlessly for me. If you have any problems, feel free to drop a line describing your challenges and I can attempt to give you a hand!

After a clean installation of Red Hat Enterprise Linux 5 (RHEL5) with an active subscription, I came across an immediate problem when attempting to run an application. I get the following error:

libXm.so.3: cannot open shared object file: No such file or directory

The first step is to insure that the files are actually missing. The easiest way to confirm if the file exists on the system is to run the locate command. Try the below linux command below which will return a list of similar files.

locate libXm.so

The number after libXm.so is merely the version number. If you are missing libXm.so.3, but you have libXm.so.4, you can create a symbolic link (-s) from file libXm.so version 4 to version 3 with the below linux command.

ln -s libXm.so.4 libXm.so.3

It turns out that I was missing a motif package, and that by installing openmotif (a perfect motif alternative) will quickly resolve the issue. Try the following below:

yum install openmotif22.i386 openmotif22.x86_64

Say yes to both prompts to complete the openmotif installation.