Search code examples
linuxbashshellcentos5

Shell Script to make Centos Backup


I read in a tutorial that I could use the following command to make a full backup of my CENTOS system:

tar cvpzf /backups/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backups --exclude=/dev --exclude=/sys --exclude=/boot/grub --exclude=/etc/fstab --exclude=/etc/sysconfig/network-scripts/ --exclude=/etc/udev/rules.d/70-persistent-net.rules /

That works great for me in the command line. I have tested it and the recovery went well. I would like to be able for this to happen automatically each night, but when I try to do it in a bash script it gives me some error messages. I know how to make a cron job to execute a shell script, but this code is not working to make the backup:

#!/bin/bash
#Generic Server Backup With tar
SERVER=`uname -n`
echo "Starting backup for $SERVER..."
tar cvpzf /home/backups/backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backups --exclude=/dev --exclude=/sys --exclude=/boot/grub --exclude=/etc/fstab --exclude=/etc/sysconfig/network-scripts/ --exclude=/etc/udev/rules.d/70-persistent-net.rules /
echo "Done."

Anyone out there know what I'm doing wrong or how I can fix it?

The error messages:

  sh bu.sh
...rting backup for juice.myserver.com
tar: Removing leading `/' from member names
tar: /\r: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
Done.

Solution

  • It seems to be some issue related to CR (Carriage Return) at the end of the tar command.

    After the excludes there's a separate slash (/) which is obviously the path which is going to be backupped. The problem is that tar considers it as /\r and it obviously does not exist.

    Recommendation is to try to use some utility like dos2unix or using some command like this tr -d '\r' < bu.sh > newbu.sh to remove it.

    After that, it should work correctly.