Cron jobs

Asked by nokangaroo

I just installed gnome-schedule, but it does not work, even though I invoked it as root. I need to run a script in my home folder, as root, to back up to my backup volume (which I do not want to make world-writable.) The script works when invoked manually with sudo, and also when I run it manually from gnome-schedule, but when left to itself nothing happens. I already uncommented the job in the crontab file but it did not help. Why is it commented out anyway? According to syslog the cron job is executed, but nothing happens. Adding sudo to the command does not work either.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu cron Edit question
Assignee:
No assignee Edit question
Solved by:
nokangaroo
Solved:
Last query:
Last reply:
Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#1

Solved - copied the script to /usr/bin, changed ownership to root:root and permissions to 550. I find that I do not need gnome-schedule; I can edit crontab directly with sudo crontab -e. But why does it work then when I invoke it manually in gnome-schedule (I did not even need sudo)? This seems like a bug to me, and a security risk.

Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#2

In case anybody is interested, this is the script:

#!/bin/bash
rsync -avxAX ~/Documents ~/Movies ~/Music ~/Pictures ~/Downloads `find /media/disk* -maxdepth 0`/`hostname`-files/

This will dump your files to any disk, disk0, disk1... that happens to be plugged in (don't plug in two of them). The paths will have to be written out in full if you use the script as root in /usr/bin.

Incidentally, the -a option in rsync causes problems when used as an alias with many exclude options, so I still use the fully-written-out command in bashrc. Like this:

alias basic-restore='rsync -rtovxHAXlgD --exclude='\''tmp/*'\'' --exclude='\''*/.thumbnails/*'\'' --exclude='\''*/Documents/*'\'' --exclude='\''*/Movies/*'\'' --exclude='\''*/Music/*'\'' --exclude='\''*/Pictures/*'\'' --exclude='\''*/Downloads/*'\'' /home/'

This works, but it will not work with rsync -avxAX (It will work in a script.) The fact that rsync needs ticks in exclude options is a serious bug that should be fixed, as it makes aliases extremely awkward to write, especially with echo and >>

Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#3

My bad - the above mentioned alias works now with rsync -avxAX, so I must have mistyped it before (no real surprise here), but the rest of the comment, especially about ticks in rsync exclusion options, is still valid.

Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#4

You should prefix the rsync command with

if ! [ -f /media/disk* [; then
echo 'no backup disk plugged in'
exit
fi

or the cron job will fill up your root partition with the backup (no harm done though, simply delete it)

so the script should be

#!/bin/bash
if ! [ -f /media/disk* ]; then
echo 'no backup disk plugged in'
exit
fi
rsync -avxAX ~/Documents ~/Movies ~/Music ~/Pictures ~/Downloads `find /media/disk* -maxdepth 0`/`hostname`-files/

If more bugs turn up I will keep posting. This is getting to be fun.

Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#5

Here's the promised bugfix:

#!/bin/bash
if [ -f $(find /media/disk* -maxdepth 0 2> /dev/null) ]; then
echo 'no backup disk plugged in'
exit
fi
rsync -avxAX ~/Documents ~/Movies ~/Music ~/Pictures ~/Downloads `find /media/disk* -maxdepth 0`/`hostname`-files/

Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#6

It seems you actually need the -H option in rsync, so I put it back in. So the command is rsync -avxHAX. I thought I was being pedantic, but I am not.

Revision history for this message
nokangaroo (nokangaroo-deactivatedaccount-deactivatedaccount) said :
#7

I might as well finish this. Here's an improved version of the backup script. If no disk is plugged in it croaks silently as before, and if there is more than one backup disk plugged in (named disk, disk0, disk1...) it will write to all of them in succession.

#!/bin/bash
TMPFILE=`mktemp`
find /media/disk* -maxdepth 0 1> $TMPFILE 2> /dev/null
j=`cat $TMPFILE`
if [ -z "$j" ]; then
rm -f $TMPFILE
exit
fi
for i in `cat $TMPFILE`; do
rsync -avxHAX /home/<user>/Documents /home/<user>/Videos /home/<user>/Music /home/<user>/Pictures /home/<user>/Downloads $i/`hostname`-files/
done
rm -f $TMPFILE

I just tried it, and it works. Put it in /bin or /usr/bin and give it a name like backup-67A4cx that is unlikely to already exist or ever be installed by synaptic, and call it as a cron job with sudo crontab -e. I need not add that this is very DANGEROUS and can wipe your system if you make a mistake. I don't recommend it unless you have a good backup that gets you back up and running quickly. The -H and -x rsync options will not be needed for this file backup, but one can do a similar script to back up one's root partition, and then they will be needed.