Automatically back up the Web site and database under Linux and send it to the mailbox/ftp

Source: Internet
Author: User

Automatically backs up sites and databases and sends them to mailboxes

1, SSH login to the VPS, into the/home/backup directory, new file backup.sh, paste the following code and save;

#!/bin/bash
# Go to backup folder
Cd/home/backup
# Create a folder that holds the backup files and databases, and modify the permissions to 777
Mkdir-m 777-p./backup$ (date + "%y%m%d")
# Copy the files that need to be backed up into the backup folder
Cp-r/home/wwwroot./backup$ (date + "%y%m%d")/wwwroot
# Export Database to backup folder
/usr/local/mysql/bin/mysqldump-uroot-p123456 Blog >/backup$ (date + "%y%m%d")/blog.sql
# Compress folders that store backup files and databases
Tar zcvf./backup$ (date + "%y%m%d"). tar.gz./backup$ (date + "%y%m%d")
# Send a compressed package to a specified mailbox as an attachment
echo "Blog date" |mutt-s "backup$ (date +%y-%m-%d)"-A./backup$ (date + "%y%m%d"). tar.gz xx@xx.com
# Delete backup folders and compression packs
Rm-rf./backup$ (date + "%y%m%d")
Rm./backup$ (date + "%y%m%d"). tar.gz
2, install mutt send mail;

Only the above commands do not reach our goal, you have to install mutt to send mail, in the client to enter the following command:

Yum Install-y Mutt Vixie-cron
Test the code below to enter backup.sh in the terminal, and you will receive a backup message if you are not surprised.
If you are prompted with an error message "Bin/bash:bad interpreter:no such file or directory", your backup script file is not in UNIX format and you can convert the file format to UNIX format using Dos2unix backup.sh.

3, add to the scheduled task, in the client input the following command;

Crontab-e
* * */home/backup/backup.sh
The 23:59 chapters of each day perform/home/backup/backup.sh automatically.

After many tests, did not find any problems, packed files decompression, database with phpMyAdmin import, no problem. Then you can rest assured that the data is priceless!


automatically daily backup files and database upload FTP space

This article describes a daily automatic backup site and database files, send email to the mailbox, and upload sites and data plus files to FTP space, automatic deletion of old backup methods.

First install email send component:

Yum Install SendMail Mutt

Script Download Address: http://static.vpsmm.com/soft/AutoBackupToFtp.sh

Script code as follows (note modify FTP server address username password):

#!/bin/bash
#你要修改的地方从这里开始
Mysql_user=root #mysql用户名
mysql_pass=123456 #mysql密码
Mail_to=cat@hostloc.com #数据库发送到的邮箱
Ftp_user=cat #ftp用户名
ftp_pass=123456 #ftp密码
Ftp_ip=imcat.in #ftp地址
Ftp_backup=backup #ftp上存放备份文件的目录, this should be built on its own FTP
Web_data=/home/www #要备份的网站数据
#你要修改的地方从这里结束

#定义数据库的名字和旧数据库的名字
databakname=data_$ (date + "%y%m%d"). tar.gz
webbakname=web_$ (date +%y%m%d). tar.gz
olddata=data_$ (date-d -5day + "%y%m%d"). tar.gz
oldweb=web_$ (date-d -5day + "%y%m%d"). tar.gz
#删除本地3天前的数据
rm-rf/home/backup/data_$ (date-d -3day + "%y%m%d"). tar.gz/home/backup/web_$ (date-d -3day + "%Y%m%d"). tar.gz
Cd/home/backup
#导出数据库, a database with a compressed file
For DB in '/usr/local/mysql/bin/mysql-u$mysql_user-p$mysql_pass-b-n-e ' show DATABASES | Xargs '; Todo
(/usr/local/mysql/bin/mysqldump-u$mysql_user-p$mysql_pass ${db} | gzip-9-> ${db}.sql.gz)
Done
#压缩数据库文件为一个文件
Tar zcf/home/backup/$DataBakName/home/backup/*.sql.gz
Rm-rf/home/backup/*.sql.gz
#发送数据库到Email, if the database is compressed too large, please comment this line
echo "Subject: Database Backup" | Mutt-a/home/backup/$DataBakName-S "Content: Database Backup" $MAIL _to
#压缩网站数据
Tar zcf/home/backup/$WebBakName $WEB _data
#上传到FTP空间, delete data from FTP space 5 days ago
FTP-V-N $FTP _ip << End
User $FTP _user $FTP _pass
Type binary
CD $FTP _backup
Delete $OldData
Delete $OldWeb
Put $DataBakName
Put $WebBakName
Bye
End

To add execute permissions to a script:

chmod +x/root/autobackuptoftp.sh

Use System crontab to achieve automatic daily operation:

Crontab-e

Enter the following content:

* * * */root/autobackuptoftp.sh

Of these, 00 00 is the time division/hour, can be modified, for example: 30 12 * * *, is every day 12.30 run this script.

Attach a backup file using Lftp

#!/bin/bash
#定义数据库的名字和旧数据库的名字
databakname=data_$ (date + "%y%m%d"). tar.gz
webbakname=web_$ (Date +%y%m% d). tar.gz
#删除本地3天前的数据
rm-rf/home/backup/data_$ (date-d -3day + "%y%m%d"). tar.gz/home/backup/web_$ (Date-d- 3day + "%y%m%d"). tar.gz
#导出mysql数据库
/usr/local/mysql/bin/mysqldump-uroot-ppassword--databases db1 >/home/ Backup/databackup.sql
#压缩数据库
Tar zcf/home/backup/$DataBakName/home/backup/databackup.sql
rm-rf/home/ Backup/databackup.sql
#压缩网站数据
Tar zcvf/home/backup/$WebBakName/home/wwwroot
#使用lftp同步备份目录
Lftp-u User,password-e "Mirror-r--only-newer/home/backup/backup" ftp.yoursite.cn
Exit
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.