Client-side method to back up data
1, install and configure Dropbox Linux client
Install Linux command line version Dropbox on VPS:
32-bit:
CD ~ && wget-o-"https://www.dropbox.com/download?plat=lnx.x86" | Tar Xzf-
64-bit:
CD ~ && wget-o-"https://www.dropbox.com/download?plat=lnx.x86_64" | Tar Xzf-
After the download is complete, run Dropbox
~/.dropbox-dist/dropboxd
The first time you run the command, you are prompted to access the URL, and you can bind the VPS to the Dropbox account by accessing the URL. When you view the command line, you are prompted for a successful binding.
After binding, the folder named Dropbox is automatically created in the root directory, and Dropbox automatically synchronizes the files within that folder.
2, using Dropbox Linux client synchronization VPS data
We generally back up the site data and database data, under normal circumstances, the Web site directory is not in the root folder, we can through a soft link to the Web site directory, and database data can be exported through the command line, and through a soft connection to the file.
To facilitate the separation of directories, you can create a new backups folder under the Dropbox folder to back up your data.
1 regularly back up the site data
Connect the Web site directory to the/dropbox/backups folder, assuming/var/www/wordpress is the Web site Directory
CD ~/dropbox/backups
Ln-s/var/www/wordpress
2) Regular BACKUP database files
Create a new script to back up the database files regularly
VI ~/backup/mysqlbackup.sh
The script reads as follows:
echo "Start MySQL back Up" ${date} >>/root/backup/backup.log
mysqldump-uroot-p123456 WordPress >/root/backup/mysqlback.sql
Where root is the database username, 123456 is the database password, WordPress is the database name.
Add executable permissions
chmod a+x ~/backup/mysqlbackup.sh
Connect Mysqlback.sql to the/dropbox/backups folder
#!/bin/sh
CD ~/dropbox/backups
Ln-s ~/backup/mysqlback.sql
Execute the backup script file regularly, enter
Crontab-e
Add the following content
* 3 * * */root/backup/mysqlbackup.sh
3) timed opening and closing Dropbox Linux client
Dropbox is always open to occupy the memory resources of the VPS, there is no need to keep on, you can use the script to run and close the Dropbox.
Create Script ~/backup/dropboxswitch.sh
VI ~/backup/dropboxswitch.sh
The contents are as follows
#!/bin/sh
Date= ' Date +%y%m%d_%h:%m:%s '
Start () {
echo "Starting Dropbox" ${date} >>/root/backup/backup.log
/root/dropbox/.dropbox-dist/dropbox &
}
Stop () {
echo "Stoping dropbox" ${date} >>/root/backup/backup.log
Pkill Dropbox
}
Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart)
Stop
Start
;;
Esac
Add executable permissions
chmod a+x ~/backup/dropboxswitch.sh
And then timed every 4 o'clock in the morning start dropbox,6 point off:
Crontab-e
Add the following content
* 4 * * */root/backup/dropboxswitch.sh start
* 6 * * */root/backup/dropboxswitc.sh stop
Scripting to back up data
1. Create Dropbox application
First, you need to create a Dropbox application that can be created from this URL: https://www.dropbox.com/developers/apps/create.
Here, the application type chooses Dropbox API App, the data storage type selects files and Datastores, and the permissions Select Yes (application only needs to access the files it creates). Then name the creation.
2. Download and execute dropbox_uploader.sh
Dropbox_uploader is a third-party dropbox backup script that downloads the script first:
wget--no-check-certificate https://raw.githubusercontent.com/tennfy/Dropbox-Uploader/master/dropbox_uploader.sh
chmod a+x dropbox_uploader.sh
Execute the script and bind the app:
./dropbox_uploader.sh
According to the prompts input Dropbox application of the app key and app secret, license type Select a, confirm Y, copy the given permission verification link to the browser, confirm back to the terminal Press any key to complete.
Dropbox_uploader1
You can then perform the following command to test the upload, suggesting that the binding was successful:
./dropbox_uploader.sh Upload/etc/passwd/backup/passwd.old
3. Write timed Script
vi/root/backup.sh
The contents are as follows:
#!/bin/bash
Mysql_user=root #mysql用户名
Mysql_pass=xxxxx #mysql密码
Mysql_databasename=xxxxx #要备份的数据库名
Web_data=/var/www/xxx #要备份的网站数据
#你要修改的地方从这里结束
# define Backup Storage directory
Backup directory on dropbox_dir=/$ (date +%y-%m-%d) # DROPBOX
Local_bak_dir=/root/backup # Local backup file storage directory
#定义备份文件名字
dbbakname=data_$ (date + "%y%m%d"). tar.gz
webbakname=web_$ (date + "%y%m%d"). tar.gz
# define old data names
old_dropbox_dir=/$ (date-d -3day +%y-%m-%d)
olddbbakname=data_$ (date-d -3day + "%y%m%d"). tar.gz
oldwebbakname=web_$ (date-d -3day + "%y%m%d"). tar.gz
#删除本地3天前的数据
RM-RF $LOCAL _bak_dir/$OldDBBakName $LOCAL _bak_dir/$OldWebBakName
./dropbox_uploader.sh Delete $Old _dropbox_dir/
#导出mysql数据库
Mysqldump-u$mysql_user-p$mysql_pass $MYSQL _databasename > $LOCAL _bak_dir/wordpress.sql
#压缩数据库
Tar zcvf $LOCAL _bak_dir/$DBBakName $LOCAL _bak_dir/wordpress.sql
RM-RF $LOCAL _bak_dir/wordpress.sql
#压缩网站数据
Tar zcvf $LOCAL _bak_dir/$WebBakName $WEB _data
#开始上传
CD ~
/dropbox_uploader.sh upload $LOCAL _bak_dir/$DBBakName $DROPBOX _dir/$DBBakName
/dropbox_uploader.sh upload $LOCAL _bak_dir/$WebBakName $DROPBOX _dir/$WebBakName
Echo-e "Backup done!"
In which, users can adapt their needs to backup of the directory, as well as the retention of old data duration (I set this here is 3 days)
If MySQL is a compiled installation, you need to specify the path to MySQL.
To increase execution permissions:
chmod +x/root/backup.sh
Test the backup script:
./backup.sh
4. Set a timed task
Perform:
Crontab–e
Add the following:
3 * * */root/backup.sh
This way, you can automatically back up to Dropbox every 3:30.
5. Finally restart Crontab
Service Cron Restart
Setup is complete after reboot.
If you do not know the current time of the server, you can use the following command to view the current time:
Date-r
Modify to current time zone
Cp/usr/share/zoneinfo/asia/shanghai/etc/localtime
That is, change the server time zone to Shanghai.