Linux VPS主机自动备份网站和数据库脚本通用版-自动备份谷歌网盘,阿里云OSS等

数据是无价的。我们使用Linux VPS主机建站的话一定要注意经常备份自己的网站文件和数据库。一般来说,如果你建站环境用的是OneinstackLNMP一键包的话,都会自带了自带了备份功能。VPS主机面板例如宝塔BT面板等自动备份功能都是标备的。

如果以上都没有采用的话,那么就要自己手动备份网站文件和数据库了。这篇文章是来分享一下适用于所有的Linux VPS主机自动备份网站和数据库,同时将网站文件和数据库自动打包上传到Google Drive、Dropbox、Onedrive、阿里云OSS等云存储当中,节省备份时间。

Linux VPS主机自动备份网站和数据库脚本通用版-自动备份谷歌网盘,阿里云,Dropbox等

更多的关于网站备份和恢复的文章还有:

  1. 巧用又拍云FTP和坚果云WebDAV-打造个人文件备份和数据云存储
  2. cPanel和DirectAdmin面板备份和恢复方法-cPanel和DirectAdmin搬家
  3. SolusVM KVM VPS主机迁移搬家方法-SolusVM KVM备份恢复教程

PS:2020年3月19日更新,如果你想在本地Windows电脑上挂载各种网盘,当成本地硬盘使用,参考: 本地网络磁盘RaiDrive挂载Dropbox,Google Drive,OneDrive支持WebDAV,FTP,SFTP

一、Linux VPS一键备份脚本

1.1 backup.sh下载

backup.sh的主要特点有:

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

下载该脚本并赋予执行权限:

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

2.2 backup.sh配置

直接编辑backup.sh文件,相关的变量名说明:

ENCRYPTFLG (加密FLG,true 为加密,false 为不加密,默认是加密)
BACKUPPASS (加密密码,重要,务必要修改)
LOCALDIR (备份目录,可自己指定)
TEMPDIR (备份目录的临时目录,可自己指定)
LOGFILE (脚本运行产生的日志文件路径)
MYSQL_ROOT_PASSWORD (MySQL/MariaDB/Percona 的 root 用户密码)
MYSQL_DATABASE_NAME (指定 MySQL/MariaDB/Percona 的数据库名,留空则是备份所有数据库)
BACKUP (需要备份的指定目录或文件列表,留空就是不备份目录或文件)

LOCALAGEDAILIES (指定多少天之后删除本地旧的备份文件,默认为 7 天)
DELETE_REMOTE_FILE_FLG (删除 Google Drive 或 FTP 上的备份文件 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_DATABASE_NAME 是一个数组变量,可以指定多个。举例如下:

MYSQL_DATABASE_NAME[0]=”phpmyadmin”
MYSQL_DATABASE_NAME[1]=”test”

※ BACKUP 是一个数组变量,可以指定多个。举例如下:

BACKUP[0]=”/data/www/default/test.tgz”
BACKUP[1]=”/data/www/default/test/”
BACKUP[2]=”/data/www/default/test2/”

2.3 backup.sh备份

执行以下命令开始备份:

./backup.sh

我们可以在我们设置的路径下看到已经备份的文件和数据库。

Linux VPS主机自动备份查看备份

如果你设置了加密备份的话,想要查看的话还需要解密,命令如下:

openssl enc -aes256 -in /root/backups/ubuntu_20200101160232.tgz.enc -out decrypted_backup1.tgz -pass pass:qimm -d -md sha1

二、Rclone备份同步Google Drive

Rclone:

  1. 官网:https://github.com/ncw/rclone 
  2. 教程:https://wzfou.com/rclone-cos-fuse-ossfs/

市面上有非常多的自动备份文件到Google Drive的脚本,这里推荐一下“全能王”脚本:Rclone,它可以自动备份到绝大多数网盘、云存储当中,如下:

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, 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 / JottaCloud
   \ “jottacloud”
17 / Koofr
   \ “koofr”
18 / Local Disk
   \ “local”
19 / Mail.ru Cloud
   \ “mailru”
20 / Mega
   \ “mega”
21 / Microsoft Azure Blob Storage
   \ “azureblob”
22 / Microsoft OneDrive
   \ “onedrive”
23 / OpenDrive
   \ “opendrive”
24 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ “swift”
25 / Pcloud
   \ “pcloud”
26 / Put.io
   \ “putio”
27 / QingCloud Object Storage
   \ “qingstor”
28 / SSH/SFTP Connection
   \ “sftp”
29 / Transparently chunk/split large files
   \ “chunker”
30 / Union merges the contents of several remotes
   \ “union”
31 / Webdav
   \ “webdav”
32 / Yandex Disk
   \ “yandex”
33 / http Connection
   \ “http”
34 / premiumize.me
   \ “premiumizeme”

2.1 申请Google API

  1. https://console.developers.google.com/

英文操作说明如下:

Log into the Google API Console with your Google account. It doesn’t matter what Google account you use. (It need not be the same account as the Google Drive you want to access)

Select a project or create a new project.

Under “ENABLE APIS AND SERVICES” search for “Drive”, and enable the “Google Drive API”.

Click “Credentials” in the left-side panel (not “Create credentials”, which opens the wizard), then “Create credentials”, then “OAuth client ID”. It will prompt you to set the OAuth consent screen product name, if you haven’t set one already.

Choose an application type of “other”, and click “Create”. (the default name is fine)

It will show you a client ID and client secret. Use these values in rclone config to add a new remote or edit an existing remote.

首选是进入到Google API Console,然后搜索Google Drive API,然后在左边新建凭据。

Linux VPS主机自动备份申请API
Linux VPS主机自动备份凭据

选择Oauth 客户端。

Linux VPS主机自动备份客户端

然后在类型中选择其它。

Linux VPS主机自动备份类型为其它

这时候你就会得到一个ID和Key了。

Linux VPS主机自动备份得到ID和Key

2.2 Rclone配置谷歌网盘

执行命令:rclone config 就可以开始配置网盘了,整个命令操作解释如下:

root@ubuntu:~# rclone config
2020/01/01 14:49:53 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> qimm #命名
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, 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 / JottaCloud
   \ "jottacloud"
17 / Koofr
   \ "koofr"
18 / Local Disk
   \ "local"
19 / Mail.ru Cloud
   \ "mailru"
20 / Mega
   \ "mega"
21 / Microsoft Azure Blob Storage
   \ "azureblob"
22 / Microsoft OneDrive
   \ "onedrive"
23 / OpenDrive
   \ "opendrive"
24 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
25 / Pcloud
   \ "pcloud"
26 / Put.io
   \ "putio"
27 / QingCloud Object Storage
   \ "qingstor"
28 / SSH/SFTP Connection
   \ "sftp"
29 / Transparently chunk/split large files
   \ "chunker"
30 / Union merges the contents of several remotes
   \ "union"
31 / Webdav
   \ "webdav"
32 / Yandex Disk
   \ "yandex"
33 / http Connection
   \ "http"
34 / premiumize.me
   \ "premiumizeme"
Storage> 13 #选择GoogleDrive
** 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> xxxxxxxxxxxxx #输入刚刚得到的ID
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> xxxxxxxxxxxxxxxx #输入刚刚得到的Key
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 #选择第一个全部权限
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.

Note that if this is blank, the first time rclone runs it will fill it
in with the ID of the root folder.

Enter a string value. Press Enter for the default ("").
root_folder_id> xxxxxxxxxxxxxx  #指定一个文件夹(该文件夹为URL后一个唯一串字符)
Service Account Credentials JSON file path 
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default ("").
service_account_file> 
Edit advanced config? (y/n)
y) Yes
n) No
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
n) No 
y/n> n #取消自动配置,改为下面的手动配置
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?xxxxxxxxxxxx #复制这个链接后到你的浏览器打开
Log in and authorize rclone for access
Enter verification code> xxxxxxxxxx #输入验证后的字符串
Configure this as a team drive?
y) Yes
n) No
y/n> n
--------------------
[qimm]
type = drive
client_id = xxxxxxx
client_secret =xxxxxx
scope = drive
root_folder_id = qimm.wzfou.com
token =xxxxxxxx
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y #确认完成
Current remotes:

Name                 Type
====                 ====
qimm                 drive

在配置过程中需要你打开本地浏览器验证。

Linux VPS主机自动备份浏览器验证

2.3 同步备份Google Drive

使用以下命令可以备份或者同步文件到Google Drive:

#备份/root/backups所有文件到Google Drive(如果刚刚指定了文件夹,就是指定文件夹,否则就是根目录)下的ceshi文件夹(该文件夹不存在则新建)
rclone copy --ignore-existing /root/backups qimm:ceshi
#--ignore-existing参数忽略已经存在的文件,相当于增量备份。

#同步/root/backups所有文件到Google Drive(如果刚刚指定了文件夹,就是指定文件夹,否则就是根目录)下的ceshi文件夹(该文件夹不存在则新建)
rclone sync /root/backups qimm:ceshi

打开Google Drive就可以看到备份和同步过来的文件了。

Linux VPS主机自动备份查看复制过来的文件

三、Rclone备份同步阿里云OSS

3.1 申请阿里云OSS API

首到阿里云OSS中申请一个用于Rclone同步到阿里云OSS的API。

Linux VPS主机自动备份阿里云OSS

同时记得赋予OSS全部权限。

Linux VPS主机自动备份赋予权限

3.2 Rclone配置阿里云OSS

按照Rclone配置Google Drive方法,执行命令:rclone config ,在配置时选择Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)

Linux VPS主机自动备份选择类型

然后就是填写你的OSS API了,接着就是选择地域了。

Linux VPS主机自动备份选择地域

最后确定完成Rclone配置阿里云OSS。

Linux VPS主机自动备份完成配置

3.3 同步备份到阿里云OSS

同步备份到阿里云OSS的命令如下:

#备份/root/backups所有文件到阿里云OSS下的ceshiqi桶中(该文件夹不存在则新建)
rclone copy --ignore-existing /root/backups qimmali:ceshiqi
#同步/root/backups所有文件到阿里云OSS下的ceshiqi桶中(该文件夹不存在则新建)
rclone sync /root/backups qimmali:ceshiqi

打开阿里云OSS就可以看到同步过来的文件了。

Linux VPS主机自动备份同步的文件

四、Rclone自动备份同步到网盘

Rclone实现在自动备份和同步到谷歌网盘,阿里云,Dropbox等就需要用到Linux定时任务了,有关于定时任务的用法,请参考:Linux Crontab命令定时任务基本语法与操作教程-VPS/服务器自动化

自动同步到Google Drive网盘定时任务如下 :

10 1 * * *  /bin/bash /root/backup.sh > /tmp/out.log5 2>&1
10 2 * * *  rclone sync /root/backups qimm:ceshi > /tmp/out.log6 2>&1

自动同步到阿里云OSS定时任务如下 :

10 1 * * *  /bin/bash /root/backup.sh > /tmp/out.log5 2>&1
10 2 * * *  rclone sync /root/backups qimmali:ceshiqi > /tmp/out.log6 2>&1

五、总结

一般来说我们用的建站脚本或者是VPS主机面板都会自带了自动备份功能,不过backup.sh具有普遍适用性,它可以自定义备份路径、数据库等等,可以设置保存的时长,适用于各种建站环境。

rclone sync是同步,即本地目录有删除文件的话,远程网盘中的文件也会删除。而rclone copy –ignore-existing则是只增不减,各有各的优势,你可以根据自己的需要来选择。

文章出自:挖站否 https://wzfou.com/linux-vps-beifen/,版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。


分享到:
Avatar for Qi
关于站长(Qi),2008年开始混迹于免费资源圈中,有幸结识了不少的草根站长。之后自己摸爬滚打潜心学习Web服务器、VPS、域名等,兴趣广泛,杂而不精,但愿将自己经验与心得分享出来与大家共勉。
已有 13 条评论
  1. 本地备份曾遇到过丢失的情况,现在用rclone每天同步到onedrive就放心了。

    2020年12月15日 17:14 回复
    • Avatar for Qi Qi

      我现在用Nextcloud

      2020年12月17日 15:07 回复
  2. Avatar for 哒哒哒 哒哒哒

    May 15 01:56:01 active-bump-1 CRON[7920]: (root) CMD (/bin/bash /home/backups/backups.sh > /tmp/out.log5 2>&1)

    Debian的定时请教为啥不执行啊 [泪]

    2020年5月15日 02:05 回复
    • Avatar for Qi Qi

      应该是没有设置环境变量,这个脚本要根据Linux的变量来调整。

      2020年9月24日 12:54 回复
  3. 我通常用tar和mysqldump打包然后下到本地来的,或者放阿里云OSS的低频访问

    2020年4月2日 17:21 回复
  4. Avatar for newone newone

    备份,备份,备份,重要的要说3遍

    2020年3月7日 20:13 回复
  5. Avatar for Rhilip Rhilip

    teddysun的备份脚本在2020 年 2 月 8 日的更新中就把gdrive改成了rclone,你这3月份的文章咋还需要额外配置rclone的crontab?

    2020年3月7日 10:42 回复
    • Avatar for Qi Qi

      定时发布的。

      2020年9月24日 12:55 回复
  6. 一直没咋备份网站。

    2020年3月6日 20:31 回复
  7. Avatar for saber saber

    备份要勤快。

    2020年3月5日 15:20 回复
  8. 可惜我国内的主机只能放oss

    2020年3月5日 10:30 回复
    • Avatar for Qi Qi

      阿里云的存储也可以,我也在用。

      2020年3月6日 13:28 回复

Login

Welcome! Login in to your account

Remember me Lost your password?

Don't have account. Register

Lost Password

Register