网站一键备份脚本

本脚本支持一键备份网站目录和数据库,可以通过crontab命令设置定时任务,定时备份网站目录和文件。安装Gdrive之后,还可以同步上传到google drive。

源代码:

源代码

#!/usr/bin/sh
# Author:  Jianrry
# Version: 0.1

# save your backup files into the folder /backups
BackupLocation="/backups"
# the name of your backup files
NameOfFolder=("jianrry.com")
# the date of your backup files
date=$(date +"%Y-%m-%d")
# backup your website folder:/data/wwwroot/jianrry.com
SourceOfFolder="/data/wwwroot/jianrry.com"

# if the folder /backups  doesn't exist , you will create the folder /backups
if [ ! -d $BackupLocation ]; then
mkdir -p $BackupLocation
fi

# zip the folder and files
zip -r $BackupLocation/$NameOfFolder-$date.zip $SourceOfFolder

# upload your backup files into google drive
gdrive upload $BackupLocation/$NameOfFolder-$date.zip

# remove the backup files 7 days ago
find $BackupLocation -mtime +7 -name "*.zip" -exec rm -rf {} \;


# the name of your database 
DB_NAME="****"
# the username of your database
DB_USER="****"
# the password of your database
DB_PASSWORD="****"

# export your database
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME > $BackupLocation/$DB_NAME-$date.sql

# upload your backup files into google drive
gdrive upload $BackupLocation/$DB_NAME-$date.sql

# remove the backup files 7 days ago
find $BackupLocation -mtime +7 -name "*.sql" -exec rm -rf {} \;

补充内容:

最后修改:2019 年 07 月 24 日 11 : 22 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论