VPS主机一键备份脚本backup.sh-自动备份到OneDrive/Dropbox等各大网盘

发布于 2020-11-26  588 次阅读


原文地址

VPS 主机一键备份脚本 backup.sh 其实在 Linux VPS 主机自动备份网站和数据库脚本通用版就已经介绍过了,不过作者最近对脚本进行了更新,将 Rclone 整合到了 backup.sh 当中,这样我们就可以利用 backup.sh 自动定时备份网站目录、数据库到各大网盘当中了。

其实,如果你用的是 OneinstackLNMP 一键包或者宝塔面板,都已经自带了备份脚本或者备份功能了,backup.sh 这个脚本有一个好处就是:通用。它是一个独立的脚本,无论你的网站运行的是什么面板,backup.sh 都可以帮助你备份指定的目录和数据库。

backup.sh 支持的网盘和存储有:阿里云 OSS、Amazon S3、Backblaze B2、Box、腾讯 COS、Dropbox、Google Drive、IBM COS S3、OneDrive、ownCloud、Nextcloud,另外 backup.sh 还支持 FTP、WebDAV、SFTP 等协议,可以说应用范围很广。

更多的网盘存储服务还有:

  1. 巧用又拍云 FTP 和坚果云 WebDAV - 打造个人文件备份和数据云存储
  2. Linux 共享文件夹目录三种方法 - NFS 远程挂载, GlusterFS 共享存储和 samba 共享目录
  3. 本地网络磁盘 RaiDrive 挂载 Dropbox,Google Drive,OneDrive 支持 WebDAV,FTP,SFTP

一、backup.sh 备份脚本安装

backup.sh 特点:

1、支持 MySQL/MariaDB/Percona 的数据库全量备份或选择备份;
2、支持指定目录或文件的备份;
3、支持加密备份文件(需安装 openssl 命令,可选);
4、支持上传至 Google Drive(需先安装 rclone 并配置,可选);
5、支持在删除指定天数本地旧的备份文件的同时,也删除 Google Drive 上的同名文件(可选)。

1.1 下载并赋予权限

执行以下命令:

wget --no-check-certificate https://github.com/teddysun/across/raw/master/backup.sh
chmod +x backup.sh
#备用
https://github.com/freehao123/across/blob/master/backup.shchmod +x backup.sh

1.2 修改并配置脚本

直接到 root 下面找到 backup.sh 用记事本打开编辑,修改配置参数。关于变量名的一些说明:

ENCRYPTFLG (加密FLG,true 为加密,false 为不加密,默认是加密)
BACKUPPASS (加密密码,重要,务必要修改)
LOCALDIR (备份目录,可自己指定)
TEMPDIR (备份目录的临时目录,可自己指定)
LOGFILE (脚本运行产生的日志文件路径)
MYSQL_ROOT_PASSWORD (MySQL/MariaDB/Percona 的 root 用户密码)
MYSQL_DATABASE_NAME (指定 MySQL/MariaDB/Percona 的数据库名,留空则是备份所有数据库)
※ MYSQL_DATABASE_NAME 是一个数组变量,可以指定多个。举例如下:

MYSQL_DATABASE_NAME[0]="phpmyadmin"
MYSQL_DATABASE_NAME[1]="test"BACKUP (需要备份的指定目录或文件列表,留空就是不备份目录或文件)
※ BACKUP 是一个数组变量,可以指定多个。举例如下:

BACKUP[0]="/data/www/default/test.tgz"
BACKUP[1]="/data/www/default/test/"
BACKUP[2]="/data/www/default/test2/"LOCALAGEDAILIES (指定多少天之后删除本地旧的备份文件,默认为 7 天)
DELETE_REMOTE_FILE_FLG (删除 Google Drive 或 FTP 上备份文件的 FLG,true 为删除,false 为不删除)
RCLONE_NAME (设置 rclone config 时设定的 remote 名称,务必要指定) 
RCLONE_FOLDER (指定备份时设定的 remote 的目录名称,该目录名在 Google Drive 不存在时则会自行创建。默认为空,也就是根目录) 
RCLONE_FLG (上传本地备份文件至 Google Drive 的 FLG,true 为上传,false 为不上传) 

FTP_FLG (上传文件至 FTP 服务器的 FLG,true 为上传,false 为不上传)
FTP_HOST (连接的 FTP 域名或 IP 地址)
FTP_USER (连接的 FTP 的用户名)
FTP_PASS (连接的 FTP 的用户的密码)
FTP_DIR (连接的 FTP 的远程目录,比如: public_html)

几个关键的地方:是否开启加密、是否开启自动上传到网盘、设置 Mysql 的 Root 密码、指定你的备份目录

1.3 注意事项的说明

脚本需要用 root 用户来执行;脚本需要用到 openssl 来加密,请事先安装好;脚本默认备份所有的数据库(全量备份);备份文件的解密命令如下:

openssl enc -aes256 -in [ENCRYPTED BACKUP] -out decrypted_backup.tgz -pass pass:[BACKUPPASS] -d -md sha1

备份文件解密后,解压命令如下:

tar -zxPf [DECRYPTION BACKUP FILE]

解释一下参数 -P:

tar 压缩文件默认都是相对路径的。加个 -P 是为了 tar 能以绝对路径压缩文件。因此,解压的时候也要带个 -P 参数。

二、安装并配置 rclone 命令

rclone 是一个命令行工具,用于 Google Drive 的上传下载等操作。官网网站:https://rclone.org/

你可以用以下的命令来安装 rclone,以 RedHat 系举例,记得要先安装 unzip 命令。

yum -y install unzip && wget -qO- https://rclone.org/install.sh | bash

然后,运行以下命令开始配置:

rclone config

输入:n 和设置一个名称。

vps-backup_01.png.webp

然后选择你要备份的网盘,这里以谷歌网盘为演示,id 和 Secret 都不用填写,直接回车。

vps-backup_02.png.webp

然后输入:1,赋予 rclone 操作云盘可读可写的权限,folder_id 和 account_file 都直接回车,不用填写。

vps-backup_03.png.webp

当设置到 Use auto config? 是否使用自动配置,选 n 不自动配置。然后根据提示用浏览器打开 rclone 给出的 URL,点击接受(Accept)

vps-backup_04.png.webp

然后将浏览器上显示出来的字符串粘贴回命令行里,完成授权,然后退出即可。

vps-backup_05.png.webp

完整的配置过程如下:

root@localhost:~# rclone config
2020/09/23 23:37:40 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> wzfoumi #设置一个名称
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / 1Fichier
   \ "fichier"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, Tencent COS, etc)
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Citrix Sharefile
   \ "sharefile"
 9 / Dropbox
   \ "dropbox"
10 / Encrypt/Decrypt a remote
   \ "crypt"
11 / FTP Connection
   \ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
13 / Google Drive
   \ "drive"
14 / Google Photos
   \ "google photos"
15 / Hubic
   \ "hubic"
16 / In memory object storage system.
   \ "memory"
17 / Jottacloud
   \ "jottacloud"
18 / Koofr
   \ "koofr"
19 / Local Disk
   \ "local"
20 / Mail.ru Cloud
   \ "mailru"
21 / Mega
   \ "mega"
22 / Microsoft Azure Blob Storage
   \ "azureblob"
23 / Microsoft OneDrive
   \ "onedrive"
24 / OpenDrive
   \ "opendrive"
25 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
26 / Pcloud
   \ "pcloud"
27 / Put.io
   \ "putio"
28 / QingCloud Object Storage
   \ "qingstor"
29 / SSH/SFTP Connection
   \ "sftp"
30 / Sugarsync
   \ "sugarsync"
31 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
32 / Transparently chunk/split large files
   \ "chunker"
33 / Union merges the contents of several upstream fs
   \ "union"
34 / Webdav
   \ "webdav"
35 / Yandex Disk
   \ "yandex"
36 / http Connection
   \ "http"
37 / premiumize.me
   \ "premiumizeme"
38 / seafile
   \ "seafile"
Storage> 13 #选择一个网盘
** See help for drive backend at: https://rclone.org/drive/ **

Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id>  #直接回车
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> #直接回车
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1 #选择1,赋予读写权限
ID of the root folder
Leave blank normally.

Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.

Enter a string value. Press Enter for the default ("").
root_folder_id> 
Service Account Credentials JSON file path 
Leave blank normally.
Needed only if you want use SA instead of interactive login.

Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.

Enter a string value. Press Enter for the default ("").
service_account_file> 
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n #不使用高级配置
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n> n #不使用自动配置
Please go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=wzfou.com
Log in and authorize rclone for access
Enter verification code> 4/4QENTDxxxwzfoucomc #输入授权码
Configure this as a team drive?
y) Yes
n) No (default)
y/n> n #是否配置为Team Drive
--------------------
[wzfoumi]
type = drive
scope = drive
token = {"access_token":"yaw.zfoucom2891+08:00"}
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y #确认你的配置是否正确
Current remotes:

Name                 Type
====                 ====
wzfoumi              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 脚本备份操作

3.1 手动备份

运行脚本开始备份

./backup.sh

脚本默认会显示备份进度,并在最后统计出所需时间。

vps-backup_08.png.webp

如果你想将脚本加入到 cron 自动运行的话,就不需要前台显示备份进度,只写日志就可以了。这个时候你需要稍微改一下脚本中的 log 函数。

log() {
    echo "$(date "+%Y-%m-%d %H:%M:%S")" "$1"
    echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE}
}

改为:

log() {
    echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE}
}

打开你的网盘就可以看到备份过来的文件了。

![]vps-backup_09.png.webp

3.2 自动定时备份

关于如何使用 cron 自动备份,可以参考:Linux Crontab 命令定时任务基本语法与操作教程,这里以 CentOS 6 来举例说明。 修改文件 /etc/crontab,内容如下:

SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
MAILTO=root
HOME=/root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
30  1  *  *  * root bash /root/backup.sh

以上表示,每天凌晨 1 点 30 分,以 root 用户执行一次 backup.sh 脚本。

注意:一定要修改其中的 PATH 和 HOME 变量的值。尤其是 HOME 变量,rclone 命令能否正确执行,是要依赖于其配置文件的。用 root 用户配置的话,其配置文件夹应该是 /root/.config/rclone ,所以要更改 HOME 的值。

四、总结

VPS 主机一键备份脚本 backup.sh 安装和使用都比较简单,主要关键点在于修改自己的配置参数,并且在 rclone 配置中绑定好自己的网盘,或者添加好 WebDAV、FTP 等账号信息,至于加密不加密看个人需要,毕竟解密也是一个 “技术” 活。


或许明日太阳西下倦鸟已归时