Solved

Easy Bash scripting question

Posted on 2004-08-31
4
250 Views
Last Modified: 2010-04-21
I'm a programmer, but not familar with Linux scripting.

I have a script that stops MySQL, zips up the database files, and then restarts MySQL.  The tar files are named mysql-YYYYMMDD.tar.gz, but I need to run the process more than once a day.  So that I would end up with.

mysql-YYYYMMDD-001.tar.gz
mysql-YYYYMMDD-002.tar.gz

I'm guessing I'm going to have check to see if file ...001 exists then ...002 in a loop, but I'm having a hard time getting it to work.
0
Comment
Question by:steve918
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
cjjclifford earned 250 total points
ID: 11944003
#!/bin/bash

tar_file_name=$(date +mysql-%Y%m%d)
i=1
while [ -f ${tar_file_name}$i ]; do
    i=$(expr $i + 1)
done

next_full_tarfile=${tar_file_name}-${i}.tar

# where blagh here are the files to be tarred...
tar cf $next_full_tarfile blagh
gzip -9 $next_full_tarfile
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11944038
appologies, the 5th line (while...) should read as follows:

while [ -f ${tar_file_name}-${i}.tar ]; do

Cheers,
C.
0
 
LVL 3

Author Comment

by:steve918
ID: 11946232
I had to change the while to include the .gz like so

while [ -f ${tar_file_name}-${i}.tar.gz ]; do

Then everything worked great.  THANKS.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11950187
d'oh, forgot that too... thanks for the points.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now