tar archiving via cron does not work - linux

I am trying to archive my localhost's root folder with tar and want to automate it's execution on a daily basis with crontab. For this purpose, I created a 'backupfolder' in my personal folder. I am running on Ubuntu 12.04.
The execution of tar in the command line works fine without problems:
sudo tar -cvpzf backupfolder/localhost.tar.gz /var/www
However, when I schedule the command for a daily backup (let's say at 17.00) in sudo crontab -e, it is not executing, i.e. the backup does not update using the following command:
0 17 * * * sudo tar -cpzf backupfolder/localhost.tar.gz /var/www
I already tried the full path home/user/backupfolder/localhost.tar.gz without success.
var/log/syslog gives me the following output for the scheduled execution:
Feb 2 17:00:01 DESKTOP-PC CRON[12052]: (root) CMD (sudo tar -cpzfbackupfolder/localhost.tar.gz /var/www)
Feb 2 17:00:01 DESKTOP-PC CRON[12051]: (CRON) info (No MTA installed, discarding output)
/etc/crontab specifies the following path:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I assume that crontab is not executing as this is a sudo command.
Is there a way how I can get this running? What is the recommended, safe way if I don't want to hardcode my root password?

Well, the command that works for you is
sudo tar -cvpzf backupfolder/localhost.tar.gz /var/www
Which means, you have to run the command with sudo access, and it will not work from within your crontab.
I would suggest adding the cron job to the root user's crontab.
Basically, do
sudo crontab -e
And add an entry there
0 17 * * * cd /home/user/backupfolder && tar -cpzf localhost.tar.gz /var/www
If that doesn't work, add the full path of tar (like /bin/tar).
Also, while debugging, set the cronjob to run every minute (* * * * *)

Basically the problem is the sudo command so we will allow sudo to run tar for the "user" without prompting for the password.
Add the following line in /etc/sudoers file.
user ALL=(ALL) NOPASSWD:/bin/tar
where user is the user installing the crontab.

I suspect a PATH problem, try to set some variables at the top of sudo crontab -e :
MAILTO=your_email#domain.tld # to get the output if there's errors
PATH=/usr/bin:/bin:/usr/local/bin:/usr/local/sbin:/sbin

You can write your command in a script like run.sh
#/bin/sh -l
tar -cvpzf backupfolder/localhost.tar.gz /var/www
then use the crontab to run the script.
IMPORTANT NOTE: the script's first line has the "-l" option.
Try it.

Related

Debian 9 problem with crontab permissions

I installed Debian 9 on my VPS. I installed LAMP on the server. I'm logged in as root, I created a new site "/var/www/example.com" and I see that the permissions are "root:root". The web page is displayed in the browser.
I created a cron.php file that writes the current time to the file. In crontab I have /usr/bin/php /var/www/example.com/cron.php. If I run this command through the terminal, everything works. However, Crontab returns an error because it does not have write permissions. However, Crontab runs as root. The directory has 777 permissions.
I tried to set /var/www as www-data:www-data and the same for crontab (crontab -u www-data -e). The result is the same, cron runs but does not write to the file.
EDIT:
I found that if the script contains: file_put_contents('output.txt', 'xxx'); the file created by cron is in root. If I set the full path, everything is fine: file_put_contents('/var/www/exmaple.com/output.txt', 'xxx'); Is there any way to modify this behavior?
You can create sample script like this:
#!/bin/bash
source ~/.bashrc #or use .bash_profile
/usr/bin/php /var/www/example.com/cron.php >>/path/to/output
and add it as cron record:
0 * * * * /path/to/script/sh

How to run linux commands using cron

I have a couple of linux commands I want to run everyday in the morning once using cron. Not sure how to do this though.
I understand that this needs to be done using shell, but I don't know how to do all this in linux. I will be able to create cron using cpanel though...
These are the commands
rm -rf <directory>
mkdir <directory>
chmod 777 <directory>
You can create a shell script with this commands in a file script.sh, for example:
#!/usr/bin/bash
rm -rf <directory>
mkdir <directory>
chmod 777 <directory>
<others commands or logical instructions>...
In linux you can add a cron job in crontab, with crontab -e command, or in /etc/cron.d directory. The difference is that with command crontab -e the cron job will be set to user that execute crontab -e and add a cron job file right in cron.d you will need to put the user ahead the command of cron job.
Examples of cron jobs to be executed at 06:00 am.
With crontab -e:
0 6 * * * /usr/bin/bash /path_to_script/script.sh
Creating a file in /etc/cron.d:
0 6 * * * root /usr/bin/bash /path_to_script/script.sh
Alternatively you can just put the commands in your cron job as:
0 6 * * * rm -rf <directory> && mkdir <directory> && chmod 777 <directory>
Attention: remember to put the absolute path to directories that want to remove or create
PS: you can make your scripts in any language and use shell calls, as php with shell_exec() function or system() function of perl.

Linux script is unable to delete files via crontab, but it works manually

I have a simple script file to copy all files to a remote server and then delete them all. I could run this script by "user" manually, when i add into crontab (user), the first part, scp, works fine, but the rm part is always with failure.
i wonder what i am missing or set up incorrectly, could somebody help me out with this ?
thanks in advance
/home/user/bin/test.sh
#!/bin/bash
scp -v -r /var/spool/asterisk/monitor test#xx.xx.xx.xx:/home/test/audio&&sudo rm -f /var/spool/asterisk/monitor/*
access permission of /var/spool/asterisk/monitor
drwxr-xr-x. 1 root root 532 Sep 06 11:14 monitor
crontab - user]
* */1 * * * bash /home/user/bin/test.sh
try this, it will work if sudo does not require password ( and it is possible ) )
scp -v -r /var/spool/asterisk/monitor test#xx.xx.xx.xx:/home/test/audio && ssh test#xx.xx.xx.xx "sudo rm -f /var/spool/asterisk/monitor/*"
Make sure requiretty is off in /etc/sudoers. It is normally on by default on Red Hat.

Crontab executes shell script: Mount error(13): Permission denied

I have got a RasPi and I actually try to execute a shellscript to automount a folder at every Reboot.
Script Command is:
sudo mount -t cifs 'folderpath' 'pointtomount' -o username=xxx,password=xxx,sec=ntlm
It works perfect if I use it manually but via cronjob it responses "Mount Error(13): Permission denied" and the mount can't be executed.
Means cronjob executes the file at least.
My idea was to mount it manually and check if automount is disabled in /etc/fstab or /etc/mtab. As it's just a folder I only found it in mtab.
I can't write in it but nowhere's "noauto" in the options so probabbly everything is correct.
Not certain if it has sth. in common with crontab execute rights but ls -lha /usr/bin/crontab output is -rwxr-sr-x 1.
If anyone of you got any clues how to solve this problem, i'd appreciate help.
Thanks
EDIT1:
Okay after hours and hours it seems to be working in /home/pi/.config/lxsession/LXDE-pi/autostart.sh (type "sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart.sh"). In that file I wrote "#/home/pi/scripttoexecute.sh". In my executescript I wrote "sudo mount -t cifs 'foldertomount' 'directorypath' -o credentials=/root/.smbcredentials,iocharset=utf8,file_mode=0777,dir_mode=0777,sec=ntlm". Obviously to use the smbcredentials file, write "sudo nano /root/.smbcredentials" and in there "username=xxx" and next line "password=xxx" and optional domain.
Thanks to all and I hope that this might save someone elses time.
Not sure if it has sth in common with apt-get upgrade and apt-get update before.
Couple things here, first off every user can have their own crontab. For example:
crontab -e # Edit crontab of current user
crontab -u root -e # Edit crontab of root user (might need sudo for this)
crontab -u www-data -e # Edit crontab of www-data user
Another thing is that if you don't use crontab -e to edit the file, and actually edit the /etc/crontab file directly (do something like vim /etc/crontab), you can actually specify the user you'd like to run the cron as:
* * * * * root mount -t cifs /path/to/folder /point/to/mount -o username=xxx,password=xxx,sec=ntlm
To run via root's crontab at reboot, type:
sudo crontab -e
And add this line:
#reboot mount -t cifs 'folderpath' 'pointtomount' -o username=xxx,password=xxx,sec=ntlm
But really, shouldn't you be adding your auto-mounts to /etc/fstab?

Is it possible that when using sudo command it should first source a file?

I need to run a bunch of scripts (with sudo) that use a single file.sh as a configuration file for all. Initially I've put the file.sh in /etc/profile.d and when I ran the scripts as root everything was ok (because when I connected to the machine it first sourced the file.sh and all vars in that file were available) but now, for security reasons, I need to run them with another user with sudo rights.
When running with sudo the "configuration file" in /etc/profile.d does not get sourced even if I'm root and do sudo - it's the same.
Using "sudo -E" is not an option, also this kind of solution "Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"" does not work for me as the vars in the file change a lot and it's easier to throw a file, with all the vars, in a location - like /etc/profile.d/ - instead to adding options to /etc/sudoers.
Later Edit (working):
Moved original sudo command to sudo.orig. Created a new sudo bash script
[root#NS1 bin]# cat sudo
#!/bin/bash
source /etc/profile.d/set_env_vmdeploy.sh
sh /usr/bin/sudo.orig "$#"
and gave it permissions
[root#NS1 bin]# chmod 4111 sudo
[root#NS1 bin]# ll sudo*
---s--x--x 1 root root 78 May 7 13:42 sudo
---s--x--x 1 root root 123832 Jul 31 2014 sudo.orig
If you want sudo to execute all the profile scripts in the child shell, you can tell it to invoke the shell as a login shell: sudo -i /usr/local/bin/my_script.sh. (Note that the child shell will start with the working directory set to /root, and also that this may have other unintended side effects.)
Alternatively, invoke bash explicitly with a command parameter: sudo /bin/bash -c "source ./config.sh; ./real_script.sh".

Resources