?
Solved

BASH & cron, creating a file and pushing content.

Posted on 2009-03-29
7
Medium Priority
?
622 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 1000 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 800 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 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 200 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
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 1000 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 35

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 real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

800 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