Solved

BASH & cron, creating a file and pushing content.

Posted on 2009-03-29
7
618 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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

749 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