1
0

db_bk.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. . ../options.conf
  11. . ../include/check_dir.sh
  12. DBname=$1
  13. LogFile=${backup_dir}/db.log
  14. DumpFile=${backup_dir}/DB_${DBname}_$(date +%Y%m%d_%H%M%S).sql
  15. NewFile=${backup_dir}/DB_${DBname}_$(date +%Y%m%d_%H%M%S).tgz
  16. OldFile=${backup_dir}/DB_${DBname}_$(date +%Y%m%d --date="${expired_days} days ago")*.tgz
  17. [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
  18. DB_tmp=`${db_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "show databases\G" | grep ${DBname}`
  19. [ -z "${DB_tmp}" ] && { echo "[${DBname}] not exist" >> ${LogFile} ; exit 1 ; }
  20. if [ -n "`ls ${OldFile} 2>/dev/null`" ]; then
  21. rm -f ${OldFile}
  22. echo "[${OldFile}] Delete Old File Success" >> ${LogFile}
  23. else
  24. echo "[${OldFile}] Delete Old Backup File" >> ${LogFile}
  25. fi
  26. if [ -e "${NewFile}" ]; then
  27. echo "[${NewFile}] The Backup File is exists, Can't Backup" >> ${LogFile}
  28. else
  29. ${db_install_dir}/bin/mysqldump -uroot -p${dbrootpwd} --databases ${DBname} > ${DumpFile}
  30. pushd ${backup_dir} > /dev/null
  31. tar czf ${NewFile} ${DumpFile##*/} >> ${LogFile} 2>&1
  32. echo "[${NewFile}] Backup success ">> ${LogFile}
  33. rm -f ${DumpFile}
  34. popd > /dev/null
  35. fi