Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: Getting script to run in cron

  1. #11
    Join Date
    May 2008
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    Just something I thought I would point out about your script and the stdout/stderr from the cron log:

    It seems to me that your script, as it is written in this post, will execute the same rsync command twice if the directory /media/Elrond exists. I only say this because the second half of your script is outside of your conditional "if" statement that looks for the existence of /media/Elrond. Also, in the cron log, it says /media/Elrond already exists. You may want to work the echo, mkdir, mount, and rsync commands into your conditionals, or when you do get the script working it will be doing twice as much work as necessary, which means twice as much room for an error to occur. Something along the lines of:
    Code:
    #!/bin/bash
    
    if [ -a /media/Elrond ]
       then sudo rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
       else echo "Couldn't find Elrond, so am attempting to manually mount it" && sudo mkdir /media/Elrond && sudo mount -t ntfs-3g /dev/sdb1 /media/Elrond && sudo rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
    fi
    might work a bit better. Not too sure whether or not the "-a" option applies to checking the existence of directories, but you can adjust that part as necessary.

    As for dealing with the encrypted directory, have you attempted to run the script minus the "--exclude-from" and "--log-file" options? The cron log may suffice for catching any info generated by the verbose output, and the only way for "rsync" to actually read the exclude file is to have it decrypted before use. This of course is pointless unless you decrypt the entire directory so "rsync" can read the file info to apply the exclusions. It may be that you simply cannot use "--exclude-from" in this instance. Just my last two cents on the subject
    Last edited by trikster_x; November 5th, 2010 at 08:51 AM.

  2. #12
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    Quote Originally Posted by Trevor Burton View Post
    This is difficult for me, as I need the encryption as I am dealing with personal data under the Data Protection Act, yet I also need backup to ensure data security. One will have to give way.

    I'm not sure what to do now, as encryption is vital in case of burglary.
    Just move the exclude-file (and log file destination) somewhere that isn't encrypted ... only your home dir is encrypted, so for example somewhere under /etc for the exclude file, and perhaps /var/log/ for the logfile.

  3. #13
    Join Date
    Feb 2005
    Location
    Melbourne, Australia
    Beans
    13,510
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Getting script to run in cron

    Quote Originally Posted by DaithiF View Post
    Just move the exclude-file (and log file destination) somewhere that isn't encrypted ... only your home dir is encrypted, so for example somewhere under /etc for the exclude file, and perhaps /var/log/ for the logfile.
    No, do not put user files in system folders. System folders are for the system. /etc is a system folder.

    Create a new folder outside of the default system folders (use whatever name makes sense to you):

    Code:
    sudo mkdir /mytools
    10 seconds of typing can prevent future, unnecessary complications.
    Regards, David.
    Please use the Forum search and Wiki search for immediate help
    Please mark your thread as Solved when appropriate
    New to technical forums?: How To Ask Questions The Smart Way

  4. #14
    Join Date
    May 2008
    Beans
    347
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    Quote Originally Posted by DaithiF View Post
    Just move the exclude-file (and log file destination) somewhere that isn't encrypted ... only your home dir is encrypted, so for example somewhere under /etc for the exclude file, and perhaps /var/log/ for the logfile.
    Sorry if this is completely incorrect, but how does moving the exclude file address the problem of trying to read an encrypted directory? If "rsync" can't find the file with the exclude list while it is encrypted, then it won't see any of the files it is supposed to exclude.

    If you really want to avoid extraneous files in your encrypted backup, you might try setting up a single encrypted directory within your /home, and move all your sensitive data their, then removing encryption from the rest of your /home folder. Truecrypt would do this efficiently. Using seahorse you could simply remove the encryption from /home and encrypt existing files individually. It might be a bit more work accessing individual files through a program, so I don't know which would fit your situation better.

  5. #15
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Getting script to run in cron

    Quote Originally Posted by trikster_x View Post
    Sorry if this is completely incorrect, but how does moving the exclude file address the problem of trying to read an encrypted directory? If "rsync" can't find the file with the exclude list while it is encrypted, then it won't see any of the files it is supposed to exclude.
    Yes I think you're absolutely right. I wasn't sure if the filenames themselves were encrypted -- but it turns out they are.

  6. #16
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    57
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Getting script to run in cron

    I've had a thought.

    If this cron job ran from the user "trevor" (who owns the encrypted home folder )instead of "root" will it work OK?

    If so, my only problem is ensuring the external hard drive is mounted. I could do this from a script that ran as root a couple of minutes before each backup job.

    What do you think?

  7. #17
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting script to run in cron

    I don't know much about the whole encrypted home dir thing outside of it being decrypted silently once you login.

    Can't hurt to try right?
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  8. #18
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    57
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Getting script to run in cron

    Right.

    I've also discovered how to mount the external drive at startup using fstab, so I don't even need to mount it in cron. Whether the permissions will be OK if it is mounted at startup I don't know, but it does let "trevor" create new files, so I'll have a go at running the backup script from "trevor"'s cron and see what happens.

    Trevor

  9. #19
    Join Date
    Jul 2009
    Beans
    469
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Getting script to run in cron

    The script could be simplified and rewritten in the following way:
    Code:
    #/bin/sh
    
    if [ "$(id -u)" != "0" ]; then
        sudo "$0"
        exit $?
    fi
    
    [ -d /media/Elrond ] || mkdir /media/Elrond
    
    if ! mountpoint -q /media/Elrond; then
        mount -t ntfs-3g /dev/sdb1 /media/Elrond
    fi
    
    rsync -av --progress --delete --log-file=/home/trevor/backup_logs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backup_logs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
    It makes sure all conditions are met before running rsync.

    Schedule the script to run via cron with user trevor. When it runs it will re-execute itself as root. To be able to do that without having to enter a password you'll have to edit /etc/sudoers with visudo adding the following lines to allow user trevor to run the backup script as root.

    Code:
    Cmnd_Alias    BACKUP = /path/to/dailybackup
    
    trevor    ALL = NOPASSWD: BACKUP
    That may then get around your encrypted homedir and rights problem.

    Edit: Just seen your post about using fstab to mount the partition - there's no harm in checking if it's mounted or not and mounting it if required.
    Mark your thread as [SOLVED], use Thread Tools on forum page.

  10. #20
    Join Date
    Feb 2010
    Location
    Yorkshire, United Kingdom
    Beans
    57
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Getting script to run in cron

    Quote Originally Posted by btindie View Post
    The script could be simplified and rewritten in the following way: ...
    Thanks btindie.

    I've just tried mounting the backup drive in fstab, and it seems to work ok. Then I've put the backup script in trevor's crontab, but it doesn't work.

    the syslog shows
    Code:
    Nov  6 17:53:01 Thingol CRON[4977]: (trevor) CMD (/home/trevor/backuplogs/scripts/dailybackup >> 2>&1 /mylogs/diagnostic)
    but nothing appears in /mylogs/diagnostic at all, which is a bit confusing.

    The actual backup script that is in dailybackup was

    Code:
    sudo rsync -av --progress --delete --log-file=/home/trevor/backuplogs/$(date +%Y%m%d%H%M%S)_daily_rsync.log --exclude-from="/home/trevor/backuplogs/scripts/rsync-exclude" /home /media/Elrond/Backups/Thingol-Ubuntu/daily_home/
    Not sure why I didn't get any errors recorded in /mylogs/diagnostic - did I mistype something?

    I think my next step is to try btindie's suggestion above.

Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •