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