If you need to perform some rsync/tar work inside a specific directory then just cd to the directory and run the backup commands inside the script. You don't need to care about "sourcing" (as in using the source keyword) the script to alter the current directory of the parent shell process.
To explain better, suppose you've got this simple script (foobar.sh) which executes a tar command inside the /foo/bar directory:
Code:
#!/bin/sh
cd /foo/bar
tar cjf foobar.tar.bz2 *
When you launch the script on the command line, the shell (read: parent process) creates a new shell process (read: child process) containing a copy of its environment (including current directory information) and the script gets executed inside this subprocess. Then:
- the parent process goes to sleep until the child terminates;
- all changes performed by the script to the shell environment (such as a cd command) are indeed applied only to the child shell process and will not outlive it;
- the current directory is then changed to /foo/bar and the tar command is performed there;
- when the child process terminates (after the tar line) it is destroyed and control returns to the parent shell process which awakes from its sleep
You can test this behavior adding simple echo inside your scripts before and after the "cd"s command:
Code:
echo "Current dir is $(pwd)"
cd /some/dir/somewhere/in/a/distant/galaxy/long/long/time/ago
echo "Current dir is $(pwd)"
You should see that the "cd" command is indeed performed, but a pwd command executed after the script will show no change in current directory.