Linux Tips : CRON

I’ve now been using Manjaro Linux since the start of 2020.

So far so good, I’m getting used to the way it works. Linux quirks that can be a real pain to learn or get used to. Coming from Windows after using it for over 30 years, for about 90% of the time I’m pleased with it.

Unlike Windows it’s harder to use, to modify or create files, folders in certain places etc can be a real pain, because of Linux’s security, I suppose it’s a good thing, but wrapping you head around how to modify a dopey file or to add a line can be more than just a pain in the ass.

In a these posts, I’m trying to write down things that I found really hard to do or learn, or very time consuming to do, hoping that maybe of some use to someone.

This tip is how I backup certain of my files and folders that I need or want or keep if ever I reinstall Linux , I’m talking about everyday files and folders here, mostly from My Documents folder, not a system backup, using a specific program like TimeShift for instance.

Cron

A Simple way to back up files and folders other than a system backup, or backups that are compressed, all mixed in one big file and complicated use

I wanted to backup my Document folder and it’s contents from my internal Linux system HD to another internal HD.
As with Windows if my system crashes, Windows or now Linux, I don’t normally bother  about repairing a system, I just wipe the HD and do a fresh install as it’s nearly always quicker, especially with Linux (no bothering with product keys, and checking with MS to see if it’s a real copy or not) Just download the latest ISO and hop it’s done. Reinstalling Linux is hours quicker than reinstalling Windows

For my everyday backups with Windows 10. I used a rather good program called Bvckup2, cost about $30 and was by far the easiest to set up and run.
Of course this doesn’t run in Linux and after a couple of questions on the Telegram group and a IRC chat group that I’m often use, people suggested me a couple of native Linux programs, which I downloaded and tried but found them all over complicated (Linux….), or outdated, or weren’t very good, then someone talked about ‘Cron’ amongst other things…. and of I went starting to learn a little, very little about this Cron thingy

This took a lot of time to get my head around it, and need to ask many questions on the forums to understand the basics, but finally, it was quite simple…… (remember above : “but wrapping you head around how to modify a dopey file…”) after having tried a few specific programs I found that it was by far the best and easiest for what I needed.
Cron works with just a file placed in the ‘cron.d’ folder which is in your /etc/ folder.

It’s just a file (for us Windows guys its just a text file without the .txt extension) that you write and drop it in the Cron.d folder on the system disc.
Linux being Linux, and just a tad more complicated that Windows because of permissions that are needed the file ‘needs Root permissions’ as the owner, but when you write a file in Linux the owner is you and not root. So you need to know how to create file with root permissions.

Note : I’m not a Linux geek, and know next to nothing about it, but as I play with it, retired… have time… I sometimes manage to get things working, Cron for instance. It took me yonks to get it to work, reading on the web, the forums the chats. So I hope this post will help you and save some time.

For starters

Don’t forget I’m using Manjaro XFCE so maybe your Linux isn’t the same as mine.

In the file system you should have a folder cron.d, it’s not hidden so easy to find /etc/cron.d/

On my Raspvberry pi machine, cron is not installed by default and needed to add it and rsync as well, plus some options to get them both working….. another story , another post maybe

In this folder you need to create a file with root permissions (important root permission, if you do with user permission, it won’t work) and name it, mine is called ‘Copy_docs’, Once this done, you can write the cron commands in the file. Here is mine.

To create a file with root permission, this is what I do :
All using terminal
cd to the cron.d folder
cd /etc/crond.d
Then :
sudo nano my_file_name_that_I_want_to_use OK

or you can do
sudo nano /etc/cron.d/my_file_name_that_I_want_to_use OK
without doing the cd….. before as the /etc/cron.d/ puts the file there


enter your password

This will open a empty ‘nano’ window (empty because the file doesn’t exist yet) in this rather strange window, similar to a text app, write in the cron commands text, or paste it (see just below)
Change the time and paths as needed
Once this nano window is filled in, do a Ctrl o (^o) then OK, then Ctrl x (^x)
you will now have the file saved as ‘root’ owner in your cron.d folder

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=””
MAILFROM=””
10 09-22 * * * root rsync -avhW /home/trevor/Documents/* /run/media/trevor/BackUp_1/Copy_trevor_home_documents

Now most of this I haven’t a clue what it means, but it works. I know that the mailto and mailfrom is so that you can send and receive mails when it runs, but you need add more software to your Linux and I tried but didn’t understand it , which is a shame as a mail telling that it’s run, would be nice, will try again one day. (Update got that working, will try and explain in a new post)

This part I just copied from examples that I found elsewhere, The first two lines I haven’t a clue what they mean.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=””  (having the “” means the Cron skips this)
MAILFROM=””  (having the “” means the Cron skips this)

This part is a little is what I created
10 09-22 * * * root rsync -avhW /home/trevor/Documents/* /run/media/trevor/BackUp_1/Copy_trevor_home_documents
That’s all one long line in my file, but WordPress breaks it into two lines because there’s a space between the /* and /run

10 09-22 * * *
This is cron timing, here I tell it to run at 10 minutes past the hour, every hour from 9am to 10pm, as with everything be careful here , I once added one too many *, nothing worked after……
https://crontab.guru/ Use this site to change, or setup your timing, it’s fairly easy to understand, basically cron time is ‘5 stars’, each star meaning something.

root rsync -avhW / Not sure about all this, rsync is what cron uses for copying and the avhW it’s to do with copying permissions , so I was told. It works….

(from) home/trevor/Documents/*
My home folder ‘Documents’ (it’s the same as the My Document Folder in Windows), the /* after means everything in it, all sub folder and files.

(To) /run/media/trevor/BackUp_1/Copy_trevor_home_documents
My BackUp destination, /run/media/trevor/BackUp_1/Copy_trevor_home_documents

So there you go, A small text file (no .txt) after the name of the file in the cron.d folder in /etc/cron.d and my files are copied easily

Just for fun, here’s another file for you
Copies my music files once a month at 11:45 on the first day of each month, From Back_1 to BackUp (BackUp a third HD BackUp in my setup)

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=””
MAILFROM=””
45 11 1 * * root rsync -avhW /run/media/trevor/BackUp_1/All_Music/* /run/media/trevor/BackUp/Music/

Hope this helps, after writing this I’ve got the MailTo and MailFrom to work, will do a another post as again it’s quite complicated, welcome to Linux 🙂

See : https://minty95.wordpress.com/2020/04/27/linux-tips-ssmtp/

Once done replace MAILTO=”” with MAILTO=mymail@gmail.com

Please don’t hesitate if you have a question, I not very good a Linux, but I’m learning …… and if I can help…