LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-19-2018, 07:00 AM   #1
Obig
LQ Newbie
 
Registered: May 2008
Posts: 21

Rep: Reputation: 0
Alerts for USB disks which aren't writable anymore


Hi all,
We're using some pretty basic bash scripts to backup some servers.
Backups are written to a USB disk.

Problem is when the USB disk isn't mounted or is hanging, we don't know until we check the backup.
As the script will just create the directory as the actual one isn't mounted anymore.

I'd like to implement alerts when a disk is not writable so we get a warning somethings up.

Also we do a check on the age of previous backups but if something goes wrong with the backup and it isn't checked in time, it will delete de parent directory too and future backups will fail as it can't find the path.

Command used to delete:

dest="/mnt/backup"

# Create archive filename.
day=$(date +%d-%m-%y)
hostname=$(hostname -s)
archive_file="$hostname-$day"

#check for files older as 7 days
echo "BACKUP LOG" >> $log
echo >> $log
echo "Start rotating files" >> $log
echo "delete files older as 7 days" >> $log
find $dest/ -mtime +7 -exec rm -rf {} \; >> $log
echo "Finished rotating files"

and is there a better way to log this?

Thanks for the input!
 
Old 09-19-2018, 09:05 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
You could issue a mount command before you start and grep the output to check for the mount in question.

Just a note that it's better if you paste code and output here within CODE tags (see https://www.linuxquestions.org/quest...og.php?b=36313).
 
Old 09-19-2018, 09:13 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
Check your script

Code:
day=$(date +%d-%m-%y)
echo "$day"
19-09-18
Code:
hostname=$(hostname -s)
echo "$hostname"
localhost
Code:
archive_file="$hostname-$day"
echo "$archive_file"
localhost-19-09-18
Code:
echo "BACKUP LOG" >> $log
bash: $log: ambiguous redirect

echo >> $log
bash: $log: ambiguous redirect
You are trying to echo to a variable that you haven't defined.

If you want a log file then make it, then you can echo to it.
Code:
touch log

echo "BACKUP LOG" >> log

cat log
BACKUP LOG
Or if you want the log stored as a variable
Code:
var1=$(echo "BACKUP LOG")
echo "$var1"
BACKUP LOG
If you want to concatenate the entry to the variable
Code:
var1+="$(echo -e "\nBACKUP LOG")"
echo "$var1"
BACKUP LOG BACKUP LOG
 
Old 09-24-2018, 07:14 AM   #4
Obig
LQ Newbie
 
Registered: May 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by teckk View Post
Check your script

Code:
day=$(date +%d-%m-%y)
echo "$day"
19-09-18
Code:
hostname=$(hostname -s)
echo "$hostname"
localhost
Code:
archive_file="$hostname-$day"
echo "$archive_file"
localhost-19-09-18
Code:
echo "BACKUP LOG" >> $log
bash: $log: ambiguous redirect

echo >> $log
bash: $log: ambiguous redirect
You are trying to echo to a variable that you haven't defined.

If you want a log file then make it, then you can echo to it.
Code:
touch log

echo "BACKUP LOG" >> log

cat log
BACKUP LOG
Or if you want the log stored as a variable
Code:
var1=$(echo "BACKUP LOG")
echo "$var1"
BACKUP LOG
If you want to concatenate the entry to the variable
Code:
var1+="$(echo -e "\nBACKUP LOG")"
echo "$var1"
BACKUP LOG BACKUP LOG
Thanks, but the log variable is defined earlier in the script. Only copied the relevant parts of the script for the questions.
Should have mentioned that.
 
Old 09-24-2018, 07:15 AM   #5
Obig
LQ Newbie
 
Registered: May 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by hydrurga View Post
You could issue a mount command before you start and grep the output to check for the mount in question.

Just a note that it's better if you paste code and output here within CODE tags (see https://www.linuxquestions.org/quest...og.php?b=36313).
Thanks for the pointer, could be an option indeed.
 
Old 09-24-2018, 08:16 AM   #6
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
See man mountpoint. The mountpoint command will tell you whether a given directory is a mountpoint or not. It's typically used in scripts for the problem you are trying to address, namely backups are going to the mountpoint directory when the drive is not mounted. Typical usage is as follows:
Code:
if mountpoint <insert full path to mountpoint>; then
   echo USB hard drive is mounted
   <insert backup script to run>
fi
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Booting from re-writable disks Completely Clueless Linux - General 8 01-12-2011 03:54 PM
Broken symlinks aren't red anymore LocoMojo Slackware 21 11-19-2007 09:24 AM
showmount -a shows directories that aren't there anymore toloughlin Linux - Server 0 07-26-2007 03:51 PM
filesystem on VG00 not writable anymore tchen@richmond.edu Linux - Enterprise 0 03-09-2007 12:52 PM
Why Gnome can't mount & umount disks anymore? DLCI Red Hat 0 11-10-2003 07:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration