FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


how do I auto backup using cron jobs





ammonkc
I need to automate the backup of the files on my webserver and the MySQL db. I want to tar up the files and mysqldump the db and save them through the network to a fileserver or ftp server. I've been trying to setup a cron job in crontab but its not working. I can't even seem to get the cron job set up properly. This is what I did:

Code:

crontab -e
01 12 * * * mysqldump -uuname -ppass dbname > dbname.$(date +%Y%m%d%-%H%M%S).sql


I saved this in crontab but I got a mail message with this error when the cron job tried to run:

Code:

Subject: Cron <ammon@pandev02> mysqldump -uuname -ppass dbname > /home/ammon/backup/dbname.$(date +
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/ammon>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=ammon>

/bin/sh: -c: line 1: unexpected EOF while looking for matching `)'
/bin/sh: -c: line 2: syntax error: unexpected end of file


I'm kinda a newb with Cron jobs. any help would be appreciated
Thanks in advance.
LethcanAernis
wow! not a bad post in a forum like this Smile

Not a master myself on cronjobs... but can you test to use this instead:
01 12 * * * mysqldump -uuname -ppass dbname > dbname.$DATE.sql

Could be a name issue with year and months etc..
Does it work if you set a none-variable name for the backup file? I know it is not what you want but a start to find the problem.
djclue917
ammonkc wrote:
Code:
crontab -e
01 12 * * * mysqldump -uuname -ppass dbname > dbname.$(date +%Y%m%d%-%H%M%S).sql


I see... Well you've committed a common mistake but it's just ok. Anyway, you have to escape all the '%' in your command like so:

Code:
01 12 * * * mysqldump -uuname -ppass dbname > dbname.$(date +\%Y\%m\%d\%-\%H\%M\%S).sql
ammonkc
This is what I ended up doing. I just wrote a shell script and have crontabs just execute the script instead of trying to run the command in the cron. These are some scripts that I wrote that work perfectly now:

Code:

#!/bin/sh
# Backup script to backup critical pantours files
#
#

DATE=$(date +%Y%m%d-%H%M%S)
DIR="/usr/local/apache/htdocs/"
BACKUPDIR="/home/ammon/backup"

# go to backup dir
cd $BACKUPDIR

# tar the inc directory
tar -cvf backup.$DATE.tar $DIR

This one is more for incremental backups:
Code:

#!/bin/sh
# full and incremental backup script

FILE=backup                                   
DIRECTORIES="/usr/local/apache/htdocs"         # directoris to backup
BACKUPDIR=/home/ammon/backup                   
TAR=/bin/tar                                                  # name and locaction of tar

DOW=`date +%a`                             # Day of the week e.g. Mon
DM=`date +%Y%b%d`                      # Date and Month e.g. 27Sep

# Weekly full backup
if [ $DOW = "Sun" ]; then
        # Update full backup date - permanent archive
        $TAR -cvf $BACKUPDIR/$FILE-$DM.tar $DIRECTORIES

# Make incremental backup - overwrite last weeks
else
        $TAR -cvf $BACKUPDIR/$FILE-$DOW.tar $DIRECTORIES
fi


Now that I got the backup scripts working and Cron jobs scheduling them properly, the thing that I'm not sure how to do is have the script save the backup to a remote network drive or ftp/file server. I have about 5 servers at a colocation that I need to backup with this script. So I think having on NAS that all of them can backup to would be better than having a tape drive or external HD attached to each one.
Related topics
Cron Jobs
propos des cron jobs
[RESOLVED] cron jobs
Cron jobs
I don't have cron jobs enabled ?
cron jobs
Cron Jobs
Cron Jobs
Cron Jobs
Cron jobs - running a php file
Executing automated tasks without using cron jobs
cron jobs?
using cron jobs and scp to automate backups
[Urgent] Cron Jobs
Reply to topic    Frihost Forum Index -> Computers -> Operating Systems

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.