LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-08-2020, 03:12 AM   #1
alex4buba
Member
 
Registered: Jul 2020
Posts: 624

Rep: Reputation: Disabled
Anacron - instaead of cron setup


I am now with KDE-Plasma 20.04 and being a Desktop machine, it is not always ON, so at times, my Cron job is not executing.

So, following suggestion here in the forum, I am trying to setup a Anacron job instead.

My cron job is looking like the following script. Once a day, at a selected time (18:00) it needs to run and perform a backup of my local folders into an external hard disk, each day into a designated folder - sun,mon,tue etc...

Code:
00 18 * * 0 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/sun/
00 18 * * 1 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/mon/
00 18 * * 2 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/tue/
00 18 * * 3 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/wed/
00 18 * * 4 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/thu/
00 18 * * 5 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/fri/
00 18 * * 6 /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/sat/
My first attempt with anacron is the following:

Code:
1 10 friday-job /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/fri/
But,
1) When I tried to run in terminal the command : sudo anacrontab -e , there is no such command
2) So, how do I actually create an anacron job?
3) Is my command above correct for anacron?
4) Do I need to create 7 anacron jobs, one for each day, or can I somehow execute ONE single anacron job and point it to the above cron job?

Thanks to any helper
Alex
 
Old 09-08-2020, 04:49 AM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,758

Rep: Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224
man anacron
man anacrontab
 
Old 09-08-2020, 05:12 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,796

Rep: Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952
3,4) create a script that uses day of week as a variable and then the anacron job will execute that script every day.

2) edit the /etc/anacrontab file and add your job to run the backup script.
 
Old 09-08-2020, 06:56 AM   #4
alex4buba
Member
 
Registered: Jul 2020
Posts: 624

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
man anacron
man anacrontab
Hello again,

I read the manual here: https://www.tecmint.com/cron-vs-anac...cron-on-linux/

My example above is following these instructions : period delay job-identifier command

Code:
1 10 friday-job /usr/bin/rsync -r /home/alex/afolders/ /media/alex/Elements/fri/
1=period = daily
10=delay=10 minutes
friday-job=job identifier
The rest is the command to execute

So, I will repeat my questions:

1) Is my anacron command structured correctly?
2) Can I have my CRON script, executed by Anacron? How
3) Or, do I have to create an Anacron Job for each day, to go into the correct day of the week?

Thanks
Alex
 
Old 09-08-2020, 06:59 AM   #5
alex4buba
Member
 
Registered: Jul 2020
Posts: 624

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
3,4) create a script that uses day of week as a variable and then the anacron job will execute that script every day.

2) edit the /etc/anacrontab file and add your job to run the backup script.
Thanks for your feedback, but can you ellaborate?
I am not clear on:

Create a script? How
Edit /etc/anacrontab - how?

Thanks
Alex
 
Old 09-08-2020, 07:50 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,796

Rep: Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952
1) Yes, but will not work as expected.
2) No
3) No
Using a text editor copy and paste the following code
Code:
#!/bin/bash

#get day of week
dow=$(/usr/bin/date "+%a")

# convert DOW to lower case i.e. Mon to mon
ldow=${dow,,}

/usr/bin/rsync -avzr /home/alex/afolders/ /media/alex/Elements/$ldow/
To execute the file run the command:
chmod 755 my_backup_script

/etc/anacrontab is a regular text file. You can use any text editor but you need to be root or use sudo. nano is a simple command line text editor.

sudo nano /etc/anacrontab

Code:
1 10 my-backups /path/to/my_backup_script
Will execute the script everyday if the computer is running and rsync the files to the dow of the week directory.

Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
This is a typical anacrontab file. Your script will run 10 minutes + the random delay after the computer boots or after the start_hour_range.
 
Old 09-08-2020, 04:11 PM   #7
alex4buba
Member
 
Registered: Jul 2020
Posts: 624

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
1) Yes, but will not work as expected.
2) No
3) No
Using a text editor copy and paste the following code
Code:
#!/bin/bash

#get day of week
dow=$(/usr/bin/date "+%a")

# convert DOW to lower case i.e. Mon to mon
ldow=${dow,,}

/usr/bin/rsync -avzr /home/alex/afolders/ /media/alex/Elements/$ldow/
To execute the file run the command:
chmod 755 my_backup_script

/etc/anacrontab is a regular text file. You can use any text editor but you need to be root or use sudo. nano is a simple command line text editor.

sudo nano /etc/anacrontab

Code:
1 10 my-backups /path/to/my_backup_script
Will execute the script everyday if the computer is running and rsync the files to the dow of the week directory.

Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
This is a typical anacrontab file. Your script will run 10 minutes + the random delay after the computer boots or after the start_hour_range.
So, let me see if I got it right

1) "my_backup_script" can be any name? so I can call it - anacrontab ?
2) I understand the 1st script, it takes the dow from the system, converts it to lower case. I assume the day format is per my folders in the external target drive : sun,mon,tue etc... correct?
3) I create that file and place it into the /etc folder?
4) path to my/backup/script is /etc/anacrontab?
5) Not clear - what do I do with the following? Create another file? Where?

Code:
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
Thanks for your help
Alex
 
Old 09-08-2020, 04:36 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,796

Rep: Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952
1) You could but not a good idea.
2) Correct.
3) You can place it anywhere. By convention /etc is for configuration files.
4) No, where ever you create and place the your script is the path. /etc/anacrontab is the configuration file where you place jobs to be run.
5) Nothing, that posted code is the from the /etc/anacrontab file and probably added more to the confusion.
 
Old 09-08-2020, 05:14 PM   #9
alex4buba
Member
 
Registered: Jul 2020
Posts: 624

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
1) You could but not a good idea.
2) Correct.
3) You can place it anywhere. By convention /etc is for configuration files.
4) No, where ever you create and place the your script is the path. /etc/anacrontab is the configuration file where you place jobs to be run.
5) Nothing, that posted code is the from the /etc/anacrontab file and probably added more to the confusion.
So, in /etc - is there an actual file called anacrontab? or is anacrontab a folder inside /etc?

Sorry, once I do that, how do I get rid of my cron job?

I am confused all by myself, no need to help me with that... (:

Cheers
Alex
 
Old 09-08-2020, 05:21 PM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,796

Rep: Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952
1) Yes, there is a file called anacrontab located in the /etc directory. It is the anacron configuration file.

2) Run the command crontab -e which starts the editor and opens your crontab file. Delete the line containing your cron job, save and exit.
 
Old 09-08-2020, 05:37 PM   #11
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
2) Perhaps a safer plan is to just comment out the line, so in case you need it again it's still there, ready to be used. A # comments everything on a line after it.
 
1 members found this post helpful.
Old 09-08-2020, 06:06 PM   #12
alex4buba
Member
 
Registered: Jul 2020
Posts: 624

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
1) You could but not a good idea.
2) Correct.
3) You can place it anywhere. By convention /etc is for configuration files.
4) No, where ever you create and place the your script is the path. /etc/anacrontab is the configuration file where you place jobs to be run.
5) Nothing, that posted code is the from the /etc/anacrontab file and probably added more to the confusion.
My command in the cron job had just -r flag, you however includded "avzr" why? I intend this job to run in the background, what for the "v" flag - verbose? Who is watching it? what is z?
My cron job was running (if thye machine was on) OK, just with the -r flag

Do I really need any other?

Thanks again
Alex
 
Old 09-08-2020, 06:37 PM   #13
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
IMO, 'a' is a better choice than 'r', because it includes 'r' but does more. Most people would, I believe, use -a as a matter of course for a backup, unless they had a special situation.
Quote:
-a archive mode; equals -rlptgoD (no -H,-A,-X)
For an explanation of -rlptgoD, run rsync --help in a terminal. Or read the manual by running "man rsync" in a terminal.
With -v, you could pipe the output to a log file, which you could read at your leisure and see if things were actually being done the way you want. With no pipe, it just goes to standard out, and does no harm. The -z option compresses the data (only during transmission, not on file write) for faster and more efficient operation. It's your choice, though, and just -r alone will work.
 
Old 09-08-2020, 06:42 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,796

Rep: Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952Reputation: 5952
I agree.

Is your external hard drive formatted using a linux filesystem? Probably a bit overkill

anacron runs as root versus your regular user so the -a basically preserves ownership, group, permissions, time stamps and copies links. The -vz is not necessary.
 
Old 09-08-2020, 06:45 PM   #15
HappyTux
Senior Member
 
Registered: Mar 2003
Location: Nova Scotia, Canada
Distribution: Debian AMD64
Posts: 4,170

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by sgosnell View Post
IMO, 'a' is a better choice than 'r', because it includes 'r' but does more. Most people would, I believe, use -a as a matter of course for a backup, unless they had a special situation. For an explanation of -rlptgoD, run rsync --help in a terminal. Or read the manual by running "man rsync" in a terminal.
With -v, you could pipe the output to a log file, which you could read at your leisure and see if things were actually being done the way you want. With no pipe, it just goes to standard out, and does no harm. The -z option compresses the data (only during transmission, not on file write) for faster and more efficient operation. It's your choice, though, and just -r alone will work.

Code:
seeder1@haswell:~$ man rsync
.... snip

--log-file=FILE         log what we're doing to the specified FILE
Is probably the best for doing a log. My favourite options for the command.

Code:
MacUser2525:~$ alias rs
alias rs='rsync -avP'
 
  


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
Learning cron : CentOS7 : anacron and cron fanoflq Linux - Newbie 2 11-24-2016 09:50 PM
LXer: What is Anacron and usage of Anacron in Linux LXer Syndicated Linux News 0 01-14-2015 06:30 AM
LXer: What is Anacron and usage of Anacron in Linux LXer Syndicated Linux News 0 01-14-2015 02:00 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron / anacron / fcron Will Linux - General 3 11-09-2001 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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