Solved

BASH & cron, creating a file and pushing content.

Posted on 2009-03-29
7
620 Views
Last Modified: 2012-05-06
Hello,
I have a  a cron task which runs a bash script that daily creates backup for mysql,
i want that script to create a text file and insert it the data which was generated by bash and was supposed to send to my email, the file will be later on read by a php script which checks numerous routine checks, and sums it up into an email.

Please modify the following bash script so it dumps what's supposed to be dumped into a file named backupLog. (That file will be overwritten by bash daily because of this cron script, so if there are permissions needed please add them too)
#!/bin/bash
OUTFILE="/mnt/nas/backups/db_`date +%m_%d_%y`.gz"
MAILTO=myemail@address.com
SERVER=$(uname -n)
mysqldump -hLocalhost -uMyUser -pMyPass--opt --databases --single-transaction MyDB | gzip > $OUTFILE
MSG=$(ls -l $OUTFILE |awk '{print $5/1024/1024, "MB", $9}')
echo $MSG $(date) | mailx -s "Backups on $SERVER" $MAILTO

Open in new window

0
Comment
Question by:tzvish
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 24014426
It's unclear exactly what you want to appear in backupLog

If you just want the same output as what you get in the mail, just add the line

echo "$MSG $(date)" >/path/to/backupLog
0
 
LVL 23

Assisted Solution

by:Maciej S
Maciej S earned 200 total points
ID: 24014436
If I understand you correctly, you want to have your output send by email and additionally, you want to have it saved to backupLog file, right? If so - just add one more line at the end of your script.
If you want to work with this file with php later, replace "/path/to/" with some directory, to which your php/apache has read permissions.
echo $MSG $(date) > /path/to/backupLog

Open in new window

0
 
LVL 34

Assisted Solution

by:Duncan Roe
Duncan Roe earned 50 total points
ID: 24014494
If you want to put some output into the *system* logs, have a look at the man page for logger e.g. "man -s 1 logger" in a command window. Doing it that way will look after timestamping the message so you could do something like shown. This will log messages in category user.notice. Add logger options to your taste - see "man -s 3 syslog" for available facilities (e.g. "user") and level (e.g. "notice").
ls -l $OUTFILE |awk '{print $5/1024/1024, "MB", $9}' | logger 

Open in new window

0
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 
LVL 1

Author Comment

by:tzvish
ID: 24022430
Thanks, but

1. what happens after content has been pushed to this file and then a day later more content is pushed, does that overwrite the file?
2. To evade permission problems do i need to create the file myself and chmod it to 755 ?
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 24022489
1.  Yes, it is overwritten.  If you want to append, use >> instead of >

2.  No.  The file will be owned by whatever the user from cronjob runs as.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 24023477
If you use logger, output is continually appended. Log files can be rotated by logrotate (a standard utility on most systems). Any user can use logger and log files are owned by root. No need for abnormal permissions. You can configure to use your own file via /etc/syslog.conf
0
 
LVL 1

Author Closing Comment

by:tzvish
ID: 31564105
Great info, thanks.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

717 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