Sponsored Content
Top Forums Shell Programming and Scripting Compare File & Copy Replace if Successful Post 302755637 by SalientAnimal on Monday 14th of January 2013 01:35:25 AM
Old 01-14-2013
I want to check the size, as well as if the backup completed successfully before replacing the old files on the remote server with the newly created files.

This is what my scripts looks like:

Code:
#!/bin/bash
# ===================================================================== #
#  OTRS BACKUP SCRIPT WITH SYSTEM INFORMATION  #
#    SCRIPT CREATED 15/11/2012   #
#               SCRIPT LAST MODIFIED 10/01/2013    #
# ===================================================================== #

# ===================================================================== #
#    VARIABLES THAT CAN BE USED IN THE SCRIPT   #
# ===================================================================== #
# DIRECTORY WHERE FILES ARE STORED
BACKUPDIR="/app/temp"
# CREATES A TITLE VARIABLE
TITLE="OTRS System Status"
# SPECIFY THE CONTENT OF THE E-MAIL TO BE SENT
MAILCONTENT="files"
# SETS THE MAXIMUM SIZE OF THE E-MAIL 4000 = +/- 5MB
MAXATTSIZE="4000"
# E-MAIL ADDRESS WHERE THE E-MAIL WILL BE SENT
# MAILADDR="myemail@mydomain.com myemail2@mydomain.com"
MAILADDR="myemail@mydomain.com myemail3@mydomain.com"

# DEFINE THE E-MAIL BODY
SUBJECTFILE=$BACKUPDIR/sqldump.txt
# CREATE THE FILE TO ATTACH
ATTACHMENT=status.html

# ===================================================================== #
#     FUNCTIONS    #
#      STUBBING IS USED AS A TEMPORY PLACE HOLDER FOR FUTURE CODE       #
# ===================================================================== #

MYSQL_DUMP ()
{
 mysqldump systembackup > systembackup.sql --user=root --password=mypassword && mysqldump systembackup2 > systembackup2.sql --user=root --password=mypassword
}
# ---- END OF MYSQL DUMP ---- #

COMPRESSION ()
{
        tar -jcvf systembackup.tar.bz2 systembackup.sql && tar -jcvf systembackup2.tar.bz2 systembackup2.sql
}
# ---- END OF COMPRESSION ---- #

REMOVE_FILES ()
{
        rm systembackup.sql && rm systembackup2.sql && rm systembackup.tar.bz2 && rm systembackup2.tar.bz2
}
# ---- END OF COMPRESSION ---- #

DRIVE_SPACE ()
{
    echo "<h2>Filesystem Storage</h2>"
    echo "<pre>"
    df -h
    echo "</pre>"
}
# ---- END OF DRIVE SPACE ---- #

BACKUP_SIZE ()
{
    echo "<h2>Backup Size</h2>"
    echo "<pre>"
    du -h systembackup.tar.bz2 && du systembackup.tar.bz2 
    du -h systembackup2.tar.bz2 && du systembackup2.tar.bz2
    echo "</pre>"
}
# ---- END OF BACKUP SIZE ---- #

FILE_COMPARE ()
{
  echo "<pre>"
 if [ systembackup.sql -nt systembackup.sql ] && [ systembackup2.sql -nt systembackup2.sql ]; then
  echo "New file created"
 else 
  echo "No change identified"
 fi
  echo "</pre>"
}
# ---- END OF FILE COMPARE ---- #
MOVE_OFF_SERVER ()
{
  scp systembackup.tar.bz2 root@255.255.255.10:/systembackupbackup
  echo $?
  scp systembackup2.tar.bz2 root@255.255.255.10:/systembackupbackup
}
# ---- END OF FILE MOVE ---- #
 
# ===================================================================== #
#            GATHERS AND DISPLAYS THE STATUS OF THE SYSTEM              #
# ===================================================================== #
ATTACHMENT ()
{
cat <<- _status_ > $ATTACHMENT

                <html>
                <head>
                <title>$TITLE</title>
                </head>
                <body>
                <h1>$TITLE</h1>
                <p></p>
                $(DRIVE_SPACE)
                $(BACKUP_SIZE)
                $(FILE_COMPARE)
                <body>
                </html>
_status_
}
# ---- CALL THE FUNCTION TO CREATE AND ATTACH THE FILE ---- #
 
 
# ===================================================================== #
#        EXECUTING FUNCTIONS     #
# ===================================================================== #
# ---- CALL INDIVIDUAL FUNCTIONS IN CORRECT SEQUENCE ---- #
cd $BACKUPDIR
MYSQL_DUMP
COMPRESSION
MOVE_OFF_SERVER
ATTACHMENT
REMOVE_FILES

# ===================================================================== #
#      GATHERS INFORMATION AND SEND MESSAGE ATTACHMENT  #
# ===================================================================== #

 
if [ "$MAILCONTENT" = "files" ]
 then
 uuencode $ATTACHMENT $ATTACHMENT | mailx -s "System Status $DATE" -- $MAILADDR
fi

exit 0


Last edited by SalientAnimal; 01-14-2013 at 02:42 AM.. Reason: Adding Code
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare & replace contents within a file

I have 2 files file1 1 TMQUEUE QUE1 STMW633A 100 DMADM DOMGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633A STMW633A 100 GWADM GWTGRPSTMW633AA STMW633A 100 GWADM GWTGRPSTMW638A STMW638A 100 TMSYSEVT EVTGRPSTMW633A STMW633A 100 TMSYSEVT ... (2 Replies)
Discussion started by: kaustubh137
2 Replies

2. Shell Programming and Scripting

XML Copy & replace problem

I probably could have done this at one time, but, the years and no need has left my scripting skills lacking and I'm unable to work this problem out. https://www.unix.com/images/smilies/frown.gif :( Using Linux, have a great many xml files in which there may be multiple occurrence of a line of... (13 Replies)
Discussion started by: xenixuser
13 Replies

3. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

4. UNIX for Dummies Questions & Answers

How to compare 2 files & get specific value & replace it in other file.

Hiiii Friends I have 2 files with huge data. I want to compare this 2 files & if they hav same set of vales in specific rows & columns i need to get that value from one file & replace it in other. For example: I have few set data of both files here: a.dat: PDE-W 2009 12 16 5 29 11.11 ... (10 Replies)
Discussion started by: reva
10 Replies

5. Shell Programming and Scripting

replace & with &amp; xml file

Hello All I have a xml file with many sets of records like this <mytag>mydata</mytag> <tag2>data&</tag2> also same file can be like this <mytag>mydata</mytag> <tag2>data&</tag2> <tag3>data2&amp;data3</tag3> Now i can grep & and replace with &amp; for whole file but it will replace all... (4 Replies)
Discussion started by: lokaish23
4 Replies

6. Red Hat

copy & replace text

how can i copy a certain word from a text file then use this word to replace in another text file?? i tried to use something like: awk '{print "Hit the",$1,"with your",$2}' /aaa/qqqq.txt > uu.txt but i can't add an argument to point to the second file which i will replace in. please... (8 Replies)
Discussion started by: mos33
8 Replies

7. Shell Programming and Scripting

Compare & Copy Directories : Bash Script Help

Beginner/Intermediate shell; comfortable in the command line. I have been looking for a solution to a backup problem. I need to compare Directory 1 to Directory 2 and copy all modified or new files/directories from Directory 1 to Directory 3. I need the directory and file structure to be... (4 Replies)
Discussion started by: Rod
4 Replies

8. UNIX for Dummies Questions & Answers

Confirm the execution of copy command is successful or not

All, I have to copy huge file from one location to another using python . I want to make sure the execution of Copy command is successful and all the files are copied properly (there has not been any abrupt interruption to the copy process or error like no space available is encountered during... (5 Replies)
Discussion started by: IshuGupta
5 Replies

9. Shell Programming and Scripting

Compare file size and then copy/overwrite

// Redhat I have this code working, but need to add one more qualification so that I don't overwrite the files. #!/bin/sh cd /P2/log/cerner_prod/millennium/archive/ for f in * do || continue #If this isn't a regular file, skip it. && continue #If a backup already... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

10. Shell Programming and Scripting

Compare file dates before copy

Sometimes when I boot, my system goes into emergency mode. I then use Clonezilla to restore an image. Usually the image is older than the current date. This is part of a backup script that runs as a startup program. cd /home/andy/bin/ zip -u -q Ubuntu_Scripts.zip *.sh *.rb *.c *.py... (22 Replies)
Discussion started by: drew77
22 Replies
All times are GMT -4. The time now is 04:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy