Advertisement
Guest User

Untitled

a guest
May 24th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3. // CODER AMIR
  4. $time_start = microtime(true);
  5. logmessages("started back up at :".date('Y-m-d h:i:s a'));
  6. error_reporting(E_ALL);
  7. ini_set('display_errors',1);
  8.  
  9. //$mail= array("sendmail"=>true,
  10. // "sendto"=>array('didarema@gmail.com'));
  11. $dbuser='root';
  12. $dbpass='passw0rd';
  13. $dbname='--all-databases';
  14. $sambaip='//172.16.8.50/137/';
  15. $ceredntial = "username=back137,password=137back@1394";
  16. $sambamountpath='/mnt';
  17. $backupdirectory=$sambamountpath.'/backup/';
  18. $backupdirectoryforsounds= $sambamountpath.'/backup/sounds/';
  19. $backupdirectoryfordbs=$sambamountpath.'/backup/db/';
  20. $directorytobackup= array("/var/spool/asterisk/monitor","/var/spool/asterisk/voicemail","/var/spool/asterisk/record");
  21.  
  22. //end configuration
  23. $date = date('Y-m-d');
  24. $timesig = str_ireplace(array(" ",":") , "_" , date('h:i:s Y-m-d'));
  25.  
  26. $findsamba=exec('df -T -x tmpfs -x devtmpfs -P -B 1K',$mountedpath) ;
  27. array_shift($mountedpath);
  28. foreach($mountedpath as $disks)
  29. {
  30. $split = preg_split('/\s+/', $disks);
  31. $Stats[] = array(
  32. 'disk' => $split[0],
  33. 'mount' => $split[6],
  34. 'type' => $split[1],
  35. //'mb_total' => $split[2],
  36. //'mb_used' => $split[3],
  37. 'mb_free' => $split[4],
  38. //'percent' => $split[5],
  39. );
  40. }
  41.  
  42.  
  43.  
  44. foreach($Stats as $path)
  45. {
  46. if(stristr($path['disk'], $sambaip))
  47. {
  48. //found the partiton for samba
  49. $mounted=true;
  50. //$mountpath=$path['mount'];
  51. }
  52. }
  53. if(!$mounted)
  54. {
  55. //try mount
  56. logmessages($sambaip . ": Not mounted trying to mount in {$sambamountpath} => ".date('Y-m-d h:i:s a'));
  57. exec("mount -t smbfs -o {$ceredntial} {$sambaip} {$sambamountpath}",$mountresult);
  58. if(empty($mountresult))
  59. {
  60. logmessages("mounted {$sambaip} in {$sambamountpath} =>" .date('Y-m-d h:i:s a'));
  61. $mountpath='/mnt';
  62. }
  63. else
  64. {
  65. logmessages($sambaip . ": Not mounted and could Not be mounted =>".date('Y-m-d h:i:s a'));
  66. }
  67.  
  68. }
  69. else
  70. {
  71. logmessages("Samba is already mounted =>" .date('Y-m-d h:i:s a'));
  72. }
  73.  
  74.  
  75.  
  76. //create backup directory in first run if it does not exist
  77. checkAndCreateDir($backupdirectory);
  78. foreach($directorytobackup as $dir )
  79. {
  80. $lastdir= substr($dir, strrpos($dir, '/')+1);
  81. checkAndCreateDir($lastdir);
  82. }
  83.  
  84.  
  85. checkAndCreateDir($backupdirectoryfordbs);
  86.  
  87. //back up all paths defined in $directorytobackup variable
  88. foreach($directorytobackup as $key=>$value)
  89. {
  90. //$finddir= $backupdirectory.substr($dir, strrpos($dir, '/')+1);
  91. $move=exec("rsync -avz {$value} {$backupdirectory} --log-file=/root/rsynclog ");
  92. logmessages("copied ". $value . " directory contetnt : ".date('Y-m-d h:i:s a'));
  93. }
  94.  
  95. //back up defined database here
  96. $dbnamebackup= str_replace('--', "",$dbname );
  97. $dbnamebackup=$dbnamebackup.'-'.$timesig;
  98. logmessages("Start database dump :" .date('Y-m-d h:i:s a'));
  99. $dobackup=exec('mysqldump -u '.$dbuser.' -p'.$dbpass.' '.$dbname . ' > ' .$backupdirectoryfordbs.$dbnamebackup.'.sql');
  100. logmessages("dumped mysql database with name :".$dbname." msg:".$dobackup." : ".date('Y-m-d h:i:s a'));
  101. //change path to tmp direcotry so when creating tar file we do not get abs path
  102.  
  103. $sqldumps = glob($backupdirectoryfordbs.'*.sql');
  104. foreach($sqldumps as $key=>$value)
  105. {
  106. if ($value != '.' && $value != '..')
  107. {
  108. $files[$value] = filemtime($value);
  109. }
  110. }
  111.  
  112. array_multisort($files,SORT_DESC,$files); //Added Descending sort Here
  113. logmessages("checking back up files timestamp :".date('Y-m-d h:i:s a'));
  114. if(count($files)>=10)
  115. {
  116. end($files);
  117. $filename= key($files);
  118. unlink($filename);
  119. logmessages("removed back up files".$filename." as policy is to keep ten last backup:".date('Y-m-d h:i:s a'));
  120. }
  121.  
  122.  
  123. //
  124. /*if($mail['sendmail'])
  125. {
  126. if(count($mail['sendto']))
  127. {
  128. foreach($mail['sendto'] as $key=>$value)
  129. {
  130. $subject = "Support Desk server backup";
  131. $message = "Daily backup of support desk is server done";
  132. $from = "its3@ut.ac.ir";
  133. $headers = "From:" . $from;
  134.  
  135. if(mail($value,$subject,$message,$headers))
  136. {
  137. logmessages('Mail sent to email address');
  138. }
  139. }
  140. }
  141. }
  142. */
  143. $time_end = microtime(true);
  144. $time = $time_end - $time_start;
  145. logmessages("back up done successfully finished at : ".date('Y-m-d h:i:s a'). " back up took to complete : ".round($time,2));
  146.  
  147. function logmessages($message)
  148. {
  149. $filename="backup.log";
  150. $handle=fopen("/var/log/".$filename,'a');
  151. fwrite($handle,$message.PHP_EOL);
  152. fclose($handle);
  153. }
  154.  
  155. function checkAndCreateDir($dir)
  156. {
  157. if(!file_exists($dir) && !is_dir($dir))
  158. {
  159. logmessages("created backup directory {$dir} : ".date('Y-m-d h:i:s a'));
  160. mkdir($dir);
  161. }
  162. }
  163.  
  164.  
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement