#!/bin/bash # #Program: This script is executed at a known time to create a database ' s backup and upload it to another host. #Info of database: #Name: Ninthday_sql username:ninthday_sql password:ninthday_passwd # #Info of Backup Host: #FTPName: Ninthday_ftp #FTPPasswd: ninthday_passwd #Backup dir:backups # #Contact: Http://www.111cn.net ## Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin Export PATH # defines the database information, where name is the database name, username is the database user name, password is the password # Name=ninthday_sql Username=ninthday_sql password=ninthday_passwd # define local backup path # Localdir=/home/chroot/home/ninthday # define temporary name for local backup # lbackname=backup-blog-$ (date +%y%m%d). sql # define the name of the backup local three days ago # loldbackup=backup-blog-$ (date-d -3day +%y%m%d). sql.tar.gz # define remote Backup name # rbackname=backup-blog-$ (date +%y%m%d). sql.tar.gz # define remote 5 days ago Backup name # roldbackup=backup-blog-$ (date-d -5day +%y%m%d). sql.tar.gz # define remote FTP information # ftphost=216.18.217.23 Ftpname=ninthday_ftp ftppasswd=ninthday_passwd # define User # User=ninthday # define addresses for sending and receiving notification messages (must be in different domains) # Sender=root@ninthday.net Receiver=kailash@126.com # Start creating local Files # Mysqldump-u$username-p$password $Name > $Localdir/backups/$LBackname # Compress to Local # Tar zpcf $Localdir/backups/$RBackname $Localdir/backups/$LBackname # Uploading Data # Lftp $FTPHost-u $FTPName, $FTPPasswd << EOF CD backups Mput $Localdir/backups/$RBackname MRM $ROldbackup Bye Eof # Delete local redundant and expired data # Rm-f $Localdir/backups/$LBackname If [-e $Localdir/backups/$LOldbackup]; Then Rm-f $Localdir/backups/$LOldbackup Fi # Write log File # echo "A datebase ' s backup has already created and uploaded to the host on $ (date +%f)." >> $Localdir/LOGS/DBBACKUP.L og # Change Permissions # Chown $user: $user $Localdir/backups/$RBackname # Send notification message # Sendmail-t <<eof From:host < $sender > To: $receiver Subject:database succeeded in creating backup. ---------------------------------------------------------- The VPS has succeeded in creating and uploading database ' s backup. It holds 5 database ' s backups created on the latest 5 sequential days On remote host and 3 database ' s backups on localhost (/backups/). Please download the backup files to your computer in a regular time schedule. Don ' t reply this email! Contact:harid@ninthday.net Http://www.111cn.net ---------------------------------------------------------- Eof Exit 0 |