1

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 [email protected]:/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 [email protected]:/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 [email protected]:/backup/backup_Friday-Dec-16-2011_20-39-08.tar^M
Password:
3
  • 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_
    Commented Dec 17, 2011 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? Commented Dec 17, 2011 at 22:55
  • Also check whether the security key stored or not for automation. Can you use rcp instead of scp?
    – ChandanK
    Commented Jan 1, 2012 at 10:56

1 Answer 1

4

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.

1
  • 3
    You can also set PATH inside of the script or crontab, to avoid having to add paths all over the place
    – jordanm
    Commented Dec 31, 2011 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .