LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2012, 12:16 PM   #1
B-Rye
LQ Newbie
 
Registered: Apr 2012
Posts: 14

Rep: Reputation: Disabled
Not a strong coder.. need help with simple script


Hi guys.. not strong at script coding.. figure this may be simple

right now i login to the server every night and dumb the mysql database to a backup file. then i go in and delete the oldest one. client only wants say the last 10 or so kept.


Curious what would be required to write a quick little script that I can crontab to do this nightly without any personal involvment from me.


it would have to run the command line i have saved to dump the db.

I would need to know:

A) what to replace the filename with in my dump command

B) how to have the script delete any old files so that I only have about 10 in the folder at any time.


Any help would be greatly appreciated!
 
Old 07-11-2012, 12:20 PM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Code:
$ man logrotate
Just have your cron job execute something like (NOTE: the following is not necessarily good practice due to hardcoding the password value):

Code:
/path/to/mysqldump -u USER -p PASS DATABASE > /var/log/mysqlbackup
# See the logrotate configuration for our backups
and configure logrotate to, everyday, rotate the backups, keeping only 10.

That's what I'd do as a quick and dirty solution, anyway.

Last edited by orgcandman; 07-11-2012 at 12:21 PM.
 
Old 07-11-2012, 12:31 PM   #3
Farzan Mufti
LQ Newbie
 
Registered: Feb 2007
Location: USA/Canada
Distribution: Red Hat, CentOS, Scientific, Fedora, Ubuntu, SUSE, SLES
Posts: 14

Rep: Reputation: 6
Linux scheduling a script

From: Farzan J Mufti July 11, 2012

The way you are trying to do is to write a script that would run as a cron job.

To delete old files you can certainly use find command. To give you a more comprehensive command I would need more information, however. You can look into the man pages by issuing command
man find

The above should take care of (B).

For (A), if you need the latest file, you can run command in your script like
LATEST_FILE=$(ls -lrt | tail -1 | awk '{print $NF;}')

Now you can use this variable in your dump command.

Hope this helps.

Last edited by Farzan Mufti; 07-11-2012 at 01:10 PM.
 
Old 07-12-2012, 08:49 AM   #4
pobrika
Member
 
Registered: Jan 2008
Location: Bournemouth, UK
Distribution: Mint, #!, Fedora, Redhat, Centos
Posts: 70

Rep: Reputation: 18
You may want to check out the following mysql backup script, it possibly the best open source script available, with lots of options included.

automysqlbackup/

Last edited by pobrika; 07-12-2012 at 08:51 AM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sleeping enough? a coder for this simple application for an UBUNTU package Xeratul Programming 7 06-24-2011 11:43 PM
[SOLVED] Nobody coder hacks in again :) qwertyjjj Linux - Security 12 08-19-2009 02:33 AM
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 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