You are here: Home Tech Notes Linux PHP CLI: Rsync Script
PHP CLI: Rsync Script PDF Print E-mail
User Rating: / 0
PoorBest 
Tech Notes - Linux
Written by Rick   
Wednesday, 23 December 2009 17:01

Here's a handy script to run rsync from the command-line that prompts for source and destination paths;

#! /usr/bin/php

<?php
## Control CLI parameters
if (($argv[1]=="") OR ($argv[2]=="")) {
echo "Options not specified.\r\n";
echo "Program Usage: ";
echo $argv[0]." <i.e.: /source/path> <i.e.: /destination/path>\r\n\r\n";
exit;
}else{
$srcpath=$argv[1];
$dstpath=$argv[2];
}

$starttime=date('G:i:s', strtotime('now'));

echo "Starting rsync...\r\n";
$retval=system("sudo rsync -avrl --delete ".$srcpath." ".$dstpath, $retval);
if (!$retval==false) {
echo "Backup from ".$srcpath." to ".$dstpath." is complete!\r\n\r\n";
}else{
echo "Backup from ".$srcpath." to ".$dstpath." FAILED!\r\n\r\n";
}

$endtime=date('G:i:s',strtotime('now'));


echo "\r\nStart time - ".$starttime."\r\n";
echo "End time - ".$endtime."\r\n\r\n";
echo "Script complete!\r\n";

?>

Note:

  • The sudo command in the system call allows the script to be used to sync root user files. This can be removed if that behavior is not desired.
  • The script also utilizes the --delete feature of rsync to remove files in the destination that do not exist in the source. Evaluate that behavior for your application as well.
Comments (0)
Write comment
Your Contact Details:
Gravatar enabled
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):S
:!::?::idea::arrow:
Security
Please input the anti-spam code that you can read in the image.