I need a simple script for cent OS 5-7 to do the following:<\/p>\n
I need to offload all tar.gz files from a dir to a share each server has access to and is mounted locally.<\/p>\n
webserver1<\/p>\n
/path/to/logs/<\/p>\n
Files in this folder look like this:<\/p>\n
server.log-compressed.tar.gz<\/p>\n
I need to move everything in /path/to/logs to /nas/log-save/%hostname% -P<\/p>\n
I would like the rsync to check the destination on the nas to ensure the move has completed then delete the tar.gz files it just moved to the nas from the local server.<\/p>\n
I’m not sure how to accomplish this.<\/p>\n
Thanks all!<\/p>","upvoteCount":6,"answerCount":5,"datePublished":"2016-06-23T00:40:02.000Z","author":{"@type":"Person","name":"ericbrandl","url":"https://community.spiceworks.com/u/ericbrandl"},"acceptedAnswer":{"@type":"Answer","text":"
I have used the ‘remove source’ option in rsync. After transfer, it removes the source files. So, if it hangs, or the connection drops, nothing is deleted.<\/p>\n
So, something like this:<\/p>\n
rsync --remove-source-files -avz /path/to/src/ /path/to/dest<\/strong><\/p>","upvoteCount":1,"datePublished":"2016-06-24T13:12:26.000Z","url":"https://community.spiceworks.com/t/rsync-noob/506136/4","author":{"@type":"Person","name":"jcLAMBERT","url":"https://community.spiceworks.com/u/jcLAMBERT"}},"suggestedAnswer":[{"@type":"Answer","text":"I need a simple script for cent OS 5-7 to do the following:<\/p>\n
I need to offload all tar.gz files from a dir to a share each server has access to and is mounted locally.<\/p>\n
webserver1<\/p>\n
/path/to/logs/<\/p>\n
Files in this folder look like this:<\/p>\n
server.log-compressed.tar.gz<\/p>\n
I need to move everything in /path/to/logs to /nas/log-save/%hostname% -P<\/p>\n
I would like the rsync to check the destination on the nas to ensure the move has completed then delete the tar.gz files it just moved to the nas from the local server.<\/p>\n
I’m not sure how to accomplish this.<\/p>\n
Thanks all!<\/p>","upvoteCount":6,"datePublished":"2016-06-23T00:40:02.000Z","url":"https://community.spiceworks.com/t/rsync-noob/506136/1","author":{"@type":"Person","name":"ericbrandl","url":"https://community.spiceworks.com/u/ericbrandl"}},{"@type":"Answer","text":"\n\n
<\/div>\n
ericb08132:<\/div>\n
\n…<\/p>\n
Files in this folder look like this:<\/p>\n
server.log-compressed.tar.gz<\/p>\n
I need to move everything in /path/to/logs to /nas/log-save/%hostname% -P<\/p>\n
I would like the rsync to check the destination on the nas to ensure the move has completed then delete the tar.gz files it just moved to the nas from the local server.<\/p>\n
I’m not sure how to accomplish this.<\/p>\n
Thanks all!<\/p>\n<\/blockquote>\n<\/aside>\n
I don’t know what \" -P \" means after “%hostname%”. (For that matter, I don’t know what “%hostname%” means.)<\/p>\n
Test this:<\/p>\n
rsync --archive /path/to/logs /nas/log-save/$(hostname -s) --dry-run<\/p>\n
When you’re comfortable some variation of the above will work, test it without “–dry-run” from the command line.<\/p>\n
When you’re comfortable that that works from the command line, test this:<\/p>\n
rsync --archive /path/to/logs /nas/log-save/$(hostname -s) && mv *.tar.gz /some/other/dir/<\/p>\n
&& won’t execute the move ( “mv” ) unless the rsync succeeded.<\/p>\n
If that works you can script it.<\/p>\n
I would not have the script do a removal. Instead, “mv” the files to a separate directory, then run a separate script out of cron that tests the access times of the mv’d files, and if older than however many days, remove them.<\/p>","upvoteCount":0,"datePublished":"2016-06-23T13:18:39.000Z","url":"https://community.spiceworks.com/t/rsync-noob/506136/2","author":{"@type":"Person","name":"pigdog","url":"https://community.spiceworks.com/u/pigdog"}},{"@type":"Answer","text":"
Here’s a copy of the one I have… see if this works for you:<\/p>\n
Just have an external file to use for your list of servers/directories.<\/p>\n
[email protected] \nTIME=`date +\"%y/%m/%d\"`\nTARRETVAL=-1\n\nBACKUP_HOME=/backups/backup_store\ntargetlist=/home/rsyncer/backup_tar.conf\nretention=30\n\n#Cycle through each server and back up each directory\n\n#Externalize backup target list using $targetlist file (backup_tar.conf).\nfor d in `cat ${targetlist}`; do\n SSHSRVRNAME=${d%:*}\n BKUPFOLDER=${d#*:}\n BKDIR=\"$BACKUP_HOME/$TIME/${SSHSRVRNAME}${BKUPFOLDER}\"\n if [ ! -d \"$BKDIR\" ]; then mkdir -p $BKDIR; fi\n ERRLOG=\"$BACKUP_HOME/$TIME/$SSHSRVRNAME/errlog.log\"\n touch $ERRLOG\n\n echo \"Backup Error Log file: $SSHSRVRNAME\" >> $ERRLOG\n echo \"Backup target = \" $SSHSRVRNAME:$BKUPFOLDER >> $ERRLOG\n echo \"Backup destination = \" $BKDIR >> $ERRLOG\n ssh `whoami`@$SSHSRVRNAME sudo tar cvzpPf - ${BKUPFOLDER%\\/} 1>$BKDIR/backuptar.tgz 2>>$ERRLOG\n TARRETVAL=$?\n\n email_priority=5\n if [ $TARRETVAL == 0 ]; then\n email_subject=\"${d} Backup Successful\"\n echo \"Backup successful for server $d. The return code is $TARRETVAL.\" >> $ERRLOG\n else\n email_priority=1\n if [ $TARRETVAL == 2 ]; then\n email_subject=\"Fatal Error! Backup failed for server $d\"\n echo \"Backup failed for server $d. Fatal, unrecoverable error occurred. The return code is $TARRETVAL.\" >> $ERRLOG\n elif [ $TARRETVAL == 1 ]; then\n email_subject=\"Warning! Some files changed while being archived for server $d\"\n echo \"Some files differ for server $d. The resulting archive does not contain the exact copy of the file set. The return code is $TARRETVAL.\" >> $ERRLOG\n else\n email_subject=\"Backup failed for server $d\"\n echo \"Backup failed for server $d. The return code is $TARRETVAL.\" >> $ERRLOG\n fi\n fi\n tail -10 $ERRLOG|/usr/sbin/sendEmail -q -u \"${email_subject}\" -f `hostname`@yourplace.com -s mailrelay.yourplace.com:25 -o message-header=\"X-Priority: ${email_priority}\" -t $email_rcpt\ndone\n\n# Clean-up: Delete the backups that are two months old. Keep only the last 30 days backup in the backup server.\nfor i in `find $BACKUP_HOME/ -type f -mtime +${retention} -print`; do rm -rf $i; done\nfor i in `find $BACKUP_HOME/ -type d -empty -print`; do rmdir $i; done\n\nexit 0\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2016-06-23T13:48:25.000Z","url":"https://community.spiceworks.com/t/rsync-noob/506136/3","author":{"@type":"Person","name":"Bud-G","url":"https://community.spiceworks.com/u/Bud-G"}},{"@type":"Answer","text":"#!/bin/sh\ncurentdatea=$(date +%d_%m_%y-%R)\n\ndate\n\nmkdir /filestore/log-save/$HOSTNAME -p\n\nrsync --remove-source-files -bav --suffix='$currentdatea' --include '*.tar.gz' --exclude '*' /jboss/server/default/log/ /filestore/log-save/$HOSTNAME\n<\/code><\/pre>\nThis is what I did.<\/p>","upvoteCount":0,"datePublished":"2016-06-24T19:12:01.000Z","url":"https://community.spiceworks.com/t/rsync-noob/506136/5","author":{"@type":"Person","name":"ericbrandl","url":"https://community.spiceworks.com/u/ericbrandl"}}]}}
I need a simple script for cent OS 5-7 to do the following:
I need to offload all tar.gz files from a dir to a share each server has access to and is mounted locally.
webserver1
/path/to/logs/
Files in this folder look like this:
server.log-compressed.tar.gz
I need to move everything in /path/to/logs to /nas/log-save/%hostname% -P
I would like the rsync to check the destination on the nas to ensure the move has completed then delete the tar.gz files it just moved to the nas from the local server.
I’m not sure how to accomplish this.
Thanks all!
6 Spice ups
pigdog
(pigdog)
June 23, 2016, 1:18pm
2
ericb08132:
…
Files in this folder look like this:
server.log-compressed.tar.gz
I need to move everything in /path/to/logs to /nas/log-save/%hostname% -P
I would like the rsync to check the destination on the nas to ensure the move has completed then delete the tar.gz files it just moved to the nas from the local server.
I’m not sure how to accomplish this.
Thanks all!
I don’t know what " -P " means after “%hostname%”. (For that matter, I don’t know what “%hostname%” means.)
Test this:
rsync --archive /path/to/logs /nas/log-save/$(hostname -s) --dry-run
When you’re comfortable some variation of the above will work, test it without “–dry-run” from the command line.
When you’re comfortable that that works from the command line, test this:
rsync --archive /path/to/logs /nas/log-save/$(hostname -s) && mv *.tar.gz /some/other/dir/
&& won’t execute the move ( “mv” ) unless the rsync succeeded.
If that works you can script it.
I would not have the script do a removal. Instead, “mv” the files to a separate directory, then run a separate script out of cron that tests the access times of the mv’d files, and if older than however many days, remove them.
Bud-G
(Bud G.)
June 23, 2016, 1:48pm
3
Here’s a copy of the one I have… see if this works for you:
Just have an external file to use for your list of servers/directories.
email_rcpt=email@yourplace.com
TIME=`date +"%y/%m/%d"`
TARRETVAL=-1
BACKUP_HOME=/backups/backup_store
targetlist=/home/rsyncer/backup_tar.conf
retention=30
#Cycle through each server and back up each directory
#Externalize backup target list using $targetlist file (backup_tar.conf).
for d in `cat ${targetlist}`; do
SSHSRVRNAME=${d%:*}
BKUPFOLDER=${d#*:}
BKDIR="$BACKUP_HOME/$TIME/${SSHSRVRNAME}${BKUPFOLDER}"
if [ ! -d "$BKDIR" ]; then mkdir -p $BKDIR; fi
ERRLOG="$BACKUP_HOME/$TIME/$SSHSRVRNAME/errlog.log"
touch $ERRLOG
echo "Backup Error Log file: $SSHSRVRNAME" >> $ERRLOG
echo "Backup target = " $SSHSRVRNAME:$BKUPFOLDER >> $ERRLOG
echo "Backup destination = " $BKDIR >> $ERRLOG
ssh `whoami`@$SSHSRVRNAME sudo tar cvzpPf - ${BKUPFOLDER%\/} 1>$BKDIR/backuptar.tgz 2>>$ERRLOG
TARRETVAL=$?
email_priority=5
if [ $TARRETVAL == 0 ]; then
email_subject="${d} Backup Successful"
echo "Backup successful for server $d. The return code is $TARRETVAL." >> $ERRLOG
else
email_priority=1
if [ $TARRETVAL == 2 ]; then
email_subject="Fatal Error! Backup failed for server $d"
echo "Backup failed for server $d. Fatal, unrecoverable error occurred. The return code is $TARRETVAL." >> $ERRLOG
elif [ $TARRETVAL == 1 ]; then
email_subject="Warning! Some files changed while being archived for server $d"
echo "Some files differ for server $d. The resulting archive does not contain the exact copy of the file set. The return code is $TARRETVAL." >> $ERRLOG
else
email_subject="Backup failed for server $d"
echo "Backup failed for server $d. The return code is $TARRETVAL." >> $ERRLOG
fi
fi
tail -10 $ERRLOG|/usr/sbin/sendEmail -q -u "${email_subject}" -f `hostname`@yourplace.com -s mailrelay.yourplace.com:25 -o message-header="X-Priority: ${email_priority}" -t $email_rcpt
done
# Clean-up: Delete the backups that are two months old. Keep only the last 30 days backup in the backup server.
for i in `find $BACKUP_HOME/ -type f -mtime +${retention} -print`; do rm -rf $i; done
for i in `find $BACKUP_HOME/ -type d -empty -print`; do rmdir $i; done
exit 0
jcLAMBERT
(jcLAMBERT)
June 24, 2016, 1:12pm
4
I have used the ‘remove source’ option in rsync. After transfer, it removes the source files. So, if it hangs, or the connection drops, nothing is deleted.
So, something like this:
rsync --remove-source-files -avz /path/to/src/ /path/to/dest
1 Spice up
#!/bin/sh
curentdatea=$(date +%d_%m_%y-%R)
date
mkdir /filestore/log-save/$HOSTNAME -p
rsync --remove-source-files -bav --suffix='$currentdatea' --include '*.tar.gz' --exclude '*' /jboss/server/default/log/ /filestore/log-save/$HOSTNAME
This is what I did.