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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix create backup on a remote computer script

Status
Not open for further replies.

Jorjo

Programmer
May 22, 2012
2
BG
Hello,

I have to make a Unix script that creates an archive of a target directory on a remote computer, it is like kind of to create a "backup" on a remote computer (in the same LAN).

The script must be started on the local computer, take the desired directory as an argument and create a number of archives, each no bigger in size than 2GB. Each file must be placed only in one archive file, as opposed to half of it in archive1 and the other half in archive2. The archives must be transfered along the net by using "ssh" with generated keys (to avoid password request every time). The directory on the remote computer can either be given as an argument or be built-in.
The script must also create a simple database (some form of txt file), listing which archive contains which files.

Thank you!
 
This sounds like a school project. Whenever I see the word "must" used that many times, it's usually a school assignment.

What have you got so far?

 
Why reinvent the wheel ...checkout rsync.

Unless as suggested this is a training project then yes its will be a great project and quite simple to do with basic (default) Unix tools, but you have to take the lead on starting your scripting.

What I find is the best way to start is with a piece of paper and pen and make a high-level flow chart of all the processes you need to do then just look at the best tools (commands) to tie them all in together.

As requested above, show us what you have already and where your stuck and we will try and help.

L.
 
Well, actually it only kinda is a school project. :)

I attended a UNIX course some years back and that was one of our assignments, I did it then, but have since forgotten how to write scripts AT ALL (since I don't use them) and that's why I want to re-do it again :) . I know that I can use the tar or gzip command to archive the folder but I honestly have no idea how to start the entire thing. :)
 
OK so one way is to write a script that calls a set of functions (these functions will have your commands scripted and you call whatever function you want or all one by one)....

OK so Monaco GP started so I have to go .... though this is not tested I hope it will jog your memory and get you started again.

(setting up ssh keys to avoid password prompts is for another subject but you can begin with getting prompted for password and set up keys later if you get that far)

For instance .....

Code:
#!/bin/ksh

Generic_user="fredBloggs"
resultLog="/export/home/localFredBloggs/backup/"

function ONE{

# -- connect to a list of remote systems and run commands on those machines ---
# -- returning the result to the local calling machine "just to prove a point" ---
# -- Assumes having set up ssh shared keys first to avoid password prompts ---
#

for HOST in hostname1 hostname2 hostname3 hostname4; do ssh $GenericUser@$HOST "echo $HOST; echo '-----'; date; echo 'next machine -----'"; done > $resultLog/ONE_`date +%d%m%y`
}

function TWO{

# -- backup a set of remote host files with tar ---  

for HOST in hostname1 hostname2 hostname3 hostname4; do ssh $GenericUser@$HOST "cd /export/home/; tar cvf /var/tmp/$GenericUser_`date +%d%m%y`.tar; gzip -9 /var/tmp/$GenericUser_`date +%d%m%y`.tar"; done >> $resultLog/TWO_`date +%d%m%y`

for HOST in hostname1 hostname2 hostname3 hostname4; do scp $GenericUser@$HOST:/var/tmp/$GenericUser_`date +%d%m%y`.tar.gz /export/home/localBackup/; done
>> $resultLog/TWO_`date +%d%m%y`
}

function THREE{

# -- Clean up remote host ---

for HOST in hostname1 hostname2 hostname3 hostname4; do ssh $GenericUser@$HOST "rm /var/tmp/$GenericUser_`date +%d%m%y`.tar.gz ; done >> $resultLog/THREE_`date +%d%m%y`

}

# -- Call the functions here one after each other ---
ONE
TWO
THREE

# -- End Script ---


Laurie.
 
Opps missed the source in here ....

Code:
for HOST in hostname1 hostname2 hostname3 hostname4; do ssh $GenericUser@$HOST "cd /export/home/; tar cvf /var/tmp/$GenericUser_`date +%d%m%y`.tar; gzip -9 /var/tmp/$GenericUser_`date +%d%m%y`.tar"; done >> $resultLog/TWO_`date +%d%m%y`

Code:
for HOST in hostname1 hostname2 hostname3 hostname4; do ssh $GenericUser@$HOST "cd /export/home/; tar cvf /var/tmp/$GenericUser_`date +%d%m%y`.tar [COLOR=red][b]*[/b][/color] ; gzip -9 /var/tmp/$GenericUser_`date +%d%m%y`.tar "; done >> $resultLog/TWO_`date +%d%m%y`

L.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top