bash - Store rsync error in variable -
i've written bash script sync backups local storage, script checks whether backup been made on day script executed, , if sync's.
i've made way if accident (or other way) backups deleted original location, synced backups on second storage won't deleted upon next sync.
#!/bin/bash files_found=`ssh user@xx.xx.xx.xx "find /home/data_folder/test* -type f -mtime -1"` rsync_to_location="/home/test_folder/"; rsync_from_location="/home/data_folder/"; if [ "$files_found" = 0 ]; echo "file not found!" send_error=`ssh user@xx.xx.xx.xx "echo 'this message body' | mail -s 'this subject' user@localhost"` else echo "files found!" rsync -arzt --ignore-existing --delete --max-delete=1 -e 'ssh' user@xx.xx.xx.xx:$rsync_from_location $rsync_to_location if [[ $? -gt 0 ]]; send_error=`ssh user@xx.xx.xx.xx "echo 'this message body' | mail -s 'this subject' earmaster@localhost"` fi fi
now question is, if rsync fails (max deletions), how can store message , send mail?
i've tried with
rsync_error="rsync -arzt --ignore-existing --delete --max-delete=1 -e 'ssh' user@xx.xx.xx.xx:$rsync_from_location $rsync_to_location"
and add $rsync_error
mail call, doesn't seem work
the line have put here store command string , not run it.
rsync_error="rsync -arzt --ignore-existing --delete --max-delete=1 -e 'ssh' user@xx.xx.xx.xx:$rsync_from_location $rsync_to_location"
to capture output need put in $( )
so.
rsync_error=$(rsync -arzt --ignore-existing --delete --max-delete=1 -e 'ssh' user@xx.xx.xx.xx:$rsync_from_location $rsync_to_location)
this capture stdout of executed command wanting stderr assume. better way of doing might pipe stderr file , handle output way.
# rsync err output file err_output="/tmp/rsync_err_$$" # when script exits, remove file trap "rm -f $err_output" exit # use 2> direct stderr output file rsync -arzt --ignore-existing --delete --max-delete=1 -e 'ssh' user@xx.xx.xx.xx:$rsync_from_location $rsync_to_location 2> "$err_output" # ever error file
Comments
Post a Comment