LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Rsync and Crontab Setting up (https://www.linuxquestions.org/questions/linux-general-1/rsync-and-crontab-setting-up-4175451897/)

alaios 02-27-2013 07:23 AM

Rsync and Crontab Setting up
 
Dear all,
I would like to use rsync for my backups (Actually copying the files to an external hard disk. Keep in mind that this is not a discussion if this is an efficient way of keeping backups but is morely for rsync mechanisms)

I have written a small script that contains the followings

Code:


rsync  -rav /home/ /media/defineIt
rsync -rav /etc/ /media/defineIt
rsync -rav /root /media/defineIt

1. Is the -rav for copying the files from the one folder to the another?

2. The script will run with root permissions... how can I see that the execution failed. Let's say that I have a typo in crontab -e, how can I find out that the script never run?

3. If I want inside the destination folder /media/defineIt/ to have the three subfolders /home, /etc, /root, exactly as the three rsync commands provided above, how should I have the / in the rsync. Should it be as it is
Code:

rsync -rav /root /media/defineIt
or change to
Code:

rsync -rav /root /media/defineIt/
I would like to thank you in advance for your replies

Regards
Alex

yowi 02-27-2013 07:53 AM

First I'd suggest you check the man pages for rsync and cron. I say this because -a includes -r, implying you haven't checked the switches.

cron on my system (Debian) logs it's activity to syslog, not sure how Opensuse does things but there will be a log somewhere. If desired your script can use whatever logging or notification mechanism you want. A conditional test and the "mail" command on failure is effective, for example:
Code:

rsync [some params] || mail [message to someone]
And lastly don't believe anything anyone tells you until you've tested it for yourself, which is not at all difficult with shell scripts.

alaios 02-28-2013 01:28 AM

I would like to thank you for your answer.
I would like to ask you about the last / , in the paths, when is needed or not in rsync.

Regards
Alex

rng 02-28-2013 07:21 AM

For rsync, it is easiest to use /source/folder/name1/* /target/folder/name2/ , so that it is clear that all files in source folder name1 need to be copied to folder name2.

Also -r is included in -a. So using -av is enough.

lleb 02-28-2013 07:22 AM

yes you will need the trailing / on the destination and source.

Code:

rsync -aviS /path/to/source/ /path/to/destination/

alaios 02-28-2013 10:26 AM

Thanks and
what happens when trailing / is missing?

as

rsync -aviS /path/to/source /path/to/destination

suicidaleggroll 02-28-2013 10:51 AM

Quote:

Originally Posted by alaios (Post 4901713)
Thanks and
what happens when trailing / is missing?

as

rsync -aviS /path/to/source /path/to/destination

Why don't you try it and see? Make two dummy directories, try various combinations of trailing slashes, and see what happens.


IIRC:
rsync -a /path/to/source /path/to/dest
will copy the folder source into dest (so you'll now have /path/to/dest/source)

rsync -a /path/to/source/ /path/to/dest
will copy the contents of source into dest

rsync -a /path/to/source /path/to/dest/
will copy the folder source into dest (so you'll now have /path/to/dest/source)

rsync -a /path/to/source/ /path/to/dest/
will copy the contents of source into dest

So basically, the only trailing slash that matters is the one on the source directory. That tells rsync whether it should copy the source folder itself into dest, or the contents of source into dest.


But, the only way to know for sure is to try it and see what happens.

rng 02-28-2013 07:57 PM

On my debian-wheezy system, I get following error if /* is not used:
Code:

$ rsync ./tmp2/ ./tmp3
skipping directory .

and the file from tmp2 is not copied to tmp3
Code:

$ rsync -v ./tmp2/* ./tmp3
file1

sent 65 bytes  received 31 bytes  192.00 bytes/sec
total size is 0  speedup is 0.00

The file in tmp2 is copied properly.

suicidaleggroll 02-28-2013 08:16 PM

Quote:

Originally Posted by rng (Post 4902019)
On my debian-wheezy system, I get following error if /* is not used:
Code:

$ rsync ./tmp2/ ./tmp3
skipping directory .

and the file from tmp2 is not copied to tmp3
Code:

$ rsync -v ./tmp2/* ./tmp3
file1

sent 65 bytes  received 31 bytes  192.00 bytes/sec
total size is 0  speedup is 0.00

The file in tmp2 is copied properly.

It's because you didn't set the recursive flag.

allend 02-28-2013 08:33 PM

If you are going to use rsync in a script to be run as a cron job, I suggest that you:
- specify the full path to rsync so that the rsync binary can be found (the path in the shell started to execute the cron job may not include the path to rsync)
- use the options '-aq' to rsync otherwise you will get very long emails if you are backing up lots of files.
e.g.
Code:

/usr/bin/rsync -aq /path/to/sourcefiles/ /path/to/destinationfiles


All times are GMT -5. The time now is 01:30 PM.