LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-13-2008, 04:23 AM   #1
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Rep: Reputation: 0
Help with Homemade Backup script


Hi,

I got a website running on a host and I have SSH access to them. I wrote as best as I could a little script to dump the mysql db and compress the directory and sql file into a single file in a folder labeled by the month and day. and then created a crontab to execute it once a month. However towards the end of Jan and through almost the entire first week of Feb it seem to have run everyday but the contents of the backup were empty. But if i run the file manually it works fine.. Im hoping someone here could help me figure what I have wrong.. (I renamed everything to generic stuff)

File: WebBackup
Code:
#!/bin/bash

# Create Backup files

# Website.com
# ---
mysqldump --host=db.host.net -u user -pPassword dbName > ./Backups/Web-db.sql
tar -cvf ./Backups/www.tar ./Website.com
tar -cvf ./Backups/Website.tar ./Backups/Web-db.sql ./Backups/www.tar
rm ./Backups/Web-db.sql ./Backups/www.tar
gzip ./Backups/Website.tar

# Create Backup Folder
value1=$(date|awk '{print $2}')
value2=$(date|awk '{print $3}')
value3=$value1-$value2
mkdir ./Backups/Website/$value3

# Move file to Backup folder
mv ./Backups/Website.tar.gz ./Backups/Website/$value3
Crontab:
Code:
59 23 1-6 * 5 ./WebBackup
Im not sure if the crontab is right, Im not familar with it and did the best searching I could to find that.. I basically just want to run this file once a month.

Is there maybe some debug code that could be added to help track down future problems? Not that familar with this stuff created this by just googling various things and trial and error.

Any help you could give I would be greatful! Thanks!!

-D
 
Old 02-13-2008, 04:41 AM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I know nothing about MySQL, but a couple of things.

1. does the WebBackup script work if you run it from a terminal?
2. is the script executable?
3. crontab entries are along the lines of min hr mday mon dow, so your crontab entry should look something like

30 0 1 * * /full/path/to/script/WebBackup

to run it at 30 past midnight on the 1st of the month

Note you need the path there

Last edited by billymayday; 02-13-2008 at 04:46 AM.
 
Old 02-13-2008, 04:46 AM   #3
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
This is just a little correction for your cron job. This will execute your script on the every 1st of the month at 11:59pm

Quote:
59 23 1 * * full_path_of_your_script
Hope this helps.


::EDIT:: posted a little late.. same as above suggestion.. cheers!
 
Old 02-13-2008, 04:57 AM   #4
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
1. Yes when im logged in by SSH I can run it manually and works fine..
2. Yep its executable
3. As far as the path I think thats where I have been running into a little issue. Since this is a host provider I dont have access to any directories below mine. Would I still need to enter the full path? How would I find that since I cant see anything other than my dir?

Thanks to both of you on the crontab correction, I will try changing the first portion now and look more into what path should be set..

-D
 
Old 02-13-2008, 05:56 AM   #5
TheBeli
LQ Newbie
 
Registered: Feb 2008
Distribution: LFS
Posts: 23

Rep: Reputation: 15
Quote:
Originally Posted by dping28 View Post
1. Yes when im logged in by SSH I can run it manually and works fine..
2. Yep its executable
3. As far as the path I think thats where I have been running into a little issue. Since this is a host provider I dont have access to any directories below mine. Would I still need to enter the full path? How would I find that since I cant see anything other than my dir?

Thanks to both of you on the crontab correction, I will try changing the first portion now and look more into what path should be set..

-D

You should still be able to run it with full path.
Just get the path with `pwd` on the script folder...
Or you could try using it from your home dir ~/folder_of_script/script

Also, try running the script with the full path when you are connected with SSH, to see any possible errors.
 
Old 02-14-2008, 07:14 AM   #6
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
Thank you.. I will give that a shot and see next month
 
Old 03-04-2008, 05:12 PM   #7
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
Ok, I seem to have got it working by changing everything to the full path that shows up in pwd. Thank you for that.. I also directed the output to a log file for checking errors.

Now I got one last question.. I have 3 sites I run this on so I repeated this code 3 times and changed it with the specific values. I know in programming languages there are ways to simplify this by just supplying the different variables when calling that block of code. Not being a programmer or that Linux savvy I'm kinda lost as to what exactly I should be googling. Is this something thats doable in bash scripts? or am i venturing into the territory of needing to write an app? If it is something I can do could you tell me what specifically I am looking for? Thanks!

-D
 
Old 03-04-2008, 09:22 PM   #8
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
Looks like these four lines can be trimed down to 2 lines:
Quote:
tar -cvf ./Backups/www.tar ./Website.com
tar -cvf ./Backups/Website.tar ./Backups/Web-db.sql ./Backups/www.tar
rm ./Backups/Web-db.sql ./Backups/www.tar
gzip ./Backups/Website.tar
Like so:
Quote:
tar -zcvf ./Backups/Website.tar ./Backups/Web-db.sql ./Website.com
rm ./Backups/Web-db.sql
 
Old 03-05-2008, 05:53 AM   #9
TheBeli
LQ Newbie
 
Registered: Feb 2008
Distribution: LFS
Posts: 23

Rep: Reputation: 15
Quote:
Originally Posted by dping28 View Post
Now I got one last question.. I have 3 sites I run this on so I repeated this code 3 times and changed it with the specific values. I know in programming languages there are ways to simplify this by just supplying the different variables when calling that block of code. Not being a programmer or that Linux savvy I'm kinda lost as to what exactly I should be googling. Is this something thats doable in bash scripts? or am i venturing into the territory of needing to write an app? If it is something I can do could you tell me what specifically I am looking for? Thanks!
-D
I think, in your case, you could use environment variables in your bash scripts.
You can read DSL's Wiki for more information.
 
Old 03-05-2008, 05:59 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You don't want to use the -v option for a job running as a cron job. You aren't attached to a console. At the very least, redirect the message output to /dev/null or a file.
 
Old 03-05-2008, 07:37 PM   #11
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
jschiwal: Yeah I added >> /full/path/Backups/Backup.log 2>&1 to the end of my crontab..

TheBeli: Thanks for the link I will give that a read..

bigrigdriver: Thanks Ill give that a try as well..
 
Old 03-05-2008, 08:39 PM   #12
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
In case anyone was curious I found what I was wanting to do.. It was a function and here is what I converted my script to:

Code:
#!/bin/bash

# Create Backup Directory Value
# ---
value1=$(date|awk '{print $2}')
value2=$(date|awk '{print $3}')
value3=$value1-$value2

# Backup files function
# ---
# 1=dbHost 2=dbUser 3=dbPass 4=dbName 5=FileName 6=webDir
function WebBackup {
mysqldump --host=$1 -u $2 -p$3 $4 > $HOME/Backups/$5-db.sql
tar -zcvf $HOME/Backups/$5.tar.gz $HOME/Backups/$5-db.sql $HOME/$6
rm $HOME/Backups/$5-db.sql
mkdir $HOME/Backups/$6/$value3
mv $HOME/Backups/$5.tar.gz $HOME/Backups/$6/$value3
}

# Create Backup files
# 1=dbHost 2=dbUser 3=dbPass 4=dbName 5=FileName 6=webDir
# WebBackup $1 $2 $3 $4 $5 $6
So I just make one line following that last comments syntax for each website. Works great! Thanks all for the help!

-D
 
Old 04-18-2008, 02:27 PM   #13
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
Quick question for you guys. My host has a long dir structure in place so when I use the $HOME variable in my tar in the file I have the entire dir structure thats 8 or so dir deep. Is there any way to do the above but not have any of the dir's in the tar from the $HOME variable? if that makes any sense. Thanks!
 
Old 04-18-2008, 03:18 PM   #14
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I think tar stores the whole path no matter what you do. Not 100% on that, but I can't see anything in the man page that tells me otherwise
 
Old 04-25-2008, 02:36 PM   #15
dping28
LQ Newbie
 
Registered: Feb 2003
Location: Triangle, VA
Posts: 18

Original Poster
Rep: Reputation: 0
Anyone know of another compression program I could use that would allow me to not include the $HOME directories? cause really I would just like an archive that contains 2 folders one being the webpage folder and the backup folder that includes the sql dump file. that way I can create a restore script and not have to mess with all the dir's from the host. Thanks!

-D
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backup script rcmonroig Linux - Newbie 4 04-21-2007 12:58 AM
Homemade NAS Server phillipw Linux - Software 1 09-07-2006 09:56 PM
How To build homemade TB Server agtlewis Linux - Hardware 3 10-16-2005 12:21 AM
homemade init script not working scylla Linux - General 2 01-21-2005 07:26 PM
HomeMade TIVO JonChristmas Linux - Hardware 1 05-31-2004 10:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration