Cron job for backups

Status
Not open for further replies.

korox10m

New Member
Messages
2
Reaction score
0
Points
1
Hello everyone, and thanks x10 for your great service.
I've created a small script to be run as a cron job for automatic backup of my blog site. It does a database dump and a full copy of all contents. It also only keeps 2 backup packages at a time, it erases all previous backups to prevent disk usage quota issues.
I have setup this script to run twice a week, on Sunday and Thursday, at 00:00. It takes around 2 minutes to run as today.
I've checked the stats on cPanel while it is supposedly running, but I haven't found anything weird in the resources use.
Also, I must clarify, this is done just as a backup mechanism. I manually download the files whenever it is created (as I check the cron notification email as much as I can).
I'm wondering, can this script get too resource expensive?
Also, I'm wondering if anyone knows of a way to automatically upload the backup to a Dropbox acount of something, that way I can automate the retrieval of the backups. I've heard of www.dropboxwiki.com/dropbox-addons/bash-dropbox-uploader, but it seems it needs curl in the server's shell.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You can do something similar using PHP and curl; there is no chance you're going to get shell access on the server. Getting the OAuth token is left as an exercise for the reader (you can probably adapt the bash script; failing that, there should be something somewhere on github.) And the script run limit is 30 seconds on Free Hosting; you'd need to chunk the tasks to several crons to avoid having them disabled (and your site temporarily disabled for tripping the resource use trigger).
 

korox10m

New Member
Messages
2
Reaction score
0
Points
1
I know there's no way I can get shell access, it is not my intention at all.
However, the shell script I wrote is bash based and it works perfectly, as follows:
Code:
#!/bin/bash
echo "Automatic backup script v0.1"
echo "By Tamashii"
echo "----------------------------------------"
timestamp_now=`date +"%Y%m%d-%H%M%S"`
echo "Started ($timestamp_now)"
echo "1. Checking if a previous backup exists..."

cd /home/site

if [ -f ./last_backup ]
    then
        timestamp=`cat ./last_backup`
        echo "A previous backup has been found with timestamp $timestamp."
        echo "2. Deleting previous backup versions..."
        mv ./site-home-${timestamp}.tar.bz2 ./actual-home.tar.bz2
        mv ./site-db-${timestamp}.sql.bz2 ./actual-db.sql.bz2
        rm -f ./site-home-*.tar.bz2
        rm -f ./site-db-*.sql.bz2
        mv ./actual-home.tar.bz2 ./site-home-${timestamp}.tar.bz2
        mv ./actual-db.sql.bz2 ./site-db-${timestamp}.sql.bz2
    else
        echo "No previous backup was found."
        echo "2. No need to delete previous backups."
fi

echo "The backup files will be creating with the timestamp $timestamp_now."

echo "3. Creating home backup..."
tar -cjf ./site-home-$timestamp_now.tar.bz2 ./public_html

echo "4. Creating database backup..."

db_usr="XXXXXXXXXXXX"
db_psw="XXXXXXXXXXXX"
db_host="XXXXXXXXXXXX"
db_name="XXXXXXXXXXXX"

mysqldump --user=$db_usr --password=$db_psw --host=$db_host $db_name | bzip2 > ./site-db-$timestamp_now.sql.bz2

echo "$timestamp_now" > ./last_backup

timestamp_now=`date +"%Y%m%d-%H%M%S"`
echo "Finished ($timestamp_now)"

<EDIT 1>I also ended up using the DropPHP script to upload to Dropbox (thanks for the wink, essellar). It isn't done automatically, and it takes its sweet time to upload, but it works rather nicely. I really hope I'm not tripping any resource use triggers there! If I am, I'll revert to the way I was working before.</EDIT 1>
 
Last edited:
Status
Not open for further replies.
Top