[SOLVED] Rsync output to logfile, what's wrong with this script?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Rsync output to logfile, what's wrong with this script?
This script to use rsync to back up my system wasn't written by me but someone on the rsync mailing list. From what I can see the script is supposed to write to a file ~/rsyncbackup.txt
Is there something missing on the line above? I see the -l option for log file is there. The entire script is below. It works fine as far as a whole system back up daily to my external USB drive.
Code:
#!/bin/sh
NOW=`date +%Y.%m.%d.%H.%M`
EXC_FILE=exclude.rules
BACKUP_DIR="/media/chris/backup2/snapshot/"
if [ ! -d ${BACKUP_DIR} ];then
echo "Backup destination directory ${BACKUP_DIR} not exist."
echo "run 'sudo mkdir ${BACKUP_DIR}' to create. "
exit 1
fi
if [ `id -u` != 0 ]; then #check user
echo "Usage: sudo sh $0"
exit 1
fi
if [ ! -f "${EXC_FILE}" ];then #search exludes file
echo "${EXC_FILE} not found.Exiting..."
exit 1
fi
rsync -aWSHPpl --delete-excluded --filter "merge ${EXC_FILE}" / "${BACKUP_DIR}" & > /home/chris/rsyncbackup.txt
RETVAL=$?
if [ ${RETVAL} -ne 0 ];then
echo "RSYNC ERROR: ${ERR} [ exit code: ${RETVAL} ]"
exit 1
fi
echo "Whole filesystem mirror in ${BACKUP_DIR} done at ${NOW}"
exit 0
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
If your purpose is to back up your system, have a look at rsnapshot. It uses rsync, but someone already invented the wheel. Sooner or later you want your backup to do that anyway.
Thank you all for the suggestions and pointing out the errors. The script now as written:
Code:
#!/bin/sh
NOW=`date +%Y.%m.%d.%H.%M`
EXC_FILE=exclude.rules
BACKUP_DIR="/media/chris/backup2/snapshot/"
if [ ! -d ${BACKUP_DIR} ];then
echo "Backup destination directory ${BACKUP_DIR} not exist."
echo "run 'sudo mkdir ${BACKUP_DIR}' to create. "
exit 1
fi
if [ `id -u` != 0 ]; then #check user
echo "Usage: sudo sh $0"
exit 1
fi
if [ ! -f "${EXC_FILE}" ];then #search exludes file
echo "${EXC_FILE} not found.Exiting..."
exit 1
fi
# rsync -aWSHPpl --delete-excluded --filter "merge ${EXC_FILE}" / "${BACKUP_DIR}" &> /home/chris/rsyncbackup.txt
rsync -aWSHpl --delete-excluded --filter "merge ${EXC_FILE}" / "${BACKUP_DIR}" > /home/chris/rsyncbackup.log 2> /home/chris/rsyncbackup.errors.log
RETVAL=$?
if [ ${RETVAL} -ne 0 ];then
echo "RSYNC ERROR: ${ERR} [ exit code: ${RETVAL} ]"
exit 1
fi
echo "Whole filesystem mirror in ${BACKUP_DIR} done at ${NOW}"
exit 0
This is my exclude.rules file
Code:
#Dont forget to add your backup mount point as exclude rule to avoid infinite loop
- /media/chris/backup/*
- /media/chris/backup2/*
- /var/backups/*
- /var/lib/named/proc/*
#Exclude non-local files
- /media/*
- /mnt/*
- /net/*
- /home/*/.gvfs
#Exclude virtual file systems
- /proc/*
- /sys/*
- /run/*
#Exclude temporary files
- /tmp/*
- /var/lock/*
- /var/run/*
- /var/tmp/*
# useless for me - udev create nodes as needed
- /dev/*
I made a run and everything worked perfectly. No errors noted to the ~/rsyncbackup.errors.log or the ~/rsyncbackup.log
I'll take a look at rsnapshot to replace what I have now.
As far as I'm concerned this thread is solved and I'll mark as such. Thanks again for all the assistance.
Good question and ever since I saw it I've been Googling for some kind of answer. Assuming it would be an I/O error. I did find mention of I/O errors in the rsysnc manpage
Code:
If the sending side detects any I/O errors, then the deletion of any files at the destination will be automatically disabled. This is to prevent temporary filesystem failures (such as NFS errors) on the sending side causing a massive deletion of files on the destination. You can override this with the --ignore-errors option.
Reading further in the manpage I found this which I should add to the command line
Code:
--timeout=TIMEOUT
This option allows you to set a maximum I/O timeout in seconds. If no data is transferred for the specified time then rsync will exit. The default is 0, which means no timeout.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
It is much worse when an external device is not detected. Then the backup is written to the mounting directory on the local drive. That is really fun to try to write you 400 GB backup to the 10GB root partition.
You can use this to check if your drive is mounted:
Code:
#!/bin/bash
mount_point='/mnt/ext_daily'
df -h | grep $mount_point > /dev/null
if [ $? -eq 0 ]
then
rsync -uav --exclude='/mnt' --exclude='/proc' --exclude='/sys' --exclude='/dev' --exclude='/data/movies' --exclude='/vmware' --delete / /mnt/ext_daily
#> /var/log/rsync_daily
echo "mount point $mount_point exists, rsync started"
else
echo "Error: mount point $mount_point does not exist, rsync operation skipped"
fi
Great, thanks very much for that. I'll modify the script tomorrow and give it a test run both ways. I forgot to ask last night. Can I integrate the commands
Code:
mount_point='/media/chris/backup2/snapshot/'
df -h | grep $mount_point > /dev/null
if [ $? -eq 0 ]
then
into the existing script? Or, are these lines that check for the existence of the destination directory and if not found exits
Code:
BACKUP_DIR="/media/chris/backup2/snapshot/"
if [ ! -d ${BACKUP_DIR} ];then
echo "Backup destination directory ${BACKUP_DIR} not exist."
echo "run 'sudo mkdir ${BACKUP_DIR}' to create. "
exit 1
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.