Skip to content

Instantly share code, notes, and snippets.

@Chairo
Created March 30, 2012 09:27
Show Gist options
  • Save Chairo/2250313 to your computer and use it in GitHub Desktop.
Save Chairo/2250313 to your computer and use it in GitHub Desktop.
自动备份脚本
#!/bin/bash
#Funciont: Backup website and mysql database
#Author: licess
#Website: http://lnmp.org
#IMPORTANT!!!Please Setting the following Values!
bak_dir=/home/backup
if [ -d $bak_dir ]; then
echo 'begin'
else
mkdir $bak_dir
fi
######~Set Directory you want to backup~######
Backup_Dir1=xxx.com
Backup_Dir2=xxxx.com
######~Set MySQL UserName and password~######
MYSQL_UserName=xxxx
MYSQL_PassWord=xxxx
######~Set MySQL Database you want to backup~######
Backup_Database_Name1=xxxx
Backup_Database_Name2=xxxx
######~Set FTP Information~######
FTP_HostName=xxx.xxx.xxx.xxx
FTP_UserName=xxxx
FTP_PassWord=xxxx
FTP_BackupDir=backup
#Values Setting END!
TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz
TodayDBBackup=db-$(date +"%Y%m%d").tar.gz
OldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gz
OldDBBackup=db-$(date -d -3day +"%Y%m%d").tar.gz
tar zcf $bak_dir/www-$Backup_Dir1-$(date +"%Y%m%d").tar.gz -C /home/wwwroot/ $Backup_Dir1
tar zcf $bak_dir/www-$Backup_Dir2-$(date +"%Y%m%d").tar.gz -C /home/wwwroot/ $Backup_Dir2
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name1 > $bak_dir/db-$Backup_Database_Name1-$(date +"%Y%m%d").sql
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name2 > $bak_dir/db-$Backup_Database_Name2-$(date +"%Y%m%d").sql
cd $bak_dir
tar zcf $bak_dir/$TodayDBBackup db-*-$(date +"%Y%m%d").sql
rm -f $bak_dir/db-*-$(date +"%Y%m%d").sql
rm -f $bak_dir/$OldWWWBackup
rm -f $bak_dir/$OldDBBackup
cd $bak_dir
lftp $FTP_HostName -u $FTP_UserName,$FTP_PassWord << EOF
cd $FTP_BackupDir
mrm $OldWWWBackup
mrm $OldDBBackup
mput $TodayWWWBackup
mput $TodayDBBackup
bye
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment