Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- print "Parsing command line arguments: " . PHP_EOL;
- $commando = getArgument("-exec=");//'echo "' . mt_rand() . '"';
- $timestamp = time();
- $path = getArgument("-outdir=");
- $prefix = getArgument("-prefix=");
- $limit = getArgument("-limit=");
- $exec = $commando . " > " . $path . DIRECTORY_SEPARATOR . $prefix . $timestamp;
- if($path == NULL || $commando == NULL || $prefix == NULL || $limit == NULL) {
- exit(printUsage());
- }
- $files = glob($path . DIRECTORY_SEPARATOR . $prefix . "*");
- print PHP_EOL . PHP_EOL . "Found " . count($files) . " file(s) in " . $path . '. ';
- if(count($files) > $limit) {
- print "This is more than " . $limit . ", proceeding to delete files: " . PHP_EOL;
- $i = 0;
- foreach($files as $file) {
- if(count($files) - $i++ > $limit) {
- print "\tDeleted: " . $file . PHP_EOL;
- `rm $file`;
- }
- }
- } else {
- print "This is less than the limit. No need to archive old files." . PHP_EOL;
- }
- print PHP_EOL . PHP_EOL . "Proceeding to execute the following command: " . PHP_EOL. "\t`" . $exec . "`" . PHP_EOL;
- system($exec);
- print PHP_EOL . "All done, have nice day. " . PHP_EOL;
- function getArgument($prefix) {
- global $argv;
- foreach($argv as $arg) {
- if(substr($arg, 0, strlen($prefix)) == $prefix) {
- print "\tFound argument: " . $arg . PHP_EOL;
- return substr($arg, strlen($prefix));
- }
- }
- return NULL;
- }
- function printUsage() {
- print "You missed one or more arguments. Example usage: " . PHP_EOL;
- print "g_archiver.php -exec=echo\ 123 ";
- print "-outdir=/home/user/ ";
- print "-prefix=foobar. ";
- print "-limit=10" . PHP_EOL . PHP_EOL;
- }
- // /user/sbin/logwatch --detail High
- ?>
Advertisement
Add Comment
Please, Sign In to add comment