LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-13-2017, 04:38 AM   #1
taru.tarak
Member
 
Registered: Aug 2016
Distribution: CentOS
Posts: 81

Rep: Reputation: Disabled
Unhappy 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
 
Old 04-13-2017, 04:42 AM   #2
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
Old 04-13-2017, 05:00 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
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.

Code:
man 5 crontab
 
1 members found this post helpful.
Old 04-13-2017, 05:02 AM   #4
taru.tarak
Member
 
Registered: Aug 2016
Distribution: CentOS
Posts: 81

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-13-2017, 05:06 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,504

Rep: Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113Reputation: 4113
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.
Old 04-13-2017, 06:03 AM   #6
taru.tarak
Member
 
Registered: Aug 2016
Distribution: CentOS
Posts: 81

Original Poster
Rep: Reputation: Disabled
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 !
 
Old 04-13-2017, 06:31 AM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
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'
 
Old 04-13-2017, 09:19 AM   #8
taru.tarak
Member
 
Registered: Aug 2016
Distribution: CentOS
Posts: 81

Original Poster
Rep: Reputation: Disabled
Thank you again !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cron job starts 6 hours late, rest of cron jobs work fine pieterhouwen Linux - Newbie 33 10-05-2015 12:38 PM
[SOLVED] [cron] Root cron ib centos 6.3 doesn't work/ killout Linux - Newbie 2 12-17-2012 01:26 PM
difference in size of the tarred files after archiving using tar command emagnun Linux - Newbie 4 05-17-2011 06:48 AM
How to automate archiving of log files using tar? profi81 Linux - Newbie 2 03-29-2011 06:39 PM
update bsd tar for archiving jyoti100in *BSD 2 11-02-2005 03:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration