Solved

Easy Bash scripting question

Posted on 2004-08-31
4
248 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

20 Experts available now in Live!

Get 1:1 Help Now