使用Google Storage自动备份

Standard

如果你的机器是放在国外,或是暂时不为被墙所担心,那使用Google Storage备份数据非常好。
优势很明显,存储空间大,速度快,还有最重要的稳定。
经常手动备份很麻烦,如果你的网站是运行在Linux系统上,又已有Google Storage for Developers的邀请,那么就可以写些crontab脚本来定时自动备份你要的数据。

下载并安装gsutil工具

root@ansonye:~# wget http://commondatastorage.googleapis.com/pub/gsutil.tar.gz
root@ansonye:~# tar zxvf gsutil.tar.gz
root@ansonye:~# mv gsutil /usr/local/
root@ansonye:~# vim ~/.bashrc
export PATH=${PATH}:/usr/local/gsutil
export PYTHONPATH=${PYTHONPATH}:/usr/local/gsutil
 
root@ansonye:~# source ./.bashrc 
 
#会提标输入access key和secret key
root@ansonye:~# gsutil ls  #按提示操作

编写定时备份脚本

#注:gs://anson中的anson为我所新建"Bucket",结构如后图所示.
root@ansonye:~# vi /etc/cron.weekly/backup
#!/bin/sh
mysqldump -u root -pXXXXXX --default-character-set=utf8 -B yemaosheng | sed 's/AUTO_INCREMENT=[0-9]*\b//' > /tmp/yemaosheng.sql
tar zcfP /tmp/yemaosheng_mysql_$(date +%y%m%d).tar.gz /tmp/yemaosheng.sql
rm /tmp/yemaosheng.sql
gsutil cp /tmp/yemaosheng_mysql_$(date +%y%m%d).tar.gz gs://anson/backup_yemaosheng/yemaosheng_mysql_$(date +%y%m%d).tar.gz
rm /tmp/yemaosheng_mysql_$(date +%y%m%d).tar.gz
 
tar zcfP /tmp/yemaosheng_site_$(date +%y%m%d).tar.gz /var/www/yemaosheng/
gsutil cp /tmp/yemaosheng_site_$(date +%y%m%d).tar.gz gs://anson/backup_yemaosheng/yemaosheng_site_$(date +%y%m%d).tar.gz
rm /tmp/yemaosheng_site_$(date +%y%m%d).tar.gz
 
root@ansonye:~# chmod +x /etc/cron.weekly/backup

Google Storage 管理界面


cacti备份及恢复shell

Standard

在网上找到下面两个shell,先收着.
备份Cacti
(1)cacti的文件目录的备份(rra目录除外)
(2)mysql备份
mysqldump -p cacti >/tmp/cacti.sql
(3)备份rra
dumprrd.sh

#!/bin/sh
rrddump="/usr/local/bin/rrdtool dump"
xmldir="/tmp/mysqlbackup/rraxml"
xmlbackup="/tmp/mysqlbackup/rrabak"
mysqlbackupDIR="/tmp/mysqlbackup"
rrdpath="/usr/local/share/cacti/rra/"
DATE=`date +"%y-%m-%d"`
 
if [ ! -e ${xmldir} ];then
   mkdir ${xmldir}
fi
 
if [ ! -e ${xmlbackup} ];then
  mkdir ${xmlbackup}
fi
 
#create rra => xml
for file in `find /usr/local/share/cacti/rra/ -mtime -2 | awk -F\/ '{print $NF}'`
  do
    ${rrddump} ${rrdpath}${file} > ${xmldir}/${file}.xml
  done
 
if [  `ls /tmp/mysqlbackup/rraxml/|wc -l`  -ge 2 ];then
   #tar backup
   cd ${mysqlbackupDIR}
   tar czvf ${xmldir}.${DATE}.tar.gz rraxml >/dev/null 2>&1
   mv ${xmldir}.${DATE}.tar.gz ${xmlbackup}
 
#delete file&dir
  if [ $? -eq 0 ];then
    rm -fdr ${xmldir}
    mkdir ${xmldir}
    /usr/bin/find ${mysqlbackupDIR}/rrabak -type f -ctime +2 | /usr/bin/xargs /bin/rm >/dev/null 2>&1
    echo "Rra Backup is ok!"
  fi
else
   echo "Rra Backup is fail!"
fi

恢复Cacti
(1)cacti的文件目录的备份(rra目录除外)
(2)mysql恢复
mysql cacti

#!/bin/sh
rrdrestore="/usr/local/bin/rrdtool restore"
xmldir=/tmp/rraxml/
for file in `find ${xmldir} | awk -F\/ '{print $NF}' | sed s/.xml//g`
  do
     echo $file
    ${rrdrestore} ${xmldir}${file}.xml /usr/local/share/cacti/rra/${file}
  done
chown -R cacti:cacti /usr/local/share/cacti/rra