Linux - SoftwareThis 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
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.
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.
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...
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?
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?
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.
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
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.
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... (:
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) 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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.