RSYNC is really simple, you can do an RSYNC by a single command line on your console. so, probably now your thinking.., what's the difference of my script? the difference is that I was able to create a script that has configuration files for you to re-use it every time you have different directories and servers to SYNC on.
First, you have to know the basic command line to do RSYNC. please see below command line.
rsync -crtz --bwlimit=56 --stats -e "ssh -l [USERNAME] -p [PORT]" [SOURCE DIRECTORY] [DESTINATION IP]:[DESTINATION DIRECTORY]
[USERNAME] is your account username to the destination server. your username should be set in passwordless to make it run in background.
[PORT] is the port that you will be using to connect to destination server.
[SOURCE DIRECTORY] is the source directory of the files you want to SYNC on.
[DESTINATION IP] is the server ip of the destination server.
[DESTINATION DIRECTORY] is the destination directory of the files you want to SYNC on.
Once you know how the RSYNC works in background, next is to know how we want our configuration to work. we should have a script that reads configuration in a flat file.
In creating configuration file, you should consider the comment syntax. this is to easily comment the lines you wanted to skip on or disregard in the configuration file.
Usually in linux, commented lines are preceded by pound sign "#" and that should be one of your condition to disregard. please see below for the sample reading of file then check for pound sign "#" using REGEX, then disregard.
open FHFILE, "< $strConf";
@arrData = <FHFILE>;
close FHFILE;
foreach $strData (@arrData)
{
chomp $strData;
if ($strData !~ /^\s*\#/)
{
print "okay here";
}
}
The simple REGEX above checks if you have pound sign "#" on each lines and print okay if you don't.
The last thing you should know is how to pass the configuration file in command line. SYNTAX will be <YOUR SCRIPT> <space> <YOUR CONFIG FILE>. If you are new to LINUX, all parameters you passed on after your PERL SCRIPT will be catched by the ARRAY variable $ARGV. print that on your script with zero "0" as index will give you the first parameter you passed on, then succeeding index will the next parameter and so on and so forth.
Hope you were able to follow. Now we're ready to do the actual code.
Please see the recommended configuration FORMAT below that works on my RSYNC script. You will notice that I have a header which is setup to be the [DESTINATION IP] and [DESTINATION PORT]. This is for me to have it organize in a per SERVER configuration, then succeeding lines will be the actual SOURCE and DESTINATION directories.
- conf.txt as configuration file
[DESTINATION IP]:[DESTINATION PORT]
[SOURCE DIRECTORY 1]:[DESTINATION DIRECTORY 1]
[SOURCE DIRECTORY 2]:[DESTINATION DIRECTORY 2]
[SOURCE DIRECTORY n..]:[DESTINATION DIRECTORY n..]
- rsync.pl as your perl script
#!/usr/bin/perl
### reading conf files
$strConf = $ARGV[0];
if (-e $strConf)
{
&do_rsync($strConf);
}
else
{
print "conf: $strConf does not exist!!\n";
}
###
sub do_rsync
{
my ($strConf) = @_;
my ($ip, $port, $cmd, $strData, $strIncomingDir, $strOutGoingDir, $strReturn);
my @arrData;
open FHFILE, "< $strConf";
@arrData = <FHFILE>;
close FHFILE;
$ip_port = shift @arrData;
chomp $ip_port;
# reading the ip and port as header
while ($ip_port =~ /^\s*\#/)
{
$ip_port = shift @arrData;
chomp $ip_port;
}
# end
($ip, $port) = split /\:/, $ip_port;
if ($ip =~ /[^0-9|.]/)
{
print "ip not valid..\n\n";
}
elsif ($port =~ /[^0-9]/)
{
print "port not valid..\n\n";
}
else
{
# reading each line after the header
foreach $strData (@arrData)
{
chomp $strData;
if ($strData !~ /^\s*\#/)
{
($strIncomingDir, $strOutGoingDir) = split /\:/, $strData;
$cmd = "rsync -crtz --bwlimit=56 --stats -e \"ssh -l contents -p $port\" $strIncomingDir $ip:$strOutGoingDir";
$strReturn = `$cmd`;
}
}
# end
}
}
1;
To execute, please see command line below. you can also put this in CRON, provided that the USERNAME is set to be PASSWORDLESS.
<path to the script>/rsync.pl <path to the config file>/conf.txt
Hope you like it!! Please leave a comment if you find this helpful. Thanks!!
You should know your purpose in life, make a difference, and believe in yourself. Always think positive, take the risk and move forward. Do that, then before you know it, you're already the man you always wanted. Cheers!!
Subscribe to:
Post Comments (Atom)
Leadership 101
- Leadership demands sacrifices for the near-term to receive lasting benefits. the longer we wait to make sacrifices, the harder they become. Successful people make important decisions early in their life, then manage those decisions the rest of their lives.
- Growth does not happen by chance. If you want to be sure to grow, you need a plan something strategic, specific, and scheduled. it's a discipline that would need incredible determination from us.
- Success comes by going the extra mile, working the extra hours, and investing the extra time. The same is true for us. If we want to get to excel in any segment of life, a little extra effort can help. Our efforts can go a long way if we only work a little smarter, listen a little better, push a little harder, and persevere a little longer.
- Making a difference in your work is not about productivity; it's about people. When you focus on others and connect with them, you can work together to accomplish great things.
- Envision a goal you'd like to reach. Make it big enough to scare you a little. Now write down a plan for moving toward it. Create mini-goals within the big goal, to set yourself up for continual progress. And include some risks, too. Set yourself up for success.
- Leaders build margins, not image. A leader may be forced to take unpopular stands for the good of the company. Popularity isn't bad, but decisions made solely on the basis of popular opinion can be devastating. So take courage and make the right though sometimes painful choices.
No comments:
Post a Comment