備份說明: 1.所有的資料庫都要備份
                  2. 七天一個循環
                  3.透過NFS的方式連線,備份完成後自動斷線
 IP位置說明: 192.168.3.212是 NFS的主機位置
                       備份的目錄是DBbackup
                        底下子目錄需要自己去建立(只要一次0~6)
                       0->>周日
                       1->>周一
                       2->>周二
                       3->>週三
                       4->>周四
                       5->>周五
                       6->>周六
#!/bin/bash
#BACKUP ALL MYSQL FILES TO NAS:192.168.3.212
#NAS:/DBbackup

#-----------------------------------------------------------
echo "start time ------------------" >> /home/linkliu/backuplog.txt
date >>/home/linkliu/backuplog.txt
#-----------------------------------------------------------
db_user="root"
db_passwd="vcqdru6u!@#map"
db_host="localhost"
time="$(date +"%d-%m-%Y")"
backup_dir="/home/nfs"
BACKUPWEEK="$(date +"%w")"
LOGPATH="/etc/link.shell/log"
SERVERIP="192.168.3.212"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
MKDIR="$(which mkdir)"
RM="$(which rm)"
MV="$(which mv)"
GZIP="$(which gzip)"
echo "JAIO MSQL BACKUP SHELL SCRIPT + $BACKUPWEEK"
#---------------------------------------------------------
if [ "$BACKUPWEEK" == 0 ];
   then
    echo "Today is Sunday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
             $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
           #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
           #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
           #$LOGPATH/Wiki_backup_log.txt
          umount /home/nfs
     fi
fi
#---------------------------------------------------------

#---------------------------------------------------------
if [ "$BACKUPWEEK" == 1 ];
   then
    echo "Today is Monday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
             $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
           #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
           #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
           #$LOGPATH/Wiki_backup_log.txt
         umount /home/nfs
     fi
fi
#---------------------------------------------------------
#---------------------------------------------------------
if [ "$BACKUPWEEK" == 2 ];
   then
    echo "Today is Tuesday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
             $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
           #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
           #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
           #$LOGPATH/Wiki_backup_log.txt
          umount /home/nfs
     fi
fi
#---------------------------------------------------------
#---------------------------------------------------------
if [ "$BACKUPWEEK" == 3 ];
   then
    echo "Today is Wendesday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
            $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
           #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
           #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
           #$LOGPATH/Wiki_backup_log.txt
          umount /home/nfs
     fi
fi
#---------------------------------------------------------
#---------------------------------------------------------
if [ "$BACKUPWEEK" == 4 ];
   then
    echo "Today is Thursday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
             $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
            #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
            #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
            #$LOGPATH/Wiki_backup_log.txt
           umount /home/nfs
     fi
fi
#---------------------------------------------------------
#---------------------------------------------------------
if [ "$BACKUPWEEK" == 5 ];
   then
    echo "Today is Firday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
            $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
           #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
           #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
           #$LOGPATH/Wiki_backup_log.txt
          umount /home/nfs
     fi
fi
#---------------------------------------------------------
#---------------------------------------------------------
if [ "$BACKUPWEEK" == 6 ];
   then
    echo "Today is Staurday"

     if  mount -t nfs $SERVERIP:/DBbackup /home/nfs
         then
         echo "RM FILE............"
         rm -rf /home/nfs/$BACKUPWEEK/*
         echo "SQL Backup ......."
         # get all databases
         all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

         for db in $all_db
          do
            #$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP > "$backup_dir/$BACKUPWEEK/$time.$db.gz"
             $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db > "$backup_dir/$BACKUPWEEK/$time.$db.sql"
          done
           #mysqldump -u root -pvcqdru6u228 wiki > /home/nfs_link/wiki/$BACKUPWEEK/wiki.sql \
           #&& umount /home/nfs_link && echo "Backup $(date +"%Y-%m-%d") OK " >> \
           #$LOGPATH/Wiki_backup_log.txt
         umount /home/nfs
     fi
fi
#---------------------------------------------------------
echo "end backup time ------------------" >> /home/linkliu/backuplog.txt
date >>/home/linkliu/backuplog.txt
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" >> /home/linkliu/backuplog.txt
#---------------------------------------------------------