LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with rsync command (or script) multiple file backups (https://www.linuxquestions.org/questions/linux-software-2/help-with-rsync-command-or-script-multiple-file-backups-597170/)

luis14 11-04-2007 11:06 PM

Help with rsync command (or script) multiple file backups
 
These are the commands I use to back up specific files from my home directory to another machine. I have to enter them one at a time and am prompted for my password after each command.

I remember reading a forum post somewhere describing how to link commands like these (something to do with &&).
Code:

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/pictures /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/video /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/music /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/documents /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/programs /home/daniel

Anybody know how to convert this to one command or script so I can enter my password once and walk away while the backups are performed?

0.o 11-05-2007 07:18 AM

Quote:

Originally Posted by luis14 (Post 2948382)
These are the commands I use to back up specific files from my home directory to another machine. I have to enter them one at a time and am prompted for my password after each command.

I remember reading a forum post somewhere describing how to link commands like these (something to do with &&).
Code:

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/pictures /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/video /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/music /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/documents /home/daniel

rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/programs /home/daniel

Anybody know how to convert this to one command or script so I can enter my password once and walk away while the backups are performed?

You can string the commands together via either && or ; or even use ||. && tells it to execute the first one and the second only if the first executes successfully. ; will execute the next command no matter what. || will execute one or the other. If the first command executes fine, the second will be ignored. However, if the first fails, the second will be executed.

You can also avoid the password prompts via the use of ssh keys. Google for ssh-keygen (or read the man page).

colucix 11-05-2007 07:32 AM

You may also consider to use a for loop, as in
Code:

#!/bin/bash
dirlist="pictures video music documents programs"

for dir in $dirlist
do
  rsync -e ssh -avz --delete-after daniel@10.0.0.7:/home/daniel/$dir /home/daniel
  test $? -eq 0 || break
done

In this way you have just to edit the list of directories to backup. The test part simply stats "if something goes wrong with the previous command break the cycle", so you can stop the backup process and investigate about the failure.

luis14 11-08-2007 10:07 PM

Works beautifully! Thanks for sharing your knowledge.


All times are GMT -5. The time now is 09:31 AM.