LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 08-09-2022, 05:43 PM   #1
noteleks
LQ Newbie
 
Registered: Feb 2011
Posts: 7

Rep: Reputation: 0
Cronjob Date Time not working


I have the following script I did using a website that explained it:

Code:
#!bin/bash

find "/obsbackupfolder" -type f -mtime +5 -exec rm {} \;

# Compress the folder with foldername + date and take backup
filename="backup_ `date +%d`_`date +%m`_`date +%Y`.tar";

# Create compressed file using tar and move to backup folder
tar cvf /obsbackupfolder/backup.tar /home/mintObsidianValults/

# Go to the backup folder location
cd /obsbackupfolder

# List the content
ls

# Show the size of the folder
du -sh
In my CronJob
I have the following:

Code:
0 16 * * * .backup.sh"
0 16 * * * ./remove-backup.sh
I get the backup.tar; but do not get a date or time or both on it. Thank you for any help.

I also checked the backup.tar and all the files are in there.
 
Old 08-09-2022, 05:58 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Code:
# Compress the folder with foldername + date and take backup
filename="backup_ `date +%d`_`date +%m`_`date +%Y`.tar";

# Create compressed file using tar and move to backup folder
tar cvf /obsbackupfolder/backup.tar /home/mintObsidianValults/
Replace backup.tar with the variable $filename in your tar command.

Code:
filename=$(date "+backup_%d_%m_%Y.tar")
A more "modern" way to create the filename.
 
2 members found this post helpful.
Old 08-09-2022, 06:07 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by noteleks View Post
I have the following script I did using a website that explained it:

Code:
#!bin/bash

find "/obsbackupfolder" -type f -mtime +5 -exec rm {} \;

# Compress the folder with foldername + date and take backup
filename="backup_ `date +%d`_`date +%m`_`date +%Y`.tar";

# Create compressed file using tar and move to backup folder
tar cvf /obsbackupfolder/backup.tar /home/mintObsidianValults/

# Go to the backup folder location
cd /obsbackupfolder

# List the content
ls

# Show the size of the folder
du -sh
The fact they explained it is no help to anyone here.

Your script creates a variable filename which includes the date and time, but it never actually uses that variable to create a file with that name.

Then you do not say what that script's name is so it is ambiguous what your cron job should actually be doing, even if it were written correctly... but it isn't...

Quote:
Originally Posted by noteleks View Post
In my CronJob
I have the following:

Code:
0 16 * * * .backup.sh"
0 16 * * * ./remove-backup.sh
I get the backup.tar; but do not get a date or time or both on it. Thank you for any help.

I also checked the backup.tar and all the files are in there.
It is surprising that you produce any backup file at all, but we probably do not have the full picture in the above example.

The first line includes an unmatched double-quote character which is certainly an error.

Both lines use relative file paths which are generally not going to work in a cron spec file. Cron does not run with the same environment as a login shell, so paths like ./remove-backup.sh will be meaningless to it. Always use absolute paths to your scripts in the crontab, or cron spec file.

See if you can fix those problems and if you cannot get it to work please include the actual name of the script and a few more details such as whether the cron runs as root or a normal user and the absolute paths to the scripts.

Good luck!
 
1 members found this post helpful.
Old 08-09-2022, 06:31 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Just as an FYI of the distributions I typically play with, the installed cron version does create a PWD environment variable with the users home directory (i.e. /home/username) so ./remove-backup.sh is valid and will work if the script is located in the users home directory. However, I agree it is much easier to just use the full path and not have to think to much.
 
2 members found this post helpful.
Old 08-09-2022, 07:39 PM   #5
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
This, which is in the script you posted
Code:
filename="backup_ `date +%d`_`date +%m`_`date +%Y`.tar";
would work much simpler with a single call to date as
Code:
filename="backup_`date +%d_%m_%Y`.tar"
(and it eliminates the space in the filename you created)

Then the line
Code:
tar cvf /obsbackupfolder/backup.tar /home/mintObsidianValults/
should be
Code:
tar cvf /obsbackupfolder/"$filename" /home/mintObsidianValults/
so it uses the file name you created with the date included.

Finally, we do not know what the ./remove-backup.sh file does so cannot know if it is correct or not. You have both those jobs running at exactly the same time so we cannot tell if they will do what you want, if they may conflict, or ?
 
1 members found this post helpful.
Old 08-09-2022, 09:13 PM   #6
noteleks
LQ Newbie
 
Registered: Feb 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Alright, it is obvious I need to learn this more. I am going to get some good reference and study. Thank you everyone.
 
Old 08-10-2022, 03:45 PM   #7
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
One very good reference I use for bash scripting is https://tldp.org/LDP/abs/html/
 
  


Reply



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
Trying to get yesterdays date using the date command in a cronjob or cron variable malbe Linux - Newbie 10 05-24-2018 07:39 PM
how to convert windows date and time to unix date and time jitupatil_2007 General 8 03-31-2008 05:58 AM
thunderbird puts time instead of date under 'date' header wabbalee Linux - Software 4 11-26-2006 04:58 AM
Start Date + Time Duration = End Date/Time calculator? ToBe Linux - General 3 09-26-2005 10:17 AM

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

All times are GMT -5. The time now is 08:11 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