分类归档:备忘录

新VPS服务器设置备份备忘录

服务器Ubuntu 17.10 承载 Linode JP2

第一部分:

crontab定时执行任务列表

1、查看crontab定时执行任务列表
crontab -l
2、添加crontab定时执行任务
crontab -e

root@localhost:~# crontab -l
31 0 * * * “/usr/local/acme.sh”/acme.sh –cron –home “/usr/local/acme.sh” > /dev/null
0 1 * * * /root/lnmp1.5/tools/backup.sh

每天凌晨1点备份并上传到GDriver

 

 

第二部分:
————————————-

GDriver同步工具安装配置
https://rclone.org/install/
安装本体程序:
curl https://rclone.org/install.sh | sudo bash

首次配置:

rclone config

交互过程:

root@localhost:~# rclone config
2018/03/08 14:01:16 NOTICE: Config file “/root/.config/rclone/rclone.conf” not found – using defaults
No remotes found – make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> google
Type of storage to configure.
Choose a number from below, or type in your own value
1 / Amazon Drive
\ “amazon cloud drive”
2 / Amazon S3 (also Dreamhost, Ceph, Minio)
\ “s3”
3 / Backblaze B2
\ “b2”
4 / Box
\ “box”
5 / Cache a remote
\ “cache”
6 / Dropbox
\ “dropbox”
7 / Encrypt/Decrypt a remote
\ “crypt”
8 / FTP Connection
\ “ftp”
9 / Google Cloud Storage (this is not Google Drive)
\ “google cloud storage”
10 / Google Drive
\ “drive”
11 / Hubic
\ “hubic”
12 / Local Disk
\ “local”
13 / Microsoft Azure Blob Storage
\ “azureblob”
14 / Microsoft OneDrive
\ “onedrive”
15 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ “swift”
16 / Pcloud
\ “pcloud”
17 / QingCloud Object Storage
\ “qingstor”
18 / SSH/SFTP Connection
\ “sftp”
19 / Webdav
\ “webdav”
20 / Yandex Disk
\ “yandex”
21 / http Connection
\ “http”
Storage> 10
Google Application Client Id – leave blank normally.
client_id>
Google Application Client Secret – leave blank normally.
client_secret>
Service Account Credentials JSON file path – needed only if you want use SA instead of interactive login.
service_account_file>
Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine or Y didn’t work
y) Yes
n) No
y/n> n
If your browser doesn’t open automatically go to the following link: 一个https网址,在浏览器打开后根据提示配置
Log in and authorize rclone for access
Enter verification code> 网页一串操作后返回的一串代码粘贴进来
Configure this as a team drive?
y) Yes
n) No
y/n> n
——————–
[google]
client_id =
client_secret =
service_account_file =
token = {“access_token”:”省略一大串”}
——————–
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name Type
==== ====
google drive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q

 

 

 

第三部分:

————————————————————-

backup.sh内容:

#!/bin/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” “/usr/local/nginx/conf/vhost” “/usr/local/nginx/conf/ssl”)

######~Set MySQL Database you want to backup~######
Backup_Database=(“ftpusers” “HyElan_Carbon” “HyElan_Flarum” “HyElan_PHPBB” “HyElan_WordPress”)

######~Set MySQL UserName and password~######
MYSQL_UserName=’root’
MYSQL_PassWord=’lishuang810715love’

######~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 GDriver Backup~######
Enable_GDriver=0
# 0: enable; 1: disable
######~Set FTP Information~######
FTP_Host=’1.2.3.4′
FTP_Username=’vpser.net’
FTP_Password=’yourftppassword’
FTP_Dir=”backup”

#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”)-$(date +”%H%M%S”).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”)-$(date +”%H%M%S”).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}

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

if [ ${Enable_GDriver} = 0 ]; then
echo “Uploading backup files to GDriver…”
cd ${Backup_Home}
rclone copy /home/backup google:VSP_Backup

echo “complete.”
fi

————————————————————–