2

I had a previous question (deleted it) and I still don't understand why it is so hard to understand what I'm trying to ask, but I'll try once again and more clearly.

So I'm copying over several folders over from server A to server B, but the command for each copy process ('rsync') has to be executed from the command line, once for each folder.

So, to copy folder A to server X, I execute a rsync command and once it's done copying, I execute a new rsync command to copy over folder B to server X.

I've did a little research, that probably helps you answer my needs. But I'm looking for a way to write multiple commands at once, that fires one after another if the previous one is finished with his actions (thus finished copying).

Usually, multiple commands are separated on one line by ; semicolons or && ampersands. The difference between is that if the first/previous command fails with semicolons, it still continues. If the command produces an error and you had used ampersands, than it will stop.

Thus, I used semicolons in my process and let my computer going on overnight.. but it only executed the first command, even though I written several on one line separated with semicolons. So It should have executed all of them, one by one... So first: How do I correctly execute multiple commands and B, what is wrong with my command?

sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-01-02-233653/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ; 
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-01-09-152837/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ;
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-01-20-201229/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ;
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-02-21-130931/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ;
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-03-05-113353/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ;
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-03-19-162703/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ;
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-05-13-215016/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap ;
sudo rsync -v -v -r -h -t —progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-06-01-231321/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap

I separated them with breaks here for readability purposes. Normally it's one whole line

This usually goes by with a ; semicolon character

3
  • 2
    I guess your problem is the repeated sudo: When the first command finished, probably your sudo password timed out, and the second command won't be executed before you enter your password again. If that is the problem, the solution is to put the whole command chain in a single subshell executed by sudo, like sudo sh -c 'rsync ...; rsync ...; ...' Also note that you could simplify your command a lot by making use of for.
    – celtschk
    Sep 6, 2014 at 9:07
  • But I need sudo with every command, because of protected folders. If I write sudo only once, at start (like you write), will sudo rights ben maintained with every in the whole chain? Sep 6, 2014 at 9:19
  • 5
    That's the purpose of the sh -c. As far as sudo is concerned, there's only one command, sh, with the two arguments -c and 'rsync ...; rsync ...; ...'. This sh is run as root, and thus will also run all those rsync commands as root. Therefore the rsync commands will have full root permissions.
    – celtschk
    Sep 6, 2014 at 9:23

3 Answers 3

12

For repeatability I suggest to put the lines into a small script, but without the keyword sudo:

#!/bin/bash

rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-01-02-233653/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap  
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-01-09-152837/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap 
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-01-20-201229/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap 
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-02-21-130931/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap 
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-03-05-113353/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap 
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-03-19-162703/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap 
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-05-13-215016/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap 
rsync -v -v -r -h -t --progress /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/2014-06-01-231321/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap

To run the series of commands, type:

chmod +x myscript.sh
sudo ./myscript.sh

This way, sudo is not timing out during a long command. There is no need for a semicolon in a script file. By default, each line may fail and the next is executed (to change this, use set -o errexit).

I also noticed something fishy in your commands. for the option --progress you didn't use a regular hyphen, but some other, similar looking character. This could be another source of the errors you're seeing.

4
  • First of all. I do need sudo, since I'm copying over protected folders (TimeMachineBackups, to be specific) which with regular permissions wouldn't copy. The -progress, which actually should've been --progress, simply displays a percentage vision of how much of each file has been copied (in case of gigabyte big files). The actual problem, indeed, like celtschk writes in his comment, is the time-out of the password input. Thus, that should be avoided, if possible. And The command, whether it fails or not, should be executed, no matter what. So set -o errexit isn't needed. Does this work on Mac? Sep 6, 2014 at 9:38
  • My solution avoids this problem. The sudo rights are inherited by the shell and only dropped when the script is finished.
    – Sebastian
    Sep 6, 2014 at 9:39
  • Thanks big time! I will try and if this indeed fits, then you'll get your answer accepted! :) Sep 6, 2014 at 9:41
  • 1
    You can also verify this by launching the script and looking at the user which owns the shell or rsync in a tool like top or htop. It should be root, not your username. Also, check out the other answer for improving the rsync command (e.g. -a)
    – Sebastian
    Sep 6, 2014 at 9:43
8

Note the usage for rsync is rsync [OPTION]... SRC [SRC]... DEST which means this can be more simply written as

 rsync -r -t -h /src1dir /src2 /src3 /destdir

which isn't subject to sudo session timeouts. You didn't ask, but the option -a is far more comprehensive than -r -t.

1
  • And even more compactly as rsync -r -t -h /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/{2014-01-02-233653,2014-01-09-152837,2014-01-20-201229,...}/SSDaeffer /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap. Maybe even a * in place of the {} group for all I know :)
    – hobbs
    Sep 7, 2014 at 4:24
3

There are many ways of doing this. The approach you tried would have worked perfectly if each sudo did not require you to enter a password. Since it does, the commands would hang since the 2nd would ask for a password and you weren't there to give it. Therefore, you need a way of running everything with a single sudo call. Here are a few ways of doing this.

  1. Write the commands in a script and run the script with sudo as explained in @Sebastian's answer.

  2. Run a single rsync giving multiple source folders as suggested by @msw.

  3. Build the commands dynamically. It looks like you want to copy all SSDaeffer folders to /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap. This means you could simply write

    sudo rsync -vah /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/*/SSDaeffer \
                    /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap  
    

    That will copy all SSDaeffer folders that are sub-folders of a directory in /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS to /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap. This assumes that you want all the SSDaeffer folders there. If you only want some of them, you could list them in a file:

    $ cat file
    2014-01-02-233653
    2014-01-09-152837
    2014-01-20-201229
    2014-02-21-130931
    

    Then read the file to get the names you want:

    sudo while read dir; do 
     rsync -vah /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/$dir/SSDaeffer \
                /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap;
    done < file
    

    If your folder names can contain whitespace or other strange characters, use this instead:

    while IFS= read -r dir; do 
     rsync -vah /Volumes/My\ Book/Backups.backupdb/MbpScs-van-iSCS/$dir/SSDaeffer \
                    /Volumes/BackupMyBook/ViaMacbookMove/DitIsDeMap;
    done < file
    
  4. Start a root shell with sudo -i. This will let you login as the superuser and then you can run commands to your heart's content:

    $ sudo -i
    Password:
    # rsync -avh /from/dir1 /to/dir
    # rsync -avh /from/dir2 /to/dir
    # rsync -avh /from/dir3 /to/dir
    
1
  • Looks great and tells a lot. I'm at the moment successful with Sebastian's solution. I will try yours, at number 3, too soon. Thanks a lot for taking the effort writing such big solution! :) Sep 6, 2014 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .