Cron command for Full Website Backup?

Status
Not open for further replies.
1 comment
First try this:

Code:
0	0	*	*	0 	mysqldump u database_user pDatabase-Password database_name > database_name-`date +%Y-%m-%d`.sql

If the above command isn't working then make this script.

The script is working in a cron job which uploads backup file at every 6 hours, then it will overwrite older backup file. If anyone wants to have backup every hour, you have to make changes accordingly in crontab file.

Script name is: daybackup.script.sh

Code:
#!/bin/sh
# Put FTP server details here
SERVER="Remote FTP server"
USERNAME="FTP user"
PASSWORD="FTP password"

# local directory containing source backup file
SOURCEFILES="/home/mybackup"

# remote server directory path in which backup needs to be put in
BACKUPDIRCTORY="/rsystem/backupdir/"

# login to remote server
ftp -n -i $SERVER <<EOF
user $USERNAME $PASSWORD
cd $ BACKUPDIRCTORY

mput $ SOURCEFILES/*.tar.gz
quit
EOF

Make another file using same code given above and give a name to that file as: nightbackup.script.sh as it will put backup to remote system at midnight.
Both the script should have executable permissions, like:

Code:
$ chmod +x daybackup.script.sh
$ chmod +x nightbackup.script.sh

Now we have to setup a cron job to run above script at regular intervals.

You need to edit your crontab file. Enter the following command:

$ crontab -e

Following lines should append to available code as there may be a chance to have some another job running in the crontab file.

0 12 * * * daybackup.script.sh >>/tmp/backup.log 2>&1
0 0 * * * nightbackup.script.sh >>/tmp/backup.log 2>&1


Save and close the file.

To store the output of cron commands, we have used /tmp/backup.log file.

Please use crontab l command to confirm crontab job uses above code or not.

Note: In the above script /home/mybackup folder contains tar.gz files. One has to write a separate script to generate those source backup file.

If the above script isn't working for you then try this script.

Code:
#! /usr/bin/env python2

import os
import sys
import time

database = sys.argv[1]
location = sys.argv[2]

location = location.replace("DAY", time.strftime("%d"))
location = location.replace("MONTH", time.strftime("%m"))
location = location.replace("YEAR", time.strftime("%y"))
location = location.replace("HOUR", time.strftime("%H"))
location = location.replace("MIN", time.strftime("%M"))
location = location.replace("SEC", time.strftime("%S"))

command = "mysqldump "+database+" > "+location+".sql"
print command
os.system(command)
command = "tar -czvf "+location+" "+location+".sql"
print command
os.system(command)
command = "rm -f "+location+".sql"
print command
os.system(command)

crontab

Code:
0 4 * * * path to above file dbtoback /home/dbtobackbackup_DAY-MONTH-YEAR.tar.gz
 
Status
Not open for further replies.
Back
Top