Friday, May 27, 2011

MySQL backup on Linux using CRON

Hi,

I'm goint to show you how to set up an automatic mySQL backup script using Shell script, Perl and CRON.

First of all, we must create an user with the minimum privileges for using only mysqldump so I will set the LOCK TABLES privilege for the database which I want to dump. The command I use is:

mysqldump -uUSER -pPASSWORD --opt DATABASE > /backupfile.sql


Next step is to compress the resulting file, por example using

tar -czf BACKUPFILECOMPRESSED.tar.gz backupfile.sql


Now we can move our file wherever we want or sent it by email...

The final step is to include the script into the cron config file /etc/crontab. FOr example, I want to execute my scripts from Monday to Saturday three times per day at 11:15, 14:15 and 18:15.

15 11,14,18 * * 1-6 USER /PATH/SCRIPT


restart the cron services

/etc/init.d/cron restart


and done!.

Hope help

Wednesday, May 25, 2011

IPTABLES and pure-ftpd

Hello,

Today I've needed to set up a server using ssh with NO ftp and NO web servers. First of all, I've configured the ftp server for upload my files. More problems... my pc is on a secure network and I cannot use ssh so... I've used a ssh connection under a vpn.

I've decided to set up a pure-ftpd server on a JEOS linux distribution:

#apt-get install pure-ftpd


To give access to my server I've opened the 21 port using IPTABLEs

iptables -I INPUT 2 -p tcp --dport 21 -j ACCEPT
iptables -I INPUT 2 -p udp --dport 21 -j ACCEP
T

If you want to set it forever you can create a firewall script in your /etc/init.d folder, for example something like

#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin

IPTABLES=/sbin/iptables

$IPTABLES -P INPUT ACCEPT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -P FORWARD ACCEPT

# Delete old rules
$IPTABLES -F

echo -n "Setting firewall rules... "
test $# -eq 1 && test $1 = "stop" && echo "stopped." && exit 0

###### ICMP
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
$IPTABLES -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
$IPTABLES -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
$IPTABLES -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
$IPTABLES -A INPUT -p icmp -m state --state INVALID -j DROP
$IPTABLES -A OUTPUT -p icmp -m state --state INVALID -j DROP
$IPTABLES -A INPUT -p icmp -j DROP
$IPTABLES -A OUTPUT -p icmp -j DROP
######

$IPTABLES -A INPUT -s localhost -j ACCEPT
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Web and FTP
$IPTABLES -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
$IPTABLES -A INPUT -p tcp -m state --state NEW --dport 21 -j ACCEPT
$IPTABLES -A INPUT -p udp -m state --state NEW --dport 21 -j ACCEPT

... more rules ...

# Deny the rest
$IPTABLES -P INPUT DROP

echo " [done]"


We can use nmap for checking if the port is open from another computer

#nmap -p 21 SERVER_IP


and the rules from our server

#iptables -L -n -v


or see our net status

#netstat -putan


Next step is to configure the users, so I've created a new system user

adduser NEW_USER

and I add this user to the pure-ftp list of users

pure-pw user add NEW_USER -u NEW_USER -d /home/NEW_USER

That's all. Everything is working now.

Hope help

Sunday, May 15, 2011

Problem with self.window.rootViewController on iPad 3.2

Today I've needed to fix a graphical mistake in one application. The problem is that this application must work on different iOS versions and I can not use the rootViewController in the app delegate in the way:

self.window.rootViewController = self.viewController;


Finally, I found the solution doing the next.

1) Change this line in the app delegate and replace it by

[self.window addSubview:self.viewController.view];


2) Open MainWindow.xib and select the UIViewController. Check "Resize View From XIB" in the attributes inspector

3) Set the view’s frame to applicationFrame programmatically. For example:

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
appFrame = CGRectMake(0, 20, appFrame.size.width, appFrame.size.height);

self.viewController.view.frame=appFrame;


Next time I will explain how to know which iOS version is running in our device.

Hope help!

Saturday, May 7, 2011

How to convert NSData to NSString

Hi,

Today I need to extract and XML string from a NSData and it's something easy but not trivial

NSString* string = [[NSString alloc] initWithData:theData encoding:NSASCIIStringEncoding];


Hope help

Friday, April 22, 2011

Set UIColor from Hex Color in iPhone

We define the HexColor in our .h file

#define HexColor(color) [UIColor
colorWithRed:((color)&0xFF)/255.0
green:((color>>8)&0xFF)/255.0
blue:((color>>16)&0xFF)/255.0
alpha:((color>>24)&0xFF)/255.0]


Then we can set the color from the hex color string

unsigned int colorValue;

[[NSScanner scannerWithString: colorString] scanHexInt: &colorValue];
UIColor *color = HexColor(colorValue);


being the colorString AARRGGBB where

AA = alpha
RR = red
GG = gren
BB = blue

Another options I've seen on the Internet


// As a C function:
UIColor* UIColorFromRGB(NSInteger rgbValue) {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}



// As an Objective-C function:
- (UIColor *)UIColorFromRGB:(NSInteger)rgbValue {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}



Cheers!

Wednesday, April 13, 2011

Combobox in datagridviews binding classes and structures

Hi, my problem today is how to show the list of data from an structure into a datagridview using combobox in some columns.

First of all I was surfing on the internet and I found this for enums:

//CF: set to null before setting it to the current to
//clear out previous results
this.bindingSource1.DataSource = null;

this.bindingSource1.DataSource = MyDal.GetDataTable();

void _formatStatusColumn()
{
DataTable dt = new DataTable("Status");

//CF: actually how it's stored in the db.
dt.Columns.Add("Status", typeof(int));
dt.Columns.Add("Status_Name", typeof(string));

//CF: this way I can add or remove enum values, and
//the combo will always reflect the correct values.
foreach(int index in Enum.GetValues(typeof(Status)){
dt.Rows.Add(index, Enum.GetName(typeof(Status), index));
}

//CF: now for the UI column
DataGridViewComboBoxColumn statusColumn =
new DataGridViewComboBoxColumn();

//CF: seems to control where the column is placed.
statusColumn.DisplayIndex = 3;
statusColumn.HeaderText = "Status";

statusColumn.DataPropertyName = "Status";
statusColumn.DataSource = dt;
statusColumn.DisplayMember = "Status_Name";
statusColumn.ValueMember = "Status";

this.dataGridView1.Columns.Add(statusColumn);
}


This was very useful but I need something easier. I have to read a list and show the field in the combobox, so I've changed the code like that:

public void FormatResidenceColumn()
{
if (this.dataGridView1.Columns.Count > 0)
{
DataTable dt = new DataTable("Residence");

dt.Columns.Add("Residence", typeof(int));
dt.Columns.Add("Residence_Name", typeof(string));

this.dataGridView1.Columns.Remove("Residence");

if (Globals.ListOfResidences.Count > 0)
{
foreach (Residence obj in Globals.ListOfResidences)
{
dt.Rows.Add(obj.Id - 1, obj.Alias);
}
}
else
{
// Empty
dt.Rows.Add(0, "Empty");
}

//CF: now for the UI column
DataGridViewComboBoxColumn combColumn =
new DataGridViewComboBoxColumn();

//CF: seems to control where the column is placed.
combColumn.DisplayIndex = 4;
combColumn.HeaderText = "Residencia";

combColumn.DataPropertyName = "Residence";
combColumn.DataSource = dt;
combColumn.DisplayMember = "Residence_Name";
combColumn.ValueMember = "Residence";

this.dataGridView1.Columns.Add(combColumn);
}
}


where I use an global variable named ListOfResidence in this example.

Hope help!!

Sunday, April 3, 2011

How to launch Apple Store from my iphone code

Hi,

Today I will show you a simple function that launches the App Store from your application.

First of all you need to know your app's link for example looking for it in iTunes. And then write a code like this:


-(IBAction) gotoFlightInstrument {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/es/app/instrumental-flight-vuelo/id413551896?mt=8"]];
}


where
"http://itunes.apple.com/es/app/instrumental-flight-vuelo/id413551896?mt=8

is my iTunes app link.

Hope help!

Bye