Linux vps/Server website and database automatic local backup and FTP upload backup script _linux

Source: Internet
Author: User
Tags server website vps
Preparatory work:

Need to advance in the VPS to install good lftp,lftp function more powerful, CentOS Direct execution: Yum install Lftp,debian execution: Apt-get install.

You need to create the/home/backup/directory on the VPS and create the backup directory on FTP.

If the VPS on the database is not much to use GoDaddy free space can (10GB space, 300GB traffic), as long as the registration of the domain name is free to send.

The following is a partial comment on the backup script:
Copy Code code as follows:

#!/bin/bash
#Funciont: Backup website and MySQL database
#Author: licess
#Website: http://lnmp.org
#IMPORTANT!!! Please Setting the following values!
######~set directory for your want to backup~##### #将下面的目录修改成自己要备份的目录, generally according to my all is under/home/wwwroot/all directly write the directory that needs to be backed up. Can continue to add: backup_dir5= your directory, backup_dir the number of subsequent increments. If you have less than 4, you can delete what you don't need and modify the Tar zcf section below.

Backup_dir1=vpser.net
backup_dir2=lnmp.org
backup_dir3=licess.org
Backup_dir4=jungehost.com

######~set MySQL UserName and password~##### #设置MySQL的用户名和密码, preferably root, other users may not be able to export part of the database because of permissions issues.
Mysql_username=root
Mysql_password=yourmysqlrootpassword

######~set MySQL database you want to backup~##### #设置要部分的数据库, can continue to add: backup_database_name5= name, Backup_database_ The number after name increments sequentially.
Backup_database_name1=vpser
Backup_database_name2=licess
Backup_database_name3=junge
backup_database_name4=vpserorg

######~set FTP information~##### #设置用来存放备份数据的FTP信息
IP or domain name of ftp_hostname=184.168.192.43//ftp server
Ftp_username=vpsernet//ftp Server user name
Ftp_password=yourftppassword//ftp Server User's password
Ftp_backupdir=backup//Backup to FTP directory, need to be created well in advance.

#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

Tar zcf/home/backup/www-$Backup _dir1-$ (date + "%y%m%d"). Tar.gz-c/home/wwwroot/$Backup _dir1--exclude=soft
Tar zcf/home/backup/www-$Backup _dir2-$ (date + "%y%m%d"). Tar.gz-c/home/wwwroot/$Backup _dir2
Tar zcf/home/backup/www-$Backup _dir3-$ (date + "%y%m%d"). Tar.gz-c/home/wwwroot/$Backup _dir3--exclude=test
Tar zcf/home/backup/www-$Backup _dir4-$ (date + "%y%m%d"). Tar.gz-c/home/wwwroot/$Backup _DIR4

# # #上面为备份网站文件数据, because my site is more fragmented, and the site directory below some of the directories belong to the temporary directory does not need to back up, so you can add the above--exclude= not to back up the directory. If you add a backup_dir5=yourdir to the front, add the tar zcf/home/backup/www-$Backup _dir5-$ (date + "%y%m%d"). Tar.gz-c
/home/wwwroot/$Backup _DIR5. If superfluous, delete the extra rows.

/usr/local/mysql/bin/mysqldump-u$mysql_username-p$mysql_password $Backup _database_name1 >/home/backup/db-$ backup_database_name1-$ (date + "%y%m%d"). sql
/usr/local/mysql/bin/mysqldump-u$mysql_username-p$mysql_password $Backup _database_name2 >/home/backup/db-$ backup_database_name2-$ (date + "%y%m%d"). sql
/usr/local/mysql/bin/mysqldump-u$mysql_username-p$mysql_password $Backup _database_name3 >/home/backup/db-$ backup_database_name3-$ (date + "%y%m%d"). sql
/usr/local/mysql/bin/mysqldump-u$mysql_username-p$mysql_password $Backup _database_name4 >/home/backup/db-$ backup_database_name4-$ (date + "%y%m%d"). sql

# # #上面为备份MySQL数据库, if you add Backup_database_name5=yourdatabasename in front, then add/usr/local/mysql/bin/mysqldump-u$mysql_ Username-p$mysql_password $Backup _database_name5 >/home/backup/db-$Backup _database_name5-$ (date + "%y%m%d"). sql. If superfluous, delete the extra rows.

RM $OldWWWBackup
RM $OldDBBackup
# # #删除3天前的备份 ###

cd/home/backup/

# # #下面为自动上传部分, have to say lftp very strong, abandon FTP bar #
Lftp $FTP _hostname-u $FTP _username, $FTP _password << EOF
CD $FTP _backupdir
MRM $OldWWWBackup
MRM $OldDBBackup
Mput $TodayWWWBackup
Mput $TodayDBBackup
Bye
Eof

Most of the above are expressed in the form of code, for beginners may be more difficult to understand, a see on the Meng, or serious study will certainly be able to harvest, I hope that the content mentioned above can help.

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.