Linux lnmp安装coscmd备份网站数据到cos

下载与安装coscmd

如果系统没有pip,pip安装参考:https://www.blyoo.com/5237.html

通过 pip 安装

执行pip命令进行安装:

pip install coscmd

安装成功之后,用户可以通过-v或者–version命令查看当前的版本信息。

pip 更新

安装完成后,执行以下命令进行更新:

pip install coscmd -U

配置

在电脑上创建一个.cos.conf

或者直接在linux里创建root目录里创建.cos.conf也行,但是还得通过vim编辑,有点麻烦了,

然后把conf文件上传到服务器,root目录里,

内容如下,修改成自己的,

secret_id:密钥中的SecretId
secret_key:密钥中的SecretKey
bucket:存储桶名称
region:存储桶所在地域

[common]
secret_id = AKIDA6wUmImTMzvXZNbGLCgtusZ2E8mG**** ##
secret_key = TghWBCyf5LIyTcXCoBdw1oRpytWk****
bucket = configure-bucket-1250000000
region = ap-chengdu
max_thread = 5
part_size = 1
retry = 5
timeout = 60
schema = https
verify = md5
anonymous = False

或者

直接执行,自动在root目录生成conf文件

coscmd config -a AChT4ThiXAbpBDEFGhT4ThiXAbp**** -s WE54wreefvds3462refgwewe**** -b configure-bucket-1250000000 -r ap-chengdu

测试上传

上传文件命令格式

请将 “<>” 中的参数替换为您需要上传的本地文件路径(localpath),以及 COS 上存储的路径(cospath)。

coscmd upload <localpath> <cospath>

例如

上传本机root目录里1.jpg文件到cos根目录

coscmd upload /root/1.jpg /

上传本机root目录里1.jpg文件到coshello目录

coscmd upload /root/1.jpg hello/

备份

我是直接修改的lnmp自带的backup.sh备份文件,

例如创建一个文件命名为backup_cos.sh

然后把文件上传root目录,

在给backup_cos.sh文件权限,

chmod -R 755 /root/backup_cos.sh

测试备份上传

/root/backup_cos.sh

备份脚本如下

注意

centos可以直接coscmd,

debian系统需要写绝对路径,/usr/local/bin/coscmd

其他系统不知,没用过,

#!/usr/bin/env bash

#Funciont: Backup website and mysql database
#Author: licess
#Website: https://lnmp.org

#IMPORTANT!!!Please Setting the following Values!

Backup_Home="/home/backup/"
MySQL_Dump="/usr/local/mysql/bin/mysqldump"
######~Set Directory you want to backup~######
Backup_Dir=("/home/wwwroot/vpser.net" "/home/wwwroot/blyoo.com")

######~Set MySQL Database you want to backup~######
Backup_Database=("lnmp" "hello")

######~Set MySQL UserName and password~######
MYSQL_UserName='root'
MYSQL_PassWord='yourrootpassword'

######~Enable Ftp Backup~######
Enable_FTP=1
# 0: enable; 1: disable
######~Set FTP Information~######
FTP_Host='1.2.3.4'
FTP_Username='vpser.net'
FTP_Password='yourftppassword'
FTP_Dir="backup"

######~Enable Cos Backup~######
Enable_COS=0
# 0: enable; 1: disable
Cos_Dir="/"
#Values Setting END!

TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz
TodayDBBackup=db-*-$(date +"%Y%m%d").sql
OldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gz
OldDBBackup=db-*-$(date -d -3day +"%Y%m%d").sql

Backup_Dir()
{
    Backup_Path=$1
    Dir_Name=`echo ${Backup_Path##*/}`
    Pre_Dir=`echo ${Backup_Path}|sed 's/'${Dir_Name}'//g'`
    tar zcf ${Backup_Home}www-${Dir_Name}-$(date +"%Y%m%d").tar.gz -C ${Pre_Dir} ${Dir_Name}
	
}
Backup_Sql()
{
    ${MySQL_Dump} -u$MYSQL_UserName -p$MYSQL_PassWord $1 > ${Backup_Home}db-$1-$(date +"%Y%m%d").sql
}

if [ ! -f ${MySQL_Dump} ]; then  
    echo "mysqldump command not found.please check your setting."
    exit 1
fi

if [ ! -d ${Backup_Home} ]; then  
    mkdir -p ${Backup_Home}
fi

if [ ${Enable_FTP} = 0 ]; then
    type lftp >/dev/null 2>&1 || { echo >&2 "lftp command not found. Install: centos:yum install lftp,debian/ubuntu:apt-get install lftp."; }
fi

echo "Backup website files..."
for dd in ${Backup_Dir[@]};do
    Backup_Dir ${dd}
done

echo "Backup Databases..."
for db in ${Backup_Database[@]};do
    Backup_Sql ${db}
done

echo "Delete old backup files..."
rm -f ${Backup_Home}${OldWWWBackup}
rm -f ${Backup_Home}${OldDBBackup}


#备份到cos
Backup_Dir_to_cos()
{
    Backup_Path=$1
    Dir_Name=`echo ${Backup_Path##*/}`
    Pre_Dir=`echo ${Backup_Path}|sed 's/'${Dir_Name}'//g'`
	
	echo 'Delete old backup files...'
	
	echo 'Delete www-'${Dir_Name}'-'$(date -d -3day +"%Y%m%d")'.tar.gz'
	
	coscmd delete -f ${Cos_Dir}www-${Dir_Name}-$(date -d -3day +"%Y%m%d").tar.gz

	echo "Uploading backup files to COS..."
	
	echo "Backup website files...to COS..."
	
    echo 'Uploading '${Backup_Home}'www-'${Dir_Name}'-'$(date +"%Y%m%d")'.tar.gz'
	
	coscmd upload ${Backup_Home}www-${Dir_Name}-$(date +"%Y%m%d").tar.gz ${Cos_Dir}
		
}

#备份到cos
Backup_Sql_to_cos()
{
    echo 'Delete old backup files...'
	
	echo 'Delete db-'$1'-'$(date -d -3day +"%Y%m%d")'.sql'
	
	coscmd delete -f ${Cos_Dir}db-$1-$(date -d -3day +"%Y%m%d").sql
	
	echo "Backup Databases...to COS..."
	
    echo 'Backup db-'$1'-'$(date +"%Y%m%d")'.sql'
	
	coscmd upload ${Backup_Home}db-$1-$(date +"%Y%m%d").sql ${Cos_Dir}
		
}

if [ ${Enable_FTP} = 0 ]; then
    echo "Uploading backup files to ftp..."
    cd ${Backup_Home}
    lftp ${FTP_Host} -u ${FTP_Username},${FTP_Password} << EOF
cd ${FTP_Dir}
mrm ${OldWWWBackup}
mrm ${OldDBBackup}
mput ${TodayWWWBackup}
mput ${TodayDBBackup}
bye
EOF

echo "complete."
fi

#备份到cos
if [ ${Enable_COS} = 0 ]; then
    echo "Uploading backup files to COS..."
	
	echo "Backup website files...to COS..."
	for dd in ${Backup_Dir[@]};do
		Backup_Dir_to_cos ${dd}
	done

	echo "Backup Databases...to COS..."
	for db in ${Backup_Database[@]};do
		Backup_Sql_to_cos ${db}
	done

echo "complete."
fi

如果测试备份上传没问题,来到下一步,

设置Crontab定时执行

crontab操作参考:http://www.vpser.net/manage/crontab.html

添加crontab定时执行任务

crontab -e

如果是vi编辑器,首先按i键,在文件末尾按crontab命令格式输入,再按ESC键,再输入:wq 回车即可。

设置每天凌晨1点钟备份开始,

0 1 * * * /root/backup_cos.sh

参考

https://cloud.tencent.com/document/product/436/10976#.E4.B8.8A.E4.BC.A0.E6.96.87.E4.BB.B6

https://www.naraku.cn/posts/92.html

http://www.vpser.net/manage/crontab.html