Linux Lite Forums

Development => Scripting and Bash => Topic started by: kjacobs on December 14, 2015, 01:09:11 AM

Title: Cron job to automate backup????
Post by: kjacobs on December 14, 2015, 01:09:11 AM
OK.....so I am trying to automate a weekly backup of my home folder. I did try the built in backup option in LL, but simply do not like the tons of zip files created during backup. I really only want mirror copies of the home folder on my NAS.

I installed grsync to setup the backup......and the backup works fine. I then created a backup.sh file to run the grsync backup session, made it executable, and tested it......it works fine as well.

Now I tried to add the script to a crontab job......this is where it all fails. I cannot get the file to run at all. I have tried MANY options.....I have added the path to the script to crontab.....tried adding the path in the script.......tried copying the script to the /bin folder.......tried standing on one foot while patting my head. I simply cannot get the file to launch using crontab. I have spent a number of hours looking up the correct formatting on google to get this right and it still fails to run.

I also tried installing gnome-scheduler to do this in a gui and it fails to run as well. Soooooo, what in the world am I missing to get this function to run automagically????

Any help would be great......thanks.

Title: Re: Cron job to automate backup????
Post by: firenice03 on December 14, 2015, 08:07:28 AM
I'm not a CRON expert but....
If not executing, maybe a permissions issue??
Any error's when running?? May have to pipe it out to a txt file to see errors..



Title: Re: Cron job to automate backup????
Post by: Wirezfree on December 14, 2015, 10:00:25 AM
Not sure if it's similar..??
I had some permissions issue trying to run some tasks...
If you run gnome-scheduler from the System menu,
The tasks are only run as "normal user".

But if you open a terminal and do:
Code: [Select]
gksu gnome-scheduler

It now run's as root, so anything you now schedule this way run's with root privileges.
If you open Scheduler from the menu, you do not see the(se) root tasks.
Title: Re: Cron job to automate backup????
Post by: kjacobs on December 14, 2015, 11:34:07 AM
Thanks for the replies......yes, I thought it might be a permissions things as well. I have a copy of backup.sh in a user folder with my user ownership and a copy in /usr/bin/ with root ownership. Neither one seem to run, yet I can double click either and it launches fine. All I seem to get in syslog for this line is this....



Dec 14 08:15:01 kenneth-OptiPlex-755 CRON[8245]: (root) CMD (/usr/bin/backup.sh)
Dec 14 08:15:01 kenneth-OptiPlex-755 CRON[8244]: (CRON) info (No MTA installed, discarding output)


I am not trying to mail anything, as the file should launch and I should see grsync start running when it hits the correct time. It looks like it is "trying" to run???? But nothing is happening.....maybe I am wrong here?
Title: Re: Cron job to automate backup????
Post by: Wirezfree on December 14, 2015, 11:40:07 AM
Does any of this help:
http://askubuntu.com/questions/222512/cron-info-no-mta-installed-discarding-output-error-in-the-syslog (http://askubuntu.com/questions/222512/cron-info-no-mta-installed-discarding-output-error-in-the-syslog)
Title: Re: Cron job to automate backup????
Post by: kjacobs on December 14, 2015, 11:55:04 AM
Thanks for the tip.....that at least got some info on what is going on......got this from the mail file:



Subject: Cron <root@kenneth-OptiPlex-755> DISPLAY=:0.0 /home/kenneth/bin/backup.sh # JOB_ID_3
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <20151214165201.DA4455E165B@kenneth-OptiPlex-755>
Date: Mon, 14 Dec 2015 08:52:01 -0800 (PST)


No protocol specified


(grsync:11062): Gtk-WARNING **: cannot open display: :0.0


Not sure what this means....LOL. Also tried it without the DISPLAY command......still did not start.



Subject: Cron <root@kenneth-OptiPlex-755> /home/kenneth/bin/backup.sh >/home/kenneth/bin/text.txt # JOB_ID_3
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <20151214164801.BDA305E165B@kenneth-OptiPlex-755>
Date: Mon, 14 Dec 2015 08:48:01 -0800 (PST)




(grsync:10646): Gtk-WARNING **: cannot open display:
Title: Re: Cron job to automate backup????
Post by: firenice03 on December 14, 2015, 12:45:11 PM
Quote
(grsync:11062): Gtk-WARNING **: cannot open display: :0.0
Looks like its trying to launch a GUI of sorts..???..??
http://askubuntu.com/questions/614387/gksu-gtk-warning-cannot-open-display-0

Another thought.. isn't CRON (sh) shell based and terminal is BASH... Not sure if this is still the case/true...
But the script runs in terminal and fails in cron - I assume it fails from any shell window...

Maybe its trying to prompt for roots PW??
Something like.. http://stackoverflow.com/questions/22704290/linux-cron-job-fails-to-execute-part-of-script-which-works-in-shell

Hope it helps point in the right direction...
Title: Re: Cron job to automate backup????
Post by: Wirezfree on December 14, 2015, 12:48:10 PM
I'm just stabbing in the dark....
Grsync is a GUI version of rsync..
Maybe that is the Gtk-WARNING cannot open display
Maybe it's trying to output something GUI-wise but it is not running a gui..??
I'm probably not making sense...

Can you not try just rsync..??
Title: Re: Cron job to automate backup????
Post by: Wirezfree on December 14, 2015, 12:50:16 PM
@firenice03
great minds, well hopefully great minds....
Title: Re: Cron job to automate backup????
Post by: firenice03 on December 14, 2015, 12:53:37 PM
@firenice03
great minds, well hopefully great minds....

Yours yes... Mine - I'm getting there  8)  I'm good with "google"....

Yeah - looks like the App is trying to access the display.. This may help..??..
http://promberger.info/linux/2009/01/02/running-x-apps-like-zenity-from-crontab-solving-cannot-open-display-problem/

Title: Re: Cron job to automate backup????
Post by: kjacobs on December 15, 2015, 11:51:44 AM
Well fellas.....I tried a few more options from the links you provided, but still had no luck running grsync. It appears the GUI for rsync is fussy about starting from cron or scheduler.


So, I decided to grab the rsync command that grsync was sending and dropped that direct into scheduler......that seemed to work fine. Then I copied that command into my backup.sh script and ran the shell script from scheduler.....that worked fine too. I even setup rsync to send a log file of the backup, so I can see that everything went fine.


Thanks for the help......
Title: Re: Cron job to automate backup????
Post by: Wirezfree on December 15, 2015, 12:24:34 PM
glad you sorted it... GL
Title: Re: Cron job to automate backup????
Post by: firenice03 on December 15, 2015, 01:19:32 PM
glad you sorted it... GL

Ditto  ;D 8)
Title: Re: Cron job to automate backup????
Post by: kpanic on June 11, 2018, 09:11:46 PM
Hi,

You need to set EDITOR environment variable to some text editor.
I use nano here, you may use emacs, vi or anything really
For example:

shell >  export EDITOR=nano
shell >  crontab -e

## Insert or append a line like:
# min hour day_of_the_month   month   day_of_the_week   command
# So 5 past midnight of every Tuesday do...

5 0 * * 2  tar -cf   ~/back.tar  ~/Documents && mv ~/back.tar  /where-ever  2>&1 > /dev/null

# Save settings = CTRL+O
# exit = CTRL+X