Schedule cron jobs to run every 2 weeks

There is no straight forward way to schedule a cron job so that a certain command gets executed biweekly, let’s say every 2 sunday nights…

To be able to do this, you need to extend your cron command with a command that tests the day of the week.

In my case I needed to perform a ‘clone MySQL database’ on the 2nd and 4th sunday of each month to a sandbox¬†environment.

This is how I’ve implemented this:

You see that the cron job itselfs is scheduled to happen from:

The 8th until 14th of each month and from the 22nd until the 28th of each month.

When executing, I test if the day is the 1st of the week (Sunday = day 1 of the week). When this result is positive, the shell script will be executed.

Rsync files younger or older than

Today I needed to copy all files from a certain folder, but younger than 1 month  to a remote machine.

I knew how to use find of course… and rsync is pretty easy too… Combining these 2 needs some explanation:

Finding files younger than 31 days is performed like this:

Rsyncing all files from a folder is pretty easy too:

Now, if you would like to rsync only the filers younger thn 31 days from your source folder, you can combine rsync and find in this way:

However… this is not 100 working as intended… This will skip filename with spaces, as the space are not escaped during the rsync…

The solution for this is to split the one liner above into 2 seperate commands again:

1) perform the find, and write the output to a tempfile:

2) perform the rsync with the –from-files parameter:

(do not forget the . in the command to declare a ‘source dir’)

Remark: When using relative paths, you could/should change the /path/to/folder to path/to/folder and put a . instead of a / in your rsync command
E.g. like this: