Crontab site backup help

Discussion in 'Site & Server Administration' started by genestoy, Jun 11, 2008.

  1. #1
    Can anyone tell me if this cpanel cron is correct for a complete site backup? It seems to work ok but something does not seem quite right and my host does not know? It excludes the tar backup when running and replaces the previous one in the backups folder when done. :)
    Thanks for any help - Gene

    cd /xxxxx/xxxxxxxx/ && tar --exclude public_html/backups/xxxxxxxx.tgz -cvzf public_html/backups/xxxxxxxx.tgz public_html && mv -f xxxxxxxx.tgz public_html/backups/

    This is entered on the "Standard" line
     
    genestoy, Jun 11, 2008 IP
  2. boltok

    boltok Active Member

    Messages:
    257
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #2
    Wait, you're creating a backup and then overwriting it? That can't be right.

    The first part looks ok, but what is the "mv -f ..." command for?

    Also, I suggest you keep backups for multiple days, say the last 5 days. If for any reason, your command fails, you would have overwritten your earlier backup with junk and then you'd be in a pickle.
     
    boltok, Jun 11, 2008 IP
  3. genestoy

    genestoy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for responding. I don't know much about the cron commands. I copied the cron exactly as it was off of my old server before I moved everything to a new server last week and just replaced the cron jobs on the new cpanel server. My sql crons are working just fine but I do get this email error with the above cron - mv: cannot stat `myfilename.tgz': No such file or directory when this complete file backup cron runs although it appears to work just fine and creates a new tgz file and overwrites the old one.

    So how would you write this cron to keep 5 days of backups and place them in the public_html/backups folder?

    Thanks
     
    genestoy, Jun 12, 2008 IP
  4. boltok

    boltok Active Member

    Messages:
    257
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    78
    #4
    I'd recommend you write a slightly more sophisticated backup script that has some checks and balances and is simple to understand and modify.

    Then run this script via cron (without additional arguments). The script can be something like this:

    #!/bin/bash
    
    # Get current date (e.g. 2008Jun14)
    current_date=`date +%Y%b%d`
    
    # Change directory to home folder
    cd /xxxxx/xxxxxxxx/
    
    # Exclude the backup directory and backup everything else into today's backup file
    tar --exclude public_html/backups/ -cvzf public_html/backups/xxxxxxxx-$current_date.tgz public_html
    
    # Delete old backup (keep last 5 backups)
    rm -f `ls -1t public_html/backups/xxxxxxxx-*.tgz | tail +6`
    Code (markup):
    You can modify this according to your setup, though do test it out to make sure nothing breaks
     
    boltok, Jun 14, 2008 IP
  5. genestoy

    genestoy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    First of all, thanks for trying to help. I have re-done the crons and they seem to be working just fine. First I moved the "backups" folder out of the public_html to the root directory then this is the cron command line in the standard cpanel mode: tar -czpf /home/username/backups/backup.tgz public_html

    the c creates the archive, the z zips it, the p keeps the folder/file permissions intact and the f creates the file (at least that is the way I understand it).
     
    genestoy, Jun 14, 2008 IP