# simple backup with cron - problem with script



## deadguysleeps (Jun 11, 2009)

hi, i'm a newbie to freebsd. I have installed FreeBSD 7.2 in my VirtualBox. Right now i want cron to perform a simple backup everyday and name the folder backup_todayDate . 

Since i'm a newbie to Unix & i have no idea how to write a script, i simply follow the tutorial here:

http://www.pixel2life.com/publish/tutorials/600/bash_scripting_backup_script/page7/

Then, i edit my /etc/crontab file with these configuration


```
*/1 * * * * root /usr/home/hisyam/backupscript
```

since i want to play around with cron first, i want to set it to execute the script every 1 min at first.

This is the edited script (credit to Matthew Brunt):

```
#!/bin/sh
OUTPUT=/BACKUP/backup_$(date +%Y%m%d).tgz
BUDIR="/usr/local/www/ntah/"
echo "Starting backup of directory $BUDIR to file $OUTPUT"
tar -czf $OUTPUT $BUDIR
if [ $? == 0 ]; then
    echo "The file:"
    echo $OUTPUT
    echo "was created as a backup for:"
    echo $BUDIR
else
    echo "There was a problem creating:"
    echo $OUTPUT
    echo "as a backup for:"
    echo $BUDIR
fi
```

Unfortunately, if I execute the command:

# crontab backupscript

it outputs:


```
"backupscript":3: bad minute
crontab: errors in crontab file, can't install
```

i have also googled "bad minute error in crontab" but unfortunately, after i have skimmed through 3 pages of searches, i gave up. I think it's useless to google if the problem lies with the script itself.

any help for this newbie will be appreciated. thanks.


----------



## DutchDaemon (Jun 11, 2009)

1. If you want something to run every minute every hour every day, just use '* * * * *'. See crontab(5) for other intervals.

2. It is advisable not to mess with the system crontab in /etc, but to use the crontab of the user executing that script, e.g. [cmd=]crontab -e[/cmd] as the root user. Note that you don't have to add the user as the sixth field! Use [cmd=]crontab -l[/cmd] for listing your current crontab entries.

3. For later: make sure the script itself is executable (chmod(1)), and that all paths are full; cron has a very limited path to work with. Either use a full path for commands, or add an extended path to the top of the crontab. See the PATH variable in crontab(5).


----------



## trev (Jun 11, 2009)

deadguysleeps said:
			
		

> Unfortunately, if I execute the command:
> 
> # crontab backupscript
> 
> ...



In addition to DD's comments above, you are completely misunderstanding the command line syntax for crontab! You are in fact trying to instantiate a new crontab by feeding it a file which comprises your script, hence the error message.

I suggest you "man 1 crontab"


----------



## deadguysleeps (Jun 12, 2009)

i'm sorry everyone, it appears that the script worked even if the command:


```
# crontab backupscript
```

displayed the bad minute error. I checked my /BACKUP folder & there they are:


```
#ls
backup_20090611.tgz    backup_20090612.tgz
```

Right now, I hope you can give me some advice on the best practice to do the backup:

- where should i put the backupscript?
- i should chmod the backupscript to 755, right? or 555?
- where should i put the backup folder? is /BACKUP ok?


----------



## trev (Jun 12, 2009)

deadguysleeps said:
			
		

> i'm sorry everyone, it appears that the script worked even if the command:
> 
> 
> ```
> ...



That command is sooo wrong it's not funny. I seriously recommend you go and read the man page for crontab(1) so that you understand what you were doing wrong and avoid future disasters.


----------



## deadguysleeps (Jun 12, 2009)

i have read the man, and it says:


```
SYNOPSIS
    crontab [-u user] file
    crontab [-u user] {-l | -r | -e}
```

and etc, etc... so i have to use the command:


```
# crontab -u root -e
```

to bring up the default text editor, then i write the script and when i save & exit, it will install the script, am i right?

okay... moving on, who can answer my previous questions about some best practices for backup using cron?


----------



## trev (Jun 12, 2009)

deadguysleeps said:
			
		

> ```
> # crontab -u root -e
> ```
> 
> to bring up the default text editor, then i write the script and when i save & exit, it will install the script, am i right?



No.

The probelm with your first attempt is that you tried to install your script file as the crontab file.

The problem with your second attempt is that it simply edits the crontab file - you do not write your script _in_ the crontab file.

Is the man page really that unclear or are you having a lend of us?


----------



## DutchDaemon (Jun 12, 2009)

Crontab is just like an appointment book. You tell it to _execute_ a script or command at a specific _time_, whenever that time arrives.

So 
	
	



```
* * * * *     /some/script
```
 will _run_ /some/script every minute. It will not _install_ /some/script, nor will it _contain_ /some/script. 

It will just _launch_ /some/script at defined times.

The script _itself_ needs to be put somewhere (pick a location, /root/bin, /usr/scripts, /home/stuff, whatever), and needs to be executable with the correct permission. 

If it's a script to be executed by root only, chmod 500 is enough. If it's to be executed by anyone, chmod 555 will do.


----------



## deadguysleeps (Jun 15, 2009)

trev said:
			
		

> No.
> 
> The probelm with your first attempt is that you tried to install your script file as the crontab file.
> 
> ...



sorry, but it's unclear to me... btw, what is the meaning of "having a lend of us" ? My native tongue is not english & googling or wolfram-ing didn't return the answer.



> Crontab is just like an appointment book. You tell it to execute a script or command at a specific time, whenever that time arrives.
> 
> So
> Code:
> ...



maybe you should put the explanations on this thread in the man page?


----------



## deadguysleeps (Jun 15, 2009)

trev said:
			
		

> No.
> 
> The probelm with your first attempt is that you tried to install your script file as the crontab file.
> 
> ...



i think my confusion comes from reading the FreeBSD handbook:



> To install a freshly written user crontab, first use your favorite editor to create a file in the proper format, and then use the crontab utility. The most common usage is:
> 
> % crontab crontab-file
> 
> In this example, crontab-file is the filename of a crontab that was previously created.



but before that it says:



> Important: You must not use the procedure described here to edit/install the system crontab. Simply use your favorite editor: the cron utility will notice that the file has changed and immediately begin using the updated version. See this FAQ entry for more information.



These instructions that made my head spinning back then. That's why I asked before:



> ... so i have to use the command:
> 
> Code:
> 
> ...


----------



## jb_fvwm2 (Jun 15, 2009)

deadguysleeps said:
			
		

> sorry, but it's unclear to me... btw, what is the meaning of "having a lend of us" ? My native tongue is not english & googling or wolfram-ing didn't return the answer.
> 
> 
> 
> maybe you should put the explanations on this thread in the man page?



I too wish man pages had many more examples.  For instance
one of the audio ports has 30 or so parameters which are
explained, but few examples of what the consequences or
effects of each one is, or even of their meaning. (Expertise
is assumed.)  However, people originally writing the man
pages may have been short of time, or not well versed in
verbose English writing, etc.  I usually try to find online
guides (freebsd-specific, linux-specific ) or peruse 
books or even searches
" freebsd "edit crontab"  path "
in the web and within the freebsd-questions list.  (Maybe think
of the man pages as 1/4 of what one should read before 
attempting anything that fails initially)


----------



## vivek (Jun 15, 2009)

jb_fvwm2 said:
			
		

> I too wish man pages had many more examples.


No offense but the info provided in man pages are correct and more than sufficient to try out examples on command line. If that failed than, try

Goggling it 
Forums / mailing list


----------



## deadguysleeps (Jun 30, 2009)

Thanks guy for all your help, but i just want to tell u all one more thing...



			
				deadguysleeps said:
			
		

> where should i put the backup folder? is /BACKUP ok?



i found out that backing up my files in /BACKUP directory is a really bad idea from the start :x . It simply fills up my /dev/ad0s1a . Should just backup to my /usr/home/username in the first place. My bad.


p/s: how do i change this thread to "solved" ?


----------



## danger@ (Jun 30, 2009)

deadguysleeps said:
			
		

> Thanks guy for all your help, but i just want to tell u all one more thing...
> 
> i found out that backing up my files in /BACKUP directory is a really bad idea from the start :x . It simply fills up my /dev/ad0s1a . Should just backup to my /usr/home/username in the first place. My bad.



That's because this way /BACKUP is part of root (/) file system. However you can create /usr/backup or /var/backup, etc. depending on where you have enough space to keep your backups. The best option is to split them to a different HDD, or even uploading them off-site.



			
				deadguysleeps said:
			
		

> p/s: how do i change this thread to "solved" ?



You have to use the "Edit thread" option, however I think you are not yet allowed to do so, as you have not been promoted to "Regular" user yet -- see http://forums.freebsd.org/faq.php?faq=vb3_reading_posting#faq_vb3_editing_deleting. I have added the prefix for you now.


----------

