Solved

BASH & cron, creating a file and pushing content.

Posted on 2009-03-29
7
607 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
  • 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now