Solved

Easy Bash scripting question

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 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