Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-13-2017, 04:38 AM
|
#1
|
Member
Registered: Aug 2016
Distribution: CentOS
Posts: 93
Rep: 
|
Tar archiving via cron does not work
Hello,
I am facing an issue with tar archiving, the execution of tar in the command line works fine without problems:
tar -czf /backup/test/bkp_`date +%d%b%Y_%H%M%S`.tar.gz -P /var/www/html/tarak
However, when I schedule the command for a daily backup (let's say at 13.15) in crontab -e, it is not executing, i.e. the backup does not update using the following command:
15 13 * * * tar -czf /backup/test/bkp_`date +%d%b%Y_%H%M%S`.tar.gz -P /var/www/html/tarak
=======================
CentOS Version: 6.8 (Final)
Is there a way how I can get this running? What is the recommended ?
Any suggestion would be appreciated.
Regards,
Tarak Nath
|
|
|
04-13-2017, 04:42 AM
|
#2
|
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375
|
Is cron finding tar, that'd be my first question, when it comes to cron jobs, I always make sure to use absolute links to programs since cron does not invoke /etc/bashrc and so will not have the same settings for PATH
To confirm where tar is.
Code:
# which tar
/bin/tar
A quick example of how PATH can differ
Code:
# echo $PATH
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# crontab -l
* * * * * echo $PATH > /root/cronpath
# cat /root/cronpath
/usr/bin:/bin
Last edited by r3sistance; 04-13-2017 at 04:45 AM.
|
|
|
04-13-2017, 05:00 AM
|
#3
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,739
|
Yes, and look for path to date as well. But here the problem is most likely the unescaped percent signs.
Code:
tar -czf /backup/test/bkp_$(date +\%d\%b\%Y_\%H\%M\%S).tar.gz -P /var/www/html/tarak
See the crontab manual page for the details.
|
|
1 members found this post helpful.
|
04-13-2017, 05:02 AM
|
#4
|
Member
Registered: Aug 2016
Distribution: CentOS
Posts: 93
Original Poster
Rep: 
|
Thank you for the quick reply.
I have checked the tar path and its /bin/tar
Now I have modified the cronjob as below:
18 15 * * * /bin/tar -czf /backup/test/bkp_`date +%d%b%Y_%H%M%S`.tar.gz -P /var/www/html/tarak
But it's still not executing.
|
|
|
04-13-2017, 05:06 AM
|
#5
|
Moderator
Registered: Aug 2002
Posts: 26,730
|
Welcome to LinuxQuestions.
cron uses % as a special character and needs to be escaped.
Code:
15 13 * * * tar -czf /backup/test/bkp_`date +\%d\%b\%Y_%H\%M\%S`.tar.gz -P /var/www/html/tarak
Due to how sorting works I always suggest file naming using Year first.
Too late...
Last edited by michaelk; 04-13-2017 at 05:07 AM.
|
|
1 members found this post helpful.
|
04-13-2017, 06:03 AM
|
#6
|
Member
Registered: Aug 2016
Distribution: CentOS
Posts: 93
Original Poster
Rep: 
|
Thank you very much. I have again modified the cron as below:
25 16 * * * /bin/tar -czf /backup/test/bkp_`date +"\%Y\%b\%d_\%H\%M\%S"`.tar.gz -P /var/www/html/tarak
And its executing perfectly without any problem. What a quick solution ! 
|
|
|
04-13-2017, 06:31 AM
|
#7
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
echo is a good tool for this type of debugging, IMO.
That and the cron's mail service.
Code:
echo $(which tar) -czf /backup/test/bkp_$(date +"%d%b%Y_%H%M%S").tar.gz -P /var/www/html/tarak
I removed the will-be-deprecated back ticks in favor of new styple $(sub_shell). This keeps me and my
quotes nice and happy.
Just sayin'
|
|
|
04-13-2017, 09:19 AM
|
#8
|
Member
Registered: Aug 2016
Distribution: CentOS
Posts: 93
Original Poster
Rep: 
|
Thank you again ! 
|
|
|
All times are GMT -5. The time now is 10:10 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|