Cpfiles Backup Script

From Superk

Jump to: navigation, search

This backup script reads a tab-delimited text file as input and uses rsync to copy from one location to another.

#!/usr/bin/php -q
<?php
if($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>
# Display help info
This command copies files from one place to another based on the contents of a from/to file.

Usage:
<?php echo $argv[0]; ?> [input_file]

[input_file]	- Input file (list) including from/to definitions (tab delimited).
<?php
} else {
// Display running process...

echo "Processing List File: ".$argv[1]."\n";
echo "-------------------------------------\n";

// Read in list
$input = file($argv[1]);
if(!$input) {
	echo "Error: could not open input file (".$argv[1].") for processing.\n";
	exit;
} else {
	 // Check the input file is not empty
	$count = count($input);
	if($count == 0) {
		echo "Error: input file (".$argv[1].") is empty.\n";
		exit;
	}
}

// Do the work
for($i=0;$i<count($input);$i++) {
	$file = explode("\t", $input[$i]);
	echo "Copying files from ".$file[0]." to ".$file[1]." ... ";
	system('rsync -urpvt '.$file[0].'* '.$file[1]);
	system('chown -R '.$file[2].'.'.str_replace("\n","",$file[3]).' '.$file[1]);
	echo " Done. \n";
}

echo "-------------------------------------\n";
echo "Processing Complete!\n";
}
?>

Input file takes on the following format:

<source>[TAB]<destination>[TAB]<user>[TAB]<group>

Command syntax:

$ /path/to/cpfiles input.txt
Personal tools