Archive for March 25th, 2010
Nested Ifs using ? : notation
by z3n on Mar.25, 2010, under Coding, Tips & Hints
Problem:
I can’t tell if this is a good practice, but i don’t really care, it speeds up the process.
I’m talking about this specific IF notation:
$x=1;
// this:
if ($x == 1) {
// do something
} else {
// do something
}
// .. can be replaced with this
return ($x == 1) ? /* true */ : /* false */;
// however... this:
if ($x == 1) {
// do something
} else {
if ($x == 2) {
// do something
} else {
// do something
}
}
// can't be replaced with the ? : notation
Solution:
function foo($x) { return $x; }
return ($x == 1) ? /* x = 1 */ : foo(($x == 2) ? /* x=2 */ : /* x = other */);
Automatic Backups to gmail
by z3n on Mar.25, 2010, under Uncategorized
Problem:
Gmail is offering a considerable ammount of free space, now, it can be used to do automatic backups, specially if you run a database or something critical.
Solution:
They offer a lot of space … but you can’t get emails bigger than 20mb, so you need to do some tricks,
first a little bash script:
#!/bin/bash
s_ts() { date "+[%H:%M:%S%p]"; }
_dt=`date +%y%m%d`
echo \(c\) z3n - R1V1.06@070509 \(PiD:$$\)
cd /path/to/backup/folder
echo `s_ts` SQL Backup...
mysqldump -pMYSQL_PASSWORD --all-databases > mysql-"$_dt".sql
echo `s_ts` Compressing...
rar a -m5 -inul -s -rr3 -v19750k -df mysql-"$_dt".rar mysql-"$_dt".sql
cd ..;php ma.php mysql-"$_dt".rar
and then a ma.php to email attachments:
// (c) z3n - z3n666@gmail.com
define('from','backup@youdomain.com');
define('to','your.gmail@gmail.com');
define('subject','MySQL Backup - '.date("Y-m-d"));
define('base_path','/path/to/backup/');
set_time_limit(0);
ini_set("memory_limit","1G");
function send_attach_mail($email_from,$email_to,$email_subject,$file) {
$email_message = $file; // Message that the email has in it
$semi_rand=md5(time());
$mime_boundary="==Multipart_Boundary_x{$semi_rand}x";
$headers="From: ".$email_from."\nMIME-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"{$mime_boundary}\"";
$email_message.="This is a multi-part message in MIME format.\n\n--{$mime_boundary}\nContent-Type:text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n".$email_message."\n\n";
/* First File */
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = $file; // Filename that will be used for the file as the attachment
$rfile = fopen($file,'rb');
$data = fread($rfile,filesize($file));
fclose($rfile);
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\nContent-Type: {$fileatt_type};\n name=\"{$fileatt_name}\"\nContent-Transfer-Encoding: base64\n\n".$data."\n\n--{$mime_boundary}\n";
@mail($email_to, $email_subject, $email_message, $headers);
}
if (!isset($argv[1])) {
die("Usage: ".$_SERVER['PHP_SELF']." <filename>");
} else {
$argv[1]=str_replace("..","",$argv[1]);
if (!file_exists(base_path.$argv[1])) { // try to see if this is multipart file
for ($i=1;file_exists(substr(base_path.$argv[1],0,-3)."part0".$i.".rar");$i++) {
send_attach_mail(from,to,subject,substr(base_path.$argv[1],0,-3)."part0".$i.".rar");
}
} else {
send_attach_mail(from,to,subject,base_path.$argv[1]);
}
}
Done!
