Backupscript

Backup, Restore und Transfer von Daten
User avatar
lumio
Posts: 46
Joined: 2008-05-24 16:48
 

Backupscript

Post by lumio »

Hi!
Ich hab schnell ein Backupscript gebastelt... und da is halt immer die Sache, dass es ein paar kleine Maken haben könnte.

Mein Code sieht so aus:

Code: Select all

<?php
/*
	(C) Benjamin Falk
	You are allowed to use it
	and modify it.
	
	Backupscript under GPLv3
	
*/
	define("BACKUP_DIR", "/var/backups/");			//The place where the backup gets to
	define("BACKUP_USER", "lumio");					//Backup-owner
	define("BACKUP_GROUP", "lumio");				//Backup-owner
	
	define("MAIN_PATH", "/var/www;/home/vmail");	//With directories should get backed up (split with ; )
	define("IGNORE_FILES", "backup.php");			//With files should be ignored (split with ; )
	define("TMP_DIR", "/tmp/");						//Temporary directory (should be /tmp/)
	define("DOWN_DIR", "/var/www/backups/");		//Directory for downloading backups.
	
	class BACKUP {
		var $files=0;
		var $last=0;
		var $copyArray=array();
		var $verbose=false;
		var $remove=false;
		
		function BACKUP() {
			/* Split up pathes and files to ignore */
			$pathes		= explode(";", MAIN_PATH);
			$ignores	= explode(";", IGNORE_FILES);
			
			/* Get arguments */
			$vals = str_split($_SERVER['argv'][1]);
			foreach($vals as $v) {
				if ($v == 'v')				//verbose mode
					$this->verbose=true;
				elseif ($v == 'r')			//remove download-backup-directory before backing up
					$this->remove = true;
			}
			
			/* Check backup-directory */
			$backup_dir = BACKUP_DIR;
			if (substr($backup_dir,-1) != '/') $backup_dir .= '/'; 
			if (!file_exists($backup_dir)) die($backup_dir." does not exist!n");
			
			/* Check download-backup-directory */
			$down_dir = DOWN_DIR;
			if (substr($down_dir,-1) != '/') $down_dir .= '/';
			$not_allowed = array("/var/www/", "/etc/", "/home/", "/tmp/");
			if ($this->remove)  {
				if (in_array($down_dir, $not_allowed))
					echo "Cancel removing download-directoryn";
				elseif (file_exists($down_dir)) {
					echo "Removing download-backup-directory... ";
					exec("rm -r $down_dir");
					echo "done";
				}
				
				echo "Removing all temporary directories... ";
				exec("rm -r /tmp/*");
				echo "donen";
			}
			
			/* Define temp-directory */
			$tmp = TMP_DIR;
			if (substr($tmp,-1) != '/') $tmp .= '/';
			$i = 0;
			
			$token = '';
			do {
				microtime()*300000;
				$token = md5(rand());
				microtime()*300000;
				$p1 = rand(0,20);
				$token = substr($token,$p1,8);
			} while (file_exists($tmp.$token));
			$tmp = $tmp.$token.'/';
			
			/* Start backing up */
			foreach($pathes as $p) {
				$this->files = 0;
				$this->last = 0;
				
				if (substr($p,-1) != '/') $p .= '/';
				echo "Backing up $p...n";
				
				if (file_exists($tmp)) exec("rm -r $tmp");
				if (!file_exists($tmp)) mkdir($tmp);
				
				$this->copyPath($p, $tmp, $ignores);
				
				if (!$this->verbose) echo ' '.strval($this->files)." file(s) copiedn";
				
				$this->compressFiles($tmp, $backup_dir, $p);
			}
			
			/* Create download-backup-directory */
			if (!file_exists($down_dir)) {
				mkdir($down_dir);
				exec("chown -R ".BACKUP_USER.":".BACKUP_GROUP." $down_dir");
			}
			
			/* Copy backup-files into download-backup-directory */
			foreach($this->copyArray as $f)
				exec("cp $f $down_dir");
			
			//Remove temprary directory
			exec("rm -r $tmp");
			
			echo "done.nn";
			
			$str = "The following files were copied into $backup_dir and $down_dir";
			echo $str."n";
			echo str_repeat('-', strlen($str))."n";
			foreach($this->copyArray as $f)
				echo " * ".basename($f)."n";
			
			echo "n";
			echo "Ended Backupscript (".date("Y-m-d H:i:s e P").")...n";
			echo "n";
		}
		
		/*
			copyPath	- copies all files and directory
			$p			- contains the item to be copied
			$base		- destination
			$ignores	- files to be ignored
		*/
		function copyPath($p, $base, $ignores=array()) {
			$d = dir($p);
			while(($item = $d->read()) !== false) {
				if ($item == "." || $item == "..") continue;
				if (in_array($item, $ignores)) continue;
				
				$fullName = $d->path.$item;
				if (is_dir($fullName)) {
					if (!file_exists($base.$item.'/')) mkdir($base.$item.'/');
					$this->copyPath($fullName.'/', $base.$item.'/');
				}else {
					copy($fullName, $base.$item);
					$this->files++;
					
					if ($this->verbose) {
						if ($this->last > 0) {
							echo chr(27).'[1A';
						}
						
						$str = ' '.strval($this->files)." file(s) copiedn";
						$this->last = strlen($str);
						echo $str;
					}
				}
			}
		}
		
		/*
			compressFiles	- Generates .tar and .gz out of a directory
			$workDir		- Working directory (Source)
			$backupDir		- Backup directory (Destination)
			$dir			- Source directory
		*/
		function compressFiles($workDir, $backupDir, $dir) {
			chdir($workDir);
			$tar_file = $backupDir.basename($dir).".tar";
			$gz_file = $tar_file.'.gz';
			
			echo "Compressing files... ";
			exec("tar -cf $tar_file ./");
			if (file_exists($gz_file)) exec("rm $gz_file");
			exec("gzip $tar_file");
			echo "donen";
			
			$this->copyArray[] = $gz_file;
		}
	}
	
	echo "Starting Backupscript (".date("Y-m-d H:i:s e P").")...n";
	$c = new BACKUP();
?>
Vielleicht sieht sich ja jemand das script an, und kann seine Kritik kund tun.

//edit: Ich sollte noch erklären wies verwendet wird:

Code: Select all

php -f backup.php vr
v für verbose (Zeigt den Fortschritt an)
r für remove (Löscht ggf. das download-backup-verzeichnis)
User avatar
lesswire
Posts: 33
Joined: 2005-04-28 03:45
 

Re: Backupscript

Post by lesswire »

Sorry, aber wird damit das Rad nicht nochmal neu erfunden? Ich mache alle Backups mit rsync und wüßte eigentlich nicht, welches Feature mir fehlt. Was macht das Besondere an dem Script aus?

vg, LW.
User avatar
lumio
Posts: 46
Joined: 2008-05-24 16:48
 

Re: Backupscript

Post by lumio »

Nichts ... und ich bin grad draufgekommen, dass es ein eigenes Forum für das gibt.