Score:1

Rsync only include files with specific string in name

id flag

I want to use rsync to backup databases from my webserver.

I created a cronjob on the webserver that creates a mysqldump of the database and names it with the current date in the beginning every night. Files older than 30 days will be deleted.

Now I want to backup only the databasefile of the current date to my NAS via a cronjob.

How can I only include that one file? Or vise versa, how can I exclude everything except that one file?

My code for rsync is the following:

rsync -avz -h -c --include="*$(date +%Y-%m-%d)*" -e "ssh -p $PORT" $USER@$SERVER:$SQL_SOURCE $SQL_TARGET --delete

The filename for today thats on the webserver would be 2021-06-30_databasename.sql
But also every other file like 2021-06-29_databasename.sql gets synced.

Score:1
us flag

Rather than syncing a path (and then defining file name inclusion/exclusion patterns) simply point rsync at the single filename you want to copy...

 filename=$(date +%Y-%m-%d_databasename.sql)
 rsync host:/path/to/file/$filename  /dest/

Note that this won't do any clean up of old files in /dest/ and a year from now there will be 366 files there

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.