Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm using CentOS 5.6. The below code might have some typos as I needed to change real info to fake info for security reasons:

backup.sh

#!/bin/bash
set -vx
rm -v /server/temp_db.gz
rm -v /server/temp_ftp.tar
rm -v /server/temp_backup.tar
mysqldump --all-databases | gzip -9 > /server/temp_db.gz
echo "$(date +%H:%M:%S) - finished DB backup"
tar -Pcf /server/temp_ftp.tar /server/public_html/
echo "$(date +%H:%M:%S) - finished FTP backup"
tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
DATE=`date +%A-%b-%d-%Y_%H-%M-%S`
sleep 120
/myfolder/upload.sh $DATE

upload.sh

#!/usr/bin/expect -f

# connect via scp
spawn scp /backup/temp_backup.tar root@mybackup.com:/server/backup.tar
#######################
expect {
  -re ".*es.*o.*" {
    exp_send "yes\r"
    exp_continue
  }
  -re ".*sword.*" {
    exp_send "mypassword\r"
  }
}
interact

I created 2 files (above):

cd /myfolder/
chmod +x backup.sh
chmod +x upload.sh
./backup.sh

Everything works fine. The time when temp_backup.tar is created is only 2 seconds after the previous timestamp, and normally takes 10-20 seconds when the command is run manually:

rm -v /server/temp_db.gz
+ rm -v /server/temp_db.gz
removed `/server/temp_db.gz'
rm -v /server/temp_ftp.tar
+ rm -v /server/temp_ftp.tar
rm: cannot remove `/server/temp_ftp.tar': No such file or directory
rm -v /server/temp_backup.tar
+ rm -v /server/temp_backup.tar
rm: cannot remove `/server/temp_backup.tar': No such file or directory
mysqldump --all-databases | gzip -9 > /server/temp_db.gz
+ mysqldump --all-databases
+ gzip -9

echo "$(date +%H:%M:%S) - finished DB backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:50:16 - finished DB backup'
20:50:16 - finished DB backup
tar -Pcf /server/temp_ftp.tar /server/public_html/
+ tar -Pcf /server/temp_ftp.tar /server/public_html/
echo "$(date +%H:%M:%S) - finished FTP backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:50:19 - finished FTP backup'
20:50:19 - finished FTP backup
tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
+ tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
DATE=`date +%A-%b-%d-%Y_%H-%M-%S`
date +%A-%b-%d-%Y_%H-%M-%S
++ date +%A-%b-%d-%Y_%H-%M-%S
+ DATE=Friday-Dec-16-2011_20-50-24
sleep 120
+ sleep 120
/myfolder/upload.sh $DATE
+ /myfolder/upload.sh Friday-Dec-16-2011_20-50-42
spawn scp /server/temp_backup.tar root@223.123.126.134:/backup/backup_Friday-Dec-16-2011_20-50-42.tar
Password:
temp_backup.tar                              100%  516MB  23.5MB/s   00:22

Now I set up a cron task:

* */10 * * * /myfolder/backup.sh 2>&1 > /myfolder/backup.log

I checked that the cron task is run by root, same as the manual run, the owner of the files is root:root and permissions are -rwxr-xr-x on both files, but for some reason it still doesn't work. Note again the timestamp before creating backup_temp.tar and after -- only 2 seconds difference:

rm -v /server/temp_db.gz
+ rm -v /server/temp_db.gz
removed `/server/temp_db.gz'
rm -v /server/temp_ftp.tar
+ rm -v /server/temp_ftp.tar
removed `/server/temp_ftp.tar'
rm -v /server/temp_backup.tar
+ rm -v /server/temp_backup.tar
removed `/server/temp_backup.tar'
mysqldump --all-databases | gzip -9 > /server/temp_db.gz
+ mysqldump --all-databases
+ gzip -9
echo "$(date +%H:%M:%S) - finished DB backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:41:05 - finished DB backup'
20:41:05 - finished DB backup
tar -Pcf /server/temp_ftp.tar /server//public_html/
+ tar -Pcf /server/temp_ftp.tar /server/public_html/
echo "$(date +%H:%M:%S) - finished FTP backup"
date +%H:%M:%S
++ date +%H:%M:%S
+ echo '20:41:07 - finished FTP backup'
20:41:07 - finished FTP backup
tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
+ tar -Pcf /server/temp_backup.tar /server/temp_db.gz /server/temp_ftp.tar
DATE=`date +%A-%b-%d-%Y_%H-%M-%S`
date +%A-%b-%d-%Y_%H-%M-%S
++ date +%A-%b-%d-%Y_%H-%M-%S
+ DATE=Friday-Dec-16-2011_20-41-09
sleep 120
+ sleep 120
/myfolder/upload.sh $DATE
+ /myfolder/upload.sh Friday-Dec-16-2011_20-39-08
spawn scp /server/temp_backup.tar root@231.122.139.9:/backup/backup_Friday-Dec-16-2011_20-39-08.tar^M
Password:
share|improve this question
    
Just some hints, key-based auth for scp will let you get rid of the expect script; and cron should send you a mail when something goes wrong -- anything there? Also your strategy could be a little risky, when, say, someone deletes /server/public_html/ and nobody notices it before the backup is run, the empty folder will kill your backup. –  sr_ Dec 17 '11 at 10:00
1  
“it still doesn't work”: what doesn't work? Do you get error messages? Are some files missing? Does the backup lack some content that you expected? How do the outcome of the working scenario and of the non-working scenario differ? –  Gilles Dec 17 '11 at 22:55
    
Also check whether the security key stored or not for automation. Can you use rcp instead of scp? –  ckj Jan 1 '12 at 10:56
add comment

1 Answer 1

Check your paths! A common mistake when creating cron jobs is assuming that the path is the same when the cron runs as when the user runs the file. mysqldump, and expect might need to be given full paths, instead of relative ones.

share|improve this answer
1  
You can also set PATH inside of the script or crontab, to avoid having to add paths all over the place –  jordanm Dec 31 '11 at 20:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.