Need Help with automatically Import from special mysqldump


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help with automatically Import from special mysqldump
# 1  
Old 09-16-2014
Need Help with automatically Import from special mysqldump

Hi @ all
I need a little bit help with a tricky problem ...
Here´s the situation:

We´ve 2 MySQL-Servers, one is productive, the other is Backup.

At the productive Server there runs every 2 hours a cron Job which does a Dump from MySQL-DB with script 'automysqlbackup.sh' and
copy it then via rsync to the Backup-Server,
so at the Backup there´re the following files:

Code:
 Sep 15 08:58  daily_test_2014-09-15_08h58m_Monday.sql.gz
 Sep 15 10:58 daily_ test  _2014-09-15_10h58m_Monday.sql.gz
 Sep 15 12:58 daily_ test  _2014-09-15_12h58m_Monday.sql.gz
 Sep 15 14:58 daily_ test  _2014-09-15_14h58m_Monday.sql.gz
 Sep 15 16:58 daily_ test  _2014-09-15_16h58m_Monday.sql.gz
 Sep 15 18:58 daily_ test  _2014-09-15_18h58m_Monday.sql.gz
 Sep 15 20:58 daily_ test  _2014-09-15_20h58m_Monday.sql.gz
 Sep 15 22:58 daily_ test  _2014-09-15_22h58m_Monday.sql.gz
 Sep 16 00:58 daily_ test  _2014-09-16_00h58m_Tuesday.sql.gz
 Sep 16 02:58 daily_ test  _2014-09-16_02h58m_Tuesday.sql.gz
 Sep 16 04:58 daily_ test  _2014-09-16_04h58m_Tuesday.sql.gz
 Sep 16 06:58 daily_ test  _2014-09-16_06h58m_Tuesday.sql.gz

Now the problem:
I´ve to import the newest MySQL-Dump-File ( in this case above the file
Sep 16 06:58 daily_ test _2014-09-16_06h58m_Tuesday.sql.gz )
automatically in the DB 'test';
so I think the procedure is:

1. have to look which one ist the newest file ( my problem )
2. unzip this file
3. import via mysql
4. do a mysqlcheck
5. send a mail to root that import and check is fine

The points 2-5 I would do the following way:

via cron Script 'mysqlimport.sh':
Code:
 /bin/echo  "--------------------------------------------"
/bin/date
 echo Script  "MySQL-Import"
 cd /var/mysql_backup
 gunzip (procedure to find out which  file is newest) && mysql -u root test < Sep 16 06:58  daily_test_2014_09-16_06h58m_Tuesday.sql && mysqlcheck test

the output in Logfile 'mysqlimport.log'
Code:
 root     /root/mysqlimport.sh >> /var/log/mysqlimport.log

then do a tail of the last lines from 'mysqlimport.log' via script 'mysqlimport2.sh'
Code:
tail -50 /var/log/mysqlimport.log | awk  '{ print $0 "\x0c";}'

at last send via cron to root
Code:
/root/mysqlimport2.sh | mail -r root -s "Result MySQL-Import"  root

So I need help with Point 1, because I don´t know how´re the script-commands to find out which is the newest file, because they are different every 2 hours and every day.

Perhaps somebody could help me here?
Thanks in advance.
Regards
# 2  
Old 09-16-2014
Hi,

Quick and dirty, although in your case you'll probably want field 5.

Code:
-rw-rw-r--   1 sc386dm sc386dm 49934602 Sep  9 08:55 AR092014(1).zip
-rw-rw-r--   1 sc386dm sc386dm 49712398 Sep 16 10:24 AR092014(2).zip
drwxr-xr-x.  2 sc386dm sc386dm     4096 Sep 16 10:24 .
drwx------. 46 sc386dm sc386dm     4096 Sep 16 10:24 ..

[~/Downloads] 
(10:42:55)-(sc386dm)-(684)-> ls -latr | tail -3 | head -1 | awk '{ print $9 }'
AR092014(2).zip

[~/Downloads] 
(10:43:03)-(sc386dm)-(685)->

Regards

Dave
# 3  
Old 09-16-2014
For your point 1:
Code:
ls -lrt daily_test_*.sql.gz|awk '{print $NF}'|tail -1

enclose above in backticks (`) when you use this in code.

Last edited by vbe; 09-16-2014 at 10:16 AM..
# 4  
Old 09-16-2014
Hi,
thanks for your reply ...

Code:
ls -lrt daily_test_*.sql.gz|awk '{print $NF}'|tail -1

gives the right file back, but how can I get this in command to unzip it, in my script ?

The script mysqlimport.sh looks like this:
Code:
/bin/echo  "--------------------------------------------" 
/bin/date  
echo Script  "MySQL-Import"  
cd /var/mysql_backup
gunzip (command 'get the newest file' ) && mysql -u root test < ( even the output from command 'get the newest file' ) && mysqlcheck test

Regards
# 5  
Old 09-16-2014
Hi,

Use something like;

Code:
MYFILE=`ls -lrt daily_test_*.sql.gz|awk '{print $NF}'|tail -1`
gunzip ${MYFILE}

Regards

Dave
# 6  
Old 09-16-2014
Hi,

so when I try
Code:
MYFILE='ls -lrt daily_test*|awk '{print $NF}'|tail -1'

then I´ll get error message:

If '}|tail -1' is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found }|tail -1
-bash: }|tail -1: command not found


The command
Code:
ls -lrt daily_test*|awk '{print $NF}'|tail -1

alone returns the right file, but with MYFILE in front of it, there comes the error above.
Where´s my mistake?
# 7  
Old 09-16-2014
Hi,

You are using the wrong quotes, it should be;

Code:
`` not ''

Regards

Dave
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Special characters automatically gets returned - Red Hat Enterprise Linux Server release 7.0

Dear experts, We are using Linux OS i.e Red Hat Enterprise Linux Server release 7.0 I log to the server using putty & default shell is -bash4.2$ echo $SHELL /bin/bash special character automatically gets returned in my putty. -bash-4.2$ ~ Also this special... (4 Replies)
Discussion started by: dba1981
4 Replies

2. Red Hat

Special characters automatically gets returned - Red Hat Enterprise Linux Server release 7.0

Dear experts, We are using Linux OS i.e Red Hat Enterprise Linux Server release 7.0 I log to the server using putty & default shell is Code: -bash4.2$ echo $SHELL /bin/bash special character automatically gets returned in my putty. Code: -bash-4.2$ ~ Also this... (0 Replies)
Discussion started by: dba1981
0 Replies

3. Shell Programming and Scripting

Mysqldump rotate backup

I have a very simple script that uses a cron job to take a daily backup of our orders database. echo "Dumping ORDERS database"; mysqldump -u root --password='mypassword' -h '1.1.1.1' --opt --compress ORDERS $tbl_names > /Volumes/Files_Backup_1/db_backups/orders.sql echo "Copied database to... (2 Replies)
Discussion started by: timgolding
2 Replies

4. UNIX for Dummies Questions & Answers

Mysqldump certain tables

Hi, I have to upload part of my database periodically when i make changes to product data etc. However I only want to upload certain tables. We suffer from bandwidth chock here, so i want to write a couple of separate scripts that upload parts of the database that changed. The database is large... (5 Replies)
Discussion started by: timgolding
5 Replies

5. UNIX for Dummies Questions & Answers

Problem with my crontab file, using mysqldump

My crontab file tells cron to run a certain shell script at 10:30 AM every day. The shell script backs up my database with mysqldump and then runs a sed script that does some editing of the backup file. I have programmed the shell script to write an error message to a file I have in my home... (1 Reply)
Discussion started by: Ultrix
1 Replies

6. Shell Programming and Scripting

Help needed for mysqldump command

I want to take a backup of a database and redirect the output of the whole process to a log file. I am using the below command: mysqldump -A --add-drop-table > mysql-daily-backup.sql &> /tmp/backup_log/mysql.log Is there anything wrong with the syntax? ---------- Post updated at 08:32 PM... (0 Replies)
Discussion started by: proactiveaditya
0 Replies

7. UNIX for Advanced & Expert Users

mysqldump slowing down the process?

Hi All, I have a data calculation process-a perl script running each and every hour which will do some calculations on the data stored in a mysql server. Normally it tooks around 2minutes (max) to complete. But in case if i did any actions on the linux box where the database is... (7 Replies)
Discussion started by: DILEEP410
7 Replies

8. Shell Programming and Scripting

mysqldump script without hardcode password

OS: Linux ambglx02 2.6.16.60-0.21-default #1 Tue May 6 12:41:02 UTC 2008 i686 i686 i386 GNU/Linux Shell: bash Currently I have a mysqldump script to backup my mysql database, the command is as below: /opt/novell/mysql/bin/mysqldump --add-drop-table -u root -p -h mydb > /home/john/mydb.sql ... (5 Replies)
Discussion started by: bulkbiz
5 Replies
Login or Register to Ask a Question