Rsync only include files with specific string in name
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.
Solution 1:
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