Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an rsync script that syncs logs files from a remote server. The log files are always named YYYY-MM-DD-HH-MM-SS.

At the moment, I have a filter in place to only get the current year's logs:

- 2009*
- 2010*
- 2011*
- 2012*
+ 2013*

My question is this....if I only wanted to sync two months worth (current month and previous month), is there a way to use a variable in the script (i.e, + yearVariable-MonthVariable*) without having to update it constantly by hand?

Thank you!

share|improve this question
up vote 0 down vote accepted

Use date and it's calc functions to aid you:

date --date="1 months ago" +%Y-%m

will currently output 2013-03. Store this in a var and use it to construct your filters.

pm=$(date --date="1 months ago" +%Y-%m)
cm=$(date +%Y-%m)

Filters in rsync string: -2012* +$cm +$pm

or something like that.

share|improve this answer
Perfect - thank you! Do you know if I can do this directly in the rsync filter txt? (i.e, date1=date --date="1 months ago" +%Y-%m, date2=date +%Y-%m, + date1, + date2) – Jason Apr 6 '13 at 12:18
Yes, you can, but it's not a good idea IMHO, as the command gets unreadable quite fast. Use variables like in my edit. – Sven Apr 6 '13 at 12:19
Thanks - your updated post just answered my question! Thanks! – Jason Apr 6 '13 at 12:19
Hello, I just implemented this in my rsync filter but it still is trying to sync all of 2013. Was wondering if you might be able to have a quick look and see what I'm doing wrong? Thank you so much! PM=$(date --date="1 months ago" +Y%-%m) CM=$(date +%Y-%m) - 2009* - 2010* - 2011* - 2012* + $PM* + $CM* – Jason Apr 8 '13 at 20:16
Could you edit your complete rsync command into your question? – Sven Apr 8 '13 at 21:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.