LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using Cron (https://www.linuxquestions.org/questions/linux-newbie-8/using-cron-422765/)

mclard 03-08-2006 04:44 AM

Using Cron
 
Is there a way using cron i could backup the /var/named folder and /etc/named.conf file to a tar file but have it create a daily backup ie. named.monday.tar which would be overwritten the following week.

Sorry if its a bit vague..

kevkim55 03-08-2006 05:44 AM

I think this should be straight forward, yeh ? Write a shell script to do exactly what you want to do and insert an appropriate line into crontab file and cron should execute the shell script at the time you've configured it to. Am I sounding stupid here ?&*^*&^*&

muha 03-08-2006 05:57 AM

to create a cronjob, type crontab -e and set it to run at the appropriate time:
* 6 * * * $HOME/script.sh >> $HOME/script.out 2>&1
This should run the script every minute for the 6 AM hour of everyday. The fields are minute, hour, day, month, day of week. See man crontab. The 2>&1 should output both errors and STDOUT.

http://wiki.linuxquestions.org/wiki/Crontab
http://bookmarks.linuxquestions.org/linux?like=cron

timmeke 03-08-2006 06:33 AM

To retrieve the name of the day, use something like:
Code:

today=`date +%A`
(days like "Sunday", "Monday", etc).
or
Code:

today=`date +%a`
(for days like "Sun", "Mon", etc)

Alternatively, you could create backups with full dates in the filenames. This allows you to store
more backups that just one week's worth.
For that, you can use:
Code:

today=`date +%Y%m%d`
In your shell script, you first put that command and then the tar command, using ${today} where you want to put the date in the name of the tar-file.

mclard 03-08-2006 07:04 AM

Thanks for the quick responses...

_KDF 03-08-2006 07:16 AM

I hate shell, so I wrote this quickly using php and run it via cron

Its not perfect and it doesnt do much error chekcing yet but I only wrote it 10 minutes ago ;) so any improvements/critcism welcome !

Code:

#/usr/bin/php
# Simple System backup script - cmcewan 08032006
<?
$backup_dirs = array('etc', 'home', 'root', 'var');  // root directories to be backed up
$backup_target = "/path/to/target";
$host_to_ping = "1.2.3.4" ; // Networked storage server IP


$ping_check = shell_exec("ping -c2 -w2 $host_to_ping") ;
if(strpos($ping_check, '0 received')) {
  echo "Server is offline, stopping backup procedure.....\n";
  echo "Sending alert to sysadmin";
  exit;

# Still to code Mail template, script just exits for just now.

}
else {
# Start the Backup
 shell_exec("rm -rf *.tar ".$backup_target);  // delete old backups

 echo "******* NETWORK STORAGE OK ********\n";
 echo "******* PHP BACKUP STARTED ********\n";

$a = '0'; $b = count($backup_dirs);
$now = date("dmY");

while ($a <= $b) {
echo "Archiving /" . $backup_dirs[$a] . "\n";
shell_exec("tar -cf ". $backup_target . $now."-".$backup_dirs[$a].".tar /" . $backup_dirs[$a]);
  $a++;
}

echo "";
echo "*******  PHP BACKUP ENDED  ********\n";

}
?>



All times are GMT -5. The time now is 10:37 AM.