Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env php
- <?php
- // CODER AMIR
- $time_start = microtime(true);
- logmessages("started back up at :".date('Y-m-d h:i:s a'));
- error_reporting(E_ALL);
- ini_set('display_errors',1);
- //$mail= array("sendmail"=>true,
- // "sendto"=>array('didarema@gmail.com'));
- $dbuser='root';
- $dbpass='passw0rd';
- $dbname='--all-databases';
- $sambaip='//172.16.8.50/137/';
- $ceredntial = "username=back137,password=137back@1394";
- $sambamountpath='/mnt';
- $backupdirectory=$sambamountpath.'/backup/';
- $backupdirectoryforsounds= $sambamountpath.'/backup/sounds/';
- $backupdirectoryfordbs=$sambamountpath.'/backup/db/';
- $directorytobackup= array("/var/spool/asterisk/monitor","/var/spool/asterisk/voicemail","/var/spool/asterisk/record");
- //end configuration
- $date = date('Y-m-d');
- $timesig = str_ireplace(array(" ",":") , "_" , date('h:i:s Y-m-d'));
- $findsamba=exec('df -T -x tmpfs -x devtmpfs -P -B 1K',$mountedpath) ;
- array_shift($mountedpath);
- foreach($mountedpath as $disks)
- {
- $split = preg_split('/\s+/', $disks);
- $Stats[] = array(
- 'disk' => $split[0],
- 'mount' => $split[6],
- 'type' => $split[1],
- //'mb_total' => $split[2],
- //'mb_used' => $split[3],
- 'mb_free' => $split[4],
- //'percent' => $split[5],
- );
- }
- foreach($Stats as $path)
- {
- if(stristr($path['disk'], $sambaip))
- {
- //found the partiton for samba
- $mounted=true;
- //$mountpath=$path['mount'];
- }
- }
- if(!$mounted)
- {
- //try mount
- logmessages($sambaip . ": Not mounted trying to mount in {$sambamountpath} => ".date('Y-m-d h:i:s a'));
- exec("mount -t smbfs -o {$ceredntial} {$sambaip} {$sambamountpath}",$mountresult);
- if(empty($mountresult))
- {
- logmessages("mounted {$sambaip} in {$sambamountpath} =>" .date('Y-m-d h:i:s a'));
- $mountpath='/mnt';
- }
- else
- {
- logmessages($sambaip . ": Not mounted and could Not be mounted =>".date('Y-m-d h:i:s a'));
- }
- }
- else
- {
- logmessages("Samba is already mounted =>" .date('Y-m-d h:i:s a'));
- }
- //create backup directory in first run if it does not exist
- checkAndCreateDir($backupdirectory);
- foreach($directorytobackup as $dir )
- {
- $lastdir= substr($dir, strrpos($dir, '/')+1);
- checkAndCreateDir($lastdir);
- }
- checkAndCreateDir($backupdirectoryfordbs);
- //back up all paths defined in $directorytobackup variable
- foreach($directorytobackup as $key=>$value)
- {
- //$finddir= $backupdirectory.substr($dir, strrpos($dir, '/')+1);
- $move=exec("rsync -avz {$value} {$backupdirectory} --log-file=/root/rsynclog ");
- logmessages("copied ". $value . " directory contetnt : ".date('Y-m-d h:i:s a'));
- }
- //back up defined database here
- $dbnamebackup= str_replace('--', "",$dbname );
- $dbnamebackup=$dbnamebackup.'-'.$timesig;
- logmessages("Start database dump :" .date('Y-m-d h:i:s a'));
- $dobackup=exec('mysqldump -u '.$dbuser.' -p'.$dbpass.' '.$dbname . ' > ' .$backupdirectoryfordbs.$dbnamebackup.'.sql');
- logmessages("dumped mysql database with name :".$dbname." msg:".$dobackup." : ".date('Y-m-d h:i:s a'));
- //change path to tmp direcotry so when creating tar file we do not get abs path
- $sqldumps = glob($backupdirectoryfordbs.'*.sql');
- foreach($sqldumps as $key=>$value)
- {
- if ($value != '.' && $value != '..')
- {
- $files[$value] = filemtime($value);
- }
- }
- array_multisort($files,SORT_DESC,$files); //Added Descending sort Here
- logmessages("checking back up files timestamp :".date('Y-m-d h:i:s a'));
- if(count($files)>=10)
- {
- end($files);
- $filename= key($files);
- unlink($filename);
- logmessages("removed back up files".$filename." as policy is to keep ten last backup:".date('Y-m-d h:i:s a'));
- }
- //
- /*if($mail['sendmail'])
- {
- if(count($mail['sendto']))
- {
- foreach($mail['sendto'] as $key=>$value)
- {
- $subject = "Support Desk server backup";
- $message = "Daily backup of support desk is server done";
- $from = "its3@ut.ac.ir";
- $headers = "From:" . $from;
- if(mail($value,$subject,$message,$headers))
- {
- logmessages('Mail sent to email address');
- }
- }
- }
- }
- */
- $time_end = microtime(true);
- $time = $time_end - $time_start;
- logmessages("back up done successfully finished at : ".date('Y-m-d h:i:s a'). " back up took to complete : ".round($time,2));
- function logmessages($message)
- {
- $filename="backup.log";
- $handle=fopen("/var/log/".$filename,'a');
- fwrite($handle,$message.PHP_EOL);
- fclose($handle);
- }
- function checkAndCreateDir($dir)
- {
- if(!file_exists($dir) && !is_dir($dir))
- {
- logmessages("created backup directory {$dir} : ".date('Y-m-d h:i:s a'));
- mkdir($dir);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement