VPS automatically backs up site database and sends email notification

Source: Internet
Author: User
Tags define local vps
The code is as follows Copy Code

#!/bin/bash
# #Program: This script is executed at a known time to create a database ' s backup and upload it to another host.
#Info of database:
#Name: Ninthday_sql username:ninthday_sql password:ninthday_passwd
#
#Info of Backup Host:
#FTPName: Ninthday_ftp
#FTPPasswd: ninthday_passwd
#Backup dir:backups
#
#Contact: Http://www.111cn.net
##

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin
Export PATH

# defines the database information, where name is the database name, username is the database user name, password is the password #
Name=ninthday_sql
Username=ninthday_sql
password=ninthday_passwd

# define local backup path #
Localdir=/home/chroot/home/ninthday

# define temporary name for local backup #
lbackname=backup-blog-$ (date +%y%m%d). sql

# define the name of the backup local three days ago #
loldbackup=backup-blog-$ (date-d -3day +%y%m%d). sql.tar.gz

# define remote Backup name #
rbackname=backup-blog-$ (date +%y%m%d). sql.tar.gz

# define remote 5 days ago Backup name #
roldbackup=backup-blog-$ (date-d -5day +%y%m%d). sql.tar.gz

# define remote FTP information #
ftphost=216.18.217.23
Ftpname=ninthday_ftp
ftppasswd=ninthday_passwd

# define User #
User=ninthday

# define addresses for sending and receiving notification messages (must be in different domains) #
Sender=root@ninthday.net
Receiver=kailash@126.com

# Start creating local Files #

Mysqldump-u$username-p$password $Name > $Localdir/backups/$LBackname

# Compress to Local #
Tar zpcf $Localdir/backups/$RBackname $Localdir/backups/$LBackname

# Uploading Data #
Lftp $FTPHost-u $FTPName, $FTPPasswd << EOF
CD backups
Mput $Localdir/backups/$RBackname
MRM $ROldbackup
Bye
Eof

# Delete local redundant and expired data #
Rm-f $Localdir/backups/$LBackname
If [-e $Localdir/backups/$LOldbackup]; Then
Rm-f $Localdir/backups/$LOldbackup
Fi

# Write log File #
echo "A datebase ' s backup has already created and uploaded to the host on $ (date +%f)." >> $Localdir/LOGS/DBBACKUP.L og

# Change Permissions #
Chown $user: $user $Localdir/backups/$RBackname

# Send notification message #
Sendmail-t <<eof
From:host < $sender >
To: $receiver
Subject:database succeeded in creating backup.
----------------------------------------------------------
The VPS has succeeded in creating and uploading database ' s backup.

It holds 5 database ' s backups created on the latest 5 sequential days
On remote host and 3 database ' s backups on localhost (/backups/).

Please download the backup files to your computer in a regular time schedule.

Don ' t reply this email!

Contact:harid@ninthday.net
Http://www.111cn.net

----------------------------------------------------------
Eof

Exit 0

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.