Announcement

Collapse
No announcement yet.

Save output of command to file then send in email alert

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Save output of command to file then send in email alert

    I'm sure this is very simple.
    I run an rsync to backup my machines as a cron job weekly.
    I would quite like to be able to see the output of that in a log file and I would also like to be able to have a log file if I manually invoke the backup.
    Also when the backup is done I would like it to send me an email stating that, with the logfile attached.
    I'm not sure how to do the first part but I'm sure the email could be achieved through sendmail but I'm not sure how to add an attachment with that.
    Any help extremely appreciated.

    EDIT so far I am working with:

    bash$ rsync -avzuh user@computertobackup:~/ /mnt/backups > "/mnt/backups/backuplog-`date +"%m-%d-%y`"

    and:

    mailx -s [MySubject] -a "/mnt/backups/backuplog-`date +"%m-%d-%y`" me@myispmail.com

    but I would have to make sure the email gets send the same day as the update starts.
    Desktop: Intel i5-4690K | 16GB DDR3 | Gigabyte Z97N-WIFI | EVGA GTX 660 3GB | Windows 10
    Server 0: Gen8 HP Microserver | Proxmox Hypervisor Server 1: Gen8 HP Microserver | FreeNAS

  • #2
    adding an attachment is quite straightforward - do a man on uuencode (my scripts are at work so can confirm in the morning unless you're sorted by then).
    something like
    Code:
    uuencode <file_to_attach> <name_of_attachment> | mailx -s "subject here" you@yourdomain.com
    As to outputting the logs - does rsync write logs ? maybe with a -v option? again, man page is your friend If not, then you'll have to write some echo "blah blah" >> logfile style statements in your script

    /edit: if the logfile is just text, then you don't necessarily need to "attach" it... just
    Code:
    cat logfile.txt | mailx -s "subject" you@yourdomain.com
    --
    Fractal R4 (shhh!) 3570k, z77x-d3h, 16gb, R9-270X, 240gb SSD, 2tb mirror w/ 60gb cache drive

    Comment


    • #3
      If I manually invoke:
      bash$ rsync -avzuh user@computertobackup:~/ /mnt/backups > "/mnt/backups/backuplog-`date +"%m-%d-%y`"

      The output is sent to the log file.

      Is there any way to have this happen but also to see the output on screen?
      Desktop: Intel i5-4690K | 16GB DDR3 | Gigabyte Z97N-WIFI | EVGA GTX 660 3GB | Windows 10
      Server 0: Gen8 HP Microserver | Proxmox Hypervisor Server 1: Gen8 HP Microserver | FreeNAS

      Comment


      • #4
        only by running that command in the background with an "&" on the end, and then doing a tail -f on the logfile
        --
        Fractal R4 (shhh!) 3570k, z77x-d3h, 16gb, R9-270X, 240gb SSD, 2tb mirror w/ 60gb cache drive

        Comment


        • #5
          kk. I don't fancy doing that. I guess I'll just look at the logfile later if I need to.
          Desktop: Intel i5-4690K | 16GB DDR3 | Gigabyte Z97N-WIFI | EVGA GTX 660 3GB | Windows 10
          Server 0: Gen8 HP Microserver | Proxmox Hypervisor Server 1: Gen8 HP Microserver | FreeNAS

          Comment


          • #6
            or use 2 windows - 1 to tail the log, the other to run the command
            --
            Fractal R4 (shhh!) 3570k, z77x-d3h, 16gb, R9-270X, 240gb SSD, 2tb mirror w/ 60gb cache drive

            Comment


            • #7
              Finished:

              Code:
              #!/bin/bash
              DATE=`date +%m-%d-%y`
              echo -n "What directory do you want to back up? : "
              read DIRECTORY
              rsync -avzuh Targetmachine:~/$DIRECTORY . > /home/username/logs/backup-log-$DIRECTORY-$DATE.txt &&
              echo  "Backup of $DIRECTORY Complete, Log attached" | mailx -s "Backup  Complete" -a "/home/username/logs/backup-log-$DIRECTORY-$DATE.txt"  user@HOST
              Desktop: Intel i5-4690K | 16GB DDR3 | Gigabyte Z97N-WIFI | EVGA GTX 660 3GB | Windows 10
              Server 0: Gen8 HP Microserver | Proxmox Hypervisor Server 1: Gen8 HP Microserver | FreeNAS

              Comment


              • #8
                oooh mailx has a "-a" on linux... worth knowing

                I'm stuck predominantly on Solaris for now
                --
                Fractal R4 (shhh!) 3570k, z77x-d3h, 16gb, R9-270X, 240gb SSD, 2tb mirror w/ 60gb cache drive

                Comment

                Working...
                X