Archive

Posts Tagged ‘crontab’

Auto backup mysql database and use ftp command to cron transfer(bash shell)

August 27th, 2010 No comments

Use mysqldump command to dump the database sql files,then merge it with site file sources to tar named suffixed by YYYYMMDD(year-month-day).Then use ftp protocal tranfering the tarball to your remote backup server.
Here goes the script:
#!/bin/bash
#wordpress_backup.sh,chmod +x wordpress_backup.sh
YYYY=`date +%Y`
MM=`date +%m`
DD=`date +%d`
filename="$YYYY$MM$DD"
mysqldump -h localhost -u root -p123456 --default-character-set=utf8 db1>/tmp/db1_${filename}.sql
mysqldump -h localhost -u root -p123456 --default-character-set=utf8 db2>/tmp/db2_${filename}.sql
tar zcvpPf /tmp/doxer_$filename.tar.gz /var/www/virtual/doxer.org /tmp/db1_${filename}.sql db2_${filename}.sql
# to your backup server
ftp -v -n xxx.xxx.xxx.xxx <<EOF
userĀ  ftp@xxx.com password
binary
cd backup_sql #change directory on remote backup server
lcd /tmp #change directory on localhost
prompt
mput doxer_$filename.tar.gz #upload tarball
close
bye
EOF

At last,if you want to backup the files three times a week,use crond to accomplish it.Here is the usage:
crontab -e
Type in the following line at the end of it:
0 23 * * 0,2,4 /usr/local/wordpress_backup.sh
Save & quit.Use /etc/init.d/cron restart,and enjoy the convienience.