How to use crontab to copy a dynamic (yyyymmdd) file.

Dear all,

I would like to centralize files into one file server and backup the central file server (running on OS X 10.5).

Everyday there is a database dump file "" that I need to copy to the server. Is there a UNIX command that can gegerate this filename so that I can use crontab e.g.

0    5    *    *    * cp /Volumes/bak/<YYYYMMDD> /Data/Daily-dmp-bak/

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kerem ERSOYPresidentCommented:
If what you want is to copy the database dump to another server you coulf use SCP. You can create a key with scp so that it wont be asking password thus it is safe t1 use in a script and wont require you to embed a user account password in it.
It is beatter to creat e a script file and later execute it trough cron:

0 5 * * *  /path/to/mvdbbackup

TRGDT=$(date -d yesterday +%Y%m%d)  
/sbin/scp -i /home/root/.ssh/identification  /Volumes/bak/$TRGDATE backuser@backupsrv:/backups/$HOSTNAME/$TRGDATE

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
try with

0    5    *    *    *  /usr/bin/cp /Volumes/bak/`/usr/bin/date "+%Y%m%d"` /Data/Daily-dmp-bak/

please note the inverted commas around the date command

Kerem ERSOYPresidentCommented:
My script assumes that your default shell is /bin/bash you might like to cahnge it to /bin/sh or ather shell of your preference.

When I am calculating I am assuming that you are moving yesterdays backup because you're running the scrip at 0:5 an there's a good change that the backup will have previous days stamp. Hence the switch to date is -d yesterday. If you want it to be today just remove the -d yesterday part.

it also assumes that your scp is installed under /sbin. To determine the correct place isue a "which scp" on your system and replace with the correct path. Cron scripts  are not executed under the default environment so it might not include each path as our interactive shell so each command must be an absolute path to ensure correct operation.
Lastly it assumes that your remote system key is placed under /home/root/.ssh/identification. Please generate it over the target system and place it under a folder on the source system and adjust the path accordingly.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Kerem ERSOYPresidentCommented:
I am also assuming that what you mean by  /Data/Daily-dmp-bak/ is the path is on a remote (central) server so I've preferred the scp command instead of copy. Since I've been using the date statement twice in my solution I've preferred to place it in a script rather than executing directly under the shell so that it wont be calculated twice.
Hi there,

Normally it would be seen as a cleaner solution to make the script that kicks off the database dump to also send the dump file to its final resting place.

You could add KeremE's code snippet at the end of the existing script that kicks off the db dump.

Its always better to have associated tasks handled by the same script, it leaves the system neat.

0    5    *    *    *  /usr/bin/cp /Volumes/bak/$(/usr/bin/date "+\%Y\%m\%d")/Data/Daily-dmp-bak/
Note - the above is omarfarid's solution, but with the '%' signs "escaped", which is mandatory with cron.

Thanks woolmilkporc :)

this is somrthing new to me, what is the idea behind it?
Hi omarfarid,
it's a neat feature of cron - if there is a percent sign somewhere in the command field, cron will treat everything preceeding it as the actual command invocation and will make all that follows it available to the command as standard input (STDIN).
This feature is rarely used, as far as I can see, but can be very handy!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.