~ overflow ~

Tag: automatic

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!

Leave a Comment :, , more...

Script to automatically build dvd / disk compilations targeting maximum usage

by z3n on Sep.19, 2009, under Coding

Problem:

I have lots of files, in lots of folders, to burn into lots of dvds.

I didn’t wanted to pick one by one in order to build the most effective compilation to use the most possible bytes on each dvd-r i would burn them into.

Solution:

I did a little script to do the job, it searches all the subfolders of a given path, and build file compilations in order to fit the maximum possible ammount of bytes. You can even set it to a different limit, for example, if you burn a cd-r or eventually a bd-r or anything else. Folder paths will be respected and files will be put sorted, this is not recommended if you are willing to burn lots of small files, it probably will mess with them distributing across many disks, making it less effective when you actually need those files.

Source:

1024) { if ($tmp > 1048576) { if ($tmp > 1073741824) { if ($tmp > 1099511627776) { if ($tmp > 1125899906842624) { if ($tmp > 1152921504606847000) { if ($tmp > 1180591620717411300000) { $u=”o”;$tmp=$tmp/1180591620717411300000; } else { $u=”e”;$tmp=$tmp/1152921504606847000; } } else { $u=”p”;$tmp=$tmp/1125899906842624; } } else { $u=”t”;$tmp=$tmp/1099511627776; } } else { $u=”g”;$tmp=$tmp/1073741824; } } else { $u=”m”;$tmp=$tmp/1048576; } } else { $u=”k”;$tmp=$tmp/1024; } } else { $u=”"; } return round($tmp, 2).” “.$u.”b”; }
  1. function _o($x) { echo $x."\n"; }
  2. function rddir($dir) { /* v2.11 */
  3.  global $tfile,$tdir,$ij;$i=0;$j=count($tdir);
  4.  if (is_dir($dir)) {
  5.   if ($dh=opendir($dir)) {
  6.    while (($file=readdir($dh)) !== false) {
  7.     if (is_dir($dir.$file)) {
  8.      if (($file != ".") &amp;&amp; ($file != "..")) {
  9.       $tdir[$j]=$dir.$file;$j++;
  10.     } } else { $tfile[$i]=$file;$i++;
  11. } } closedir($dh); } } $ij=$i; }
  12.  
  13. // main
  14.  
  15. if (!isset($argv[1])) { _o("Usage: ".$_SERVER['PHP_SELF']."  [move_to]\nEg: ".$_SERVER['PHP_SELF']." path/to/folder path/to/compilation/folder");die; }
  16.  
  17. define('move_to',(isset($_argv[2])) ? $argv[2] : "_disk_compilations_");
  18.  
  19. // collect file sizes and paths
  20. for ($tsk=$tdk=$tsz=$i=0,$files=array(),$sizes=array(),$flag=array(),$tdir[0]=$argv[1];$i 0) &amp;&amp; ($sz &lt;= dsize)) {
  21.      $files[]=$fn;
  22.      $sizes[]=$sz;
  23.      $flag[]=1;
  24.     } else {
  25.      _o($fn." skipped.");$tsk++;
  26.     }
  27.    } elseif ((is_dir($fn)) &amp;&amp; ($tfile[$j] != ".") &amp;&amp; ($tfile[$j] != "..")) {
  28.     $tdir[]=$fn;
  29.    }
  30.   }
  31.   unset($tfile);
  32.  }
  33. }
  34.  
  35. // build compilations
  36. _o("Building Compilations…");
  37. while (in_array(1,$flag)) { // loop until there's nothing left
  38.  for ($k=array_keys($flag,1),$fl=array(),$sz=$i=0,$j=count($k);$i&lt;$j;$i++) {
  39.   if (($sizes[$k[$i]]+$sz) &lt; dsize) { // search for files that fits/still fitting on this disk
  40.    $sz+=$sizes[$k[$i]];
  41.    $fl[]=$files[$k[$i]];
  42.    $flag[$k[$i]]=0;
  43.    $sd=move_to."/".$tdk."/".dirname($files[$k[$i]]);
  44.    if (!is_dir($sd)) {
  45.     mkdir($sd,0755,true);
  46.    }
  47.    if (debug) { // just create a zero byte file
  48.     touch(move_to."/".$tdk."/".$files[$k[$i]]);
  49.    } else {
  50.     rename($files[$k[$i]],move_to."/".$tdk."/".$files[$k[$i]]);
  51.    }
  52.   }
  53.  }
  54.  $tdk++;
  55.  $tsz+=$sz;
  56.  if (debug) {
  57.   _o(var_export($fl,true)."\nSize:".$sz."\n");
  58.  }
  59. }
  60.  
  61. _o("Found: ".bytec($tsz)." (".number_format($tsz)." bytes) in ".number_format(count($flag))." files (".$tsk." skipped)\nIdeal disk count: ".round($tsz/dsize,3)." did: ".$tdk);
  62.  
  63. ?&gt;

Download Source: Here

Leave a Comment :, , , , , , , , , more...

Ultimate PHP Resize Function

by z3n on Aug.15, 2009, under Coding

In the last few days I’ve been developing some new stuff that required my old img_resizer php function to be refurbished, I also found a issue with wBMP not being the same as BMP format.

Apart from that i got some extra file checking and the ability of recognizing the file format independent of the extension (this will only work with exif extension, which when not enabled will fall back to a try and error little loop).

There’s also some new stuff added, when you don’t want to automatically crop an `out of ratio` resized image you can enable the $nocrop option which will resize the image to the right ratio without cropping or stretching it.

So this is the v2.7, not sure if it will be the last one, but so far it’s the ultimate.

// (c) z3n – R1V2.7@090815 – www.overflow.biz – z3n666@gmail.com
  1.  
  2. function _o($x) { echo $x."’n"; }
  3.  
  4. function _ckdir($fn) {
  5.  if (strpos($fn,"/") !== false) {
  6.   $p=substr(substr($fn,0,strrpos($fn,"/")),0,250);
  7.   if (!is_dir($p)) {
  8.    mkdir($p,755,true);
  9.   }
  10.  }
  11. }
  12. function imagecreatefrombmp($p_sFile) { // by alexander@alexauto.nl at http://www.php.net/manual/en/function.imagecreatefromwbmp.php
  13.   // Load the image into a string
  14.   $file = fopen($p_sFile,"rb");
  15.   $read=fread($file,10);
  16.   while(!feof($file)&amp;&amp;($read&lt;&gt;""))
  17.    $read.=fread($file,1024);
  18.   $temp=unpack("H*",$read);
  19.   $hex=$temp[1];
  20.   $header=substr($hex,0,108);
  21.  
  22.   // Process the header
  23.   // Structure: http://www.fastgraph.com/help/bmp_header_format.html
  24.   if (substr($header,0,4) == "424d") {
  25.     // Cut it in parts of 2 bytes
  26.     $header_parts=str_split($header,2);
  27.  
  28.     // Get the width4 bytes
  29.     $width=hexdec($header_parts[19].$header_parts[18]);
  30.  
  31.     // Get the height4 bytes
  32.     $height=hexdec($header_parts[23].$header_parts[22]);
  33.  
  34.     // Unset the header params
  35.     unset($header_parts);
  36.   }
  37.  
  38.   // Define starting X and Y
  39.   $x=0;
  40.   $y=1;
  41.  
  42.   // Create newimage
  43.   $image=imagecreatetruecolor($width,$height);
  44.  
  45.   // Grab the body from the image
  46.   $body=substr($hex,108);
  47.  
  48.   // Calculate if padding at the end-line is needed
  49.   // Divided by two to keep overview.
  50.   // 1 byte = 2 HEX-chars
  51.   $body_size=(strlen($body)/2);
  52.   $header_size=($width*$height);
  53.  
  54.   // Use end-line padding? Only when needed
  55.   $usePadding=($body_size&gt;($header_size*3)+4);
  56.  
  57.   // Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption
  58.   // Calculate the next DWORD-position in the body
  59.   for ($i=0;$i&lt;$body_size;$i+=3) {
  60.     // Calculate line-ending and padding
  61.     if ($x&gt;=$width) {
  62.       // If padding needed, ignore image-padding
  63.       // Shift i to the ending of the current 32-bit-block
  64.       if ($usePadding)
  65.        $i+=$width%4;
  66.  
  67.       // Reset horizontal position
  68.       $x=0;
  69.  
  70.       // Raise the height-position (bottom-up)
  71.       $y++;
  72.  
  73.       // Reached the image-height? Break the for-loop
  74.       if ($y &gt; $height)
  75.        break;
  76.     }
  77.  
  78.     // Calculation of the RGB-pixel (defined as BGR in image-data)
  79.     // Define $i_pos as absolute position in the body
  80.     $i_pos=$i*2;
  81.     $r=hexdec($body[$i_pos+4].$body[$i_pos+5]);
  82.     $g=hexdec($body[$i_pos+2].$body[$i_pos+3]);
  83.     $b=hexdec($body[$i_pos].$body[$i_pos+1]);
  84.  
  85.     // Calculate and draw the pixel
  86.     $color=imagecolorallocate($image,$r,$g,$b);
  87.     imagesetpixel($image,$x,$height-$y,$color);
  88.  
  89.     // Raise the horizontal position
  90.     $x++;
  91.   }
  92.  
  93.   // Unset the body / free the memory
  94.   unset($body);
  95.  
  96.   // Return image-object
  97.   return $image;
  98. }
  99. function img_force($src,$quality,$w,$h,$saveas,$nocrop,$force) {
  100.  foreach (array("jpg","gif","png") as $x) { // will skip bmp formats
  101.   if ($x != $force) {
  102.    if (img_resizer($src,$quality,$w,$h,$saveas,$nocrop,$x)) {
  103.     return 1;
  104.     break;
  105.    }
  106.   }
  107.  }
  108.  return 0;
  109. }
  110. function img_resizer($src,$quality,$w,$h,$saveas,$nocrop=0,$force=0) { /* v2.71 with auto crop, no crop, automatic file type detection when avalible (requires exif library) */
  111.  
  112.  if (!is_file($src)) { _o("Source is not a file.");return 0; }
  113.  if (!is_readable($src)) { _o("Can't read: ".$src);return 0; }
  114.  if ((file_exists($saveas)) && (!is_writeable($saveas))) { _o("Can't write to: ".$saveas);return 0; }
  115.  
  116.  $r=1;
  117.  
  118.  if (function_exists('exif_imagetype')) { // automatic filetype detection
  119.    $e=exif_imagetype($src);
  120.    if ($e == IMAGETYPE_JPEG) {
  121.     if (!$OldImage=@ImageCreateFromJpeg($src)) { $r=0; }
  122.    } elseif ($e == IMAGETYPE_GIF) {
  123.     if (!$OldImage=@ImageCreateFromGif($src)) { $r=0; }
  124.    } elseif ($e == IMAGETYPE_BMP) { // custom
  125.     if (!$OldImage=@ImageCreateFrombmp($src)) { $r=0; }
  126.    } elseif ($e == IMAGETYPE_WBMP) {
  127.     if (!$OldImage=@ImageCreateFromwbmp($src)) { $r=0; }
  128.    } elseif ($e == IMAGETYPE_PNG) {
  129.     if (!$OldImage=@ImageCreateFromPng($src)) { $r=0; }
  130.    } else {
  131.     _o("Not a Valid Image! (".$e.") — ".$src);$r=0;
  132.    }
  133.  } else { // filename based filetype detection, might be subject to incorrect file extensions, resulting on a error, which when enabled will fall into a try and error `force` mode
  134.    $e=($force == "0") ? strtolower(substr($src,strrpos($src,".")+1)) : $force;
  135.    if (($e == "jpg") || ($e == "jpeg")) {
  136.     if (!$OldImage=@ImageCreateFromJpeg($src)) { $r=0; }
  137.    } elseif ($e == "gif") {
  138.     if (!$OldImage=@ImageCreateFromGif($src)) { $r=0; }
  139.    } elseif ($e == "bmp") { // this is NOT wbmp
  140.     if (!$OldImage=@ImageCreateFrombmp($src)) { $r=0; }
  141.    } elseif ($e == "png") {
  142.     if (!$OldImage=@ImageCreateFromPng($src)) { $r=0; }
  143.    } else {
  144.     _o("Not a Valid Image! (".$e.") — ".$src);$r=0;
  145.    }
  146.  }
  147.  
  148.  if ($r) {
  149.   list($width,$height)=getimagesize($src);
  150.   // check if ratios match
  151.   $_ratio=array($width/$height,$w/$h);
  152.   if ($_ratio[0] != $_ratio[1]) { // ratio don't match
  153.    if ($nocrop) { // since we can't crop we will need to do image on a different height/width
  154.       $hx = (100 / ($width / $w)) * .01;
  155.       $hx = @round ($height * $hx);
  156.  
  157.       $wx = (100 / ($height / $h)) * .01;
  158.       $wx = @round ($width * $wx);
  159.  
  160.       if ($hx &lt; $h) {
  161.        $h = (100 / ($width / $w)) * .01;
  162.        $h = @round ($height * $h);
  163.       } else {
  164.        $w = (100 / ($height / $h)) * .01;
  165.        $w = @round ($width * $w);
  166.       }
  167.    } else { // crop image
  168.      // find the right scale to use
  169.      $_scale=min((float)($width/$w),(float)($height/$h));
  170.  
  171.      // coords to crop
  172.      $cropX=(float)($width-($_scale*$w));
  173.      $cropY=(float)($height-($_scale*$h));
  174.  
  175.      // cropped image size
  176.      $cropW=(float)($width-$cropX);
  177.      $cropH=(float)($height-$cropY);
  178.  
  179.      $crop=ImageCreateTrueColor($cropW,$cropH);
  180.      ImageCopy($crop,$OldImage,0,0,(int)($cropX/2),(int)($cropY/2),$cropW,$cropH); // crop the middle part of the image to fit the thumbnail's proportions
  181.    }
  182.   }
  183.  
  184.   // do the thumbnail
  185.   $NewThumb=ImageCreateTrueColor($w,$h);
  186.   if (isset($crop)) { // been cropped
  187.    ImageCopyResampled($NewThumb,$crop,0,0,0,0,$w,$h,$cropW,$cropH);
  188.    ImageDestroy($crop);
  189.   } else { // ratio match, regular resize
  190.    ImageCopyResampled($NewThumb,$OldImage,0,0,0,0,$w,$h,$width,$height);
  191.   }
  192.   _ckdir($saveas);
  193.   ImageJpeg($NewThumb,$saveas,$quality);
  194.   ImageDestroy($NewThumb);
  195.   ImageDestroy($OldImage);
  196.  } elseif (($force == "0") &amp;&amp; (!function_exists('exif_imagetype'))) {
  197.   $r=img_force($src,$quality,$w,$h,$saveas,$nocrop,$e);
  198.  }
  199.  return $r;
  200. }
Leave a Comment :, , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!