Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script to copy files based on available disk space

Status
Not open for further replies.

namityadav

Programmer
Aug 2, 2005
5
US
I have to write a perl script that will be executed periodically to copy files from a remote server, and will have to do the following:

1. Check the disk space on local server, find out how much free disk space is available for the copying (We can assume that the copying can take 90% of the total free disk space on local server)
2. Use scp to copy the files (Keys are already set) from the remote machine

What we have to note is: Due to performance issues of the ssh tunnels, it'll be better to copy the files over in one go. One possible problem, in that case, is that if there are too many files on remote machine, then we should figure out how many files we can copy ( and what files are those? ).

Earlier I was using a shell script to copy files, but that wasn't taking care of the disk space. So I decided to see if perl can help me. Unfortunately I don't know much about perl. Any suggestions?
 
First I guess you need to create a set of building blocks. Tools that will allow you to get the info you need. Then you have to join them up.

As I understand it, ssh can run a command on a remote machine so therefore anything you can run locally to find disk space you can probably run remotely using ssh and capture the output with perl.

You don't say what OS you're running but I assume it's a unix or similar. In my linux environment I use "df" to find out how much space is left on my disks. You could run this in perl using backticks "`" and assign the result to a scalar for processing to allow you to collect the numbers and convert them to bytes (for example).

As for counting up how much space you need, perl has a module called "File::Find" that will allow you to traverse the filesystem tree and you can use "stat" to collect the details of each file in turn (particularly size). You could add up all the sizes and hey presto, there's your total.

Hope that helps.



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top