Linux backup database and Web site files detailed

Source: Internet
Author: User
Tags mkdir sybase

Example 1, manually backing up the database and Web site files

The code is as follows Copy Code

#!/bin/sh
# FileName: rptsmain.sh
# Copyrights: Copyright (shell) 2007-2007
# version Number: V1.0
# function Description: Data backup
# Create Person: xxxxx
# Date: 2007/10/26
# Modified Person: xxxxxxx
# Date: 2007/10/26

#程序开始

today= ' Date ' +%y%m%d '
Workdate= $TODAY
Backdir= '/databak/'

Tput Init
Clear
While:
Todo
Tput Clear

echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo "
Data Backup Program V1.0

Copyright (Shell)

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃┃
┃1. Start backing up data ┃
┃┃
┃2. Backup data writes to tape ┃
┃┃
┃----------------------------------┃
┃┃
┃0. Exit System ┃
┃┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Please select []

Read Choice
 case $choice in
  1):
   Clear
  
   echo "Start BCP database data ... "
   databasedir= $BACKDIR/database
   If [-D $DATABASEDIR]; then
 & nbsp;  RM-RF $DATABASEDIR/*
   else   
    mkdir $DATABASEDIR
   fi
  
   #备份webdb数据库
   mkdir $DATABASEDIR/webdb
    total= ' cat webdb.table|wc-l '    
   current=0                    
   for table in ' Cat webdb.table '
    do 
     current= $current +1 
 & nbsp;  /sybase/ocs-12_5/bin/bcp webdb. $table out

$DATABASEDIR/aadb/$table. Bcp-usa-ppwd-sserver-jiso_1-n-t409600
Done

#备份abcdb数据库
mkdir $DATABASEDIR/abcdb
Total= ' Cat abcdb.table|wc-l '
Current=0
For table in ' Cat abcdb.table '
Todo
current= $current +1
/SYBASE/OCS-12_5/BIN/BCP abcdb. $table out

$DATABASEDIR/bbdb/$table. Bcp-usa-ppwd-sserver-jiso_1-n-t409600
Done

echo "starts packing compressed database backup data ..."
CD $BACKDIR
Tar UVF database-$WORKDATE. Tar database/*

echo "is deleting temporary files ..."
RM-RF $DATABASEDIR



Echo starts packing the compressed data file ...
echo "(1) starts packing the TOMCAT-ABC website data file ..."
Sourcedir= '/aaaa/uploads '
CD $BACKDIR
#tar czf abc-files-$WORKDATE. tar.gz $SOuRCEDIR/iso/*

echo "(2) starts packing the WebSphere Management network data File ..."
CD $BACKDIR
#tar czf web-files-$WORKDATE. tar.gz/upload/images/807/*

echo "Data backup complete, press any key to return ..."
CD $HOME
Read return
;;
2):
;;
0):
Break
;;
Esac
Done

Example 2, automatically timed back up the database

The code is as follows Copy Code

#!/bin/bash
#Setting
#设置数据库名, database login name, password, backup path, log path, data file location, and backup method
#默认情况下备份方式是mysqldump, can also be mysqldump,mysqldotcopy, if the comment off the first
#18行, the default is to be backed up by tag
#默认情况下, log in to the MySQL database with root (empty), back up to/root/dbxxxxx.tgz
Dbname=mysql
Dbuser=root
dbpasswd=123456
Backuppath=/home/wang/www/backup/mysql/mysqlbackup
Logfile=/home/wang/www/backup/mysql/db.log
dbpath=/var/lib/mysql/
#BackupMethod =mysqldump
#BackupMethod =mysqlhotcopy
#BackupMethod =tar
#Setting End

Newfile= "$BackupPath" db$ (date +%y%m%d). tgz
Dumpfile= "$BackupPath" db$ (date +%y%m%d)
Oldfile= "$BackupPath" db$ (date +%y%m%d--date= ' 5 days ago '). tgz
echo "-------------------------------------------" >> $LogFile
echo $ (date + "%y-%m-%d%h:%m:%s") >> $LogFile
echo "--------------------------" >> $LogFile
#Delete Old File
If [f $OldFile]
Then
Rm-f $OldFile >> $LogFile 2>&1
echo "[$OldFile]delete old File success!" >> $LogFile
Else
echo "[$OldFile]no old Backup file!" >> $LogFile
Fi
If [f $NewFile]
Then
echo [$NewFile]the Backup File is Exists,can ' t backup! ' >> $LogFile
Else
Case $BackupMethod in
mysqldump*)
If [-Z $DBPasswd]
Then
Mysqldump-u $DBUser--opt $DBName > $DumpFile
Else
Mysqldump-u $DBUser-p$dbpasswd--opt $DBName > $DumpFile
Fi
Tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]backup success!" >> $LogFile
RM-RF $DumpFile
;;
mysqlhotcopy*)
RM-RF $DumpFile
mkdir $DumpFile
If [-Z $DBPasswd]
Then
Mysqlhotcopy-u $DBUser $DBName $DumpFile >> $LogFile 2>&1
Else
Mysqlhotcopy-u $DBUser-P $DBPasswd $DBName $DumpFile >> $LogFile 2>&1
Fi
Tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]backup success!" >> $LogFile
RM-RF $DumpFile
;;
*)
/etc/init.d/mysqld Stop >/dev/null 2>&1
Tar czvf $NewFile $DBPath >> $LogFile 2>&1
/etc/init.d/mysqld Start >/dev/null 2>&1
echo "[$NewFile]backup success!" >> $LogFile
;;
Esac
Fi
echo "-------------------------------------------"
Echo


#lftp-F lftp.sh ftp can be uploaded at the same time.

Then use the crontab command to generate the/var/spool/cron/root file

Crontab–e (will open an edit window, the first line has content formatting prompts, enter the command here)

M h Dom Mon Dow command (meaning: Minute hour date Month week commands, one day (DOM) of a month (Mon), or several days (DOW) (h,24 hours) to execute a command (m)


such as: * * */home/backup/autobackupmysql.sh (0 morning 2:10 to perform backup script)


Example 3, our own Linux VPS automatically backs up data to remote host FTP

If there is no FTP command on the system, perform the Yum install-y FTP installation first.
You need to create a/home/backup/directory that cannot be used for other purposes.

The code is as follows Copy Code

#!/bin/bash

#FTP信息
ftp_ip=
Ftp_user=
ftp_pass=

#备份数据库
/etc/init.d/mysqld stop
mysqlbakname=mysql_$ (date + "%y%m%d"). tar.gz
Tar zcf/home/backup/$MySQLBakName/var/lib/mysql
/etc/init.d/mysqld start

#备份网站
webbakname=web_$ (date +%y%m%d). Tar
Tar cf/home/backup/$WebBakName/home/www

#操作FTP
oldmysql=mysql_$ (date-d -1day + "%y%m%d"). tar.gz
oldweb=web_$ (date-d -1day + "%y%m%d"). Tar

cd/home/backup/
/USR/BIN/FTP-V-N $FTP _ip << EOF
User $FTP _user $FTP _pass
Type binary
Put $MySQLBakName
Put $WebBakName
Delete $OldMySQL
Delete $OldWeb
Bye
Eof

rm-rf/home/backup/*.tar*

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.