一个简单的脚本 自己做个笔记 以后直接copy就好了 又变懒了
#!/bin/bash set -x today=`date +%Y%m%d%H%M` mkdir -p /data/backup/mysql/$today /usr/local/mysql-5.1.60/bin/mysqldump -uroot -p'passwd' -h127.0.0.1 --master-data=2 --all-database > /data/backup/mysql/$today/backupdb.sql
find /Data/mysqlbackup/mysql -type f -mtime +5 -exec rm {} \;
50 1 * * * /data/shell/mysqlback.sh
下面是借用 dngood的博客
备份svn 数据,分两个脚本
svn_data.sh 用于备份svn 数据到,远程rsync 服务器
mysql_bak.sh 用于备份mysql 数据库到,远程rsync 服务器
- #!/bin/bash
- #backup /u01/svndata/
- #20110909 by dongnan
- #variables
- rsync=/usr/bin/rsync
- rsync_add=192.168.57.82
- svn_data_dir=/u01/svndata
- mysql_data_dir=/data/mysql
- l_time=`date +'%Y-%m-%d %H:%M:%S'`
- today_time=`date +'%Y-%m-%d'`
- log_dir=/root/sh/log
- #add for backup mysql
- /root/sh/mysql_bak.sh || echo "$l_time mysql backup error!" >> /root/sh/log/mysql_bak.err
- sleep 30
- #rsync
- for var in $svn_data_dir $mysql_data_dir ;do
- #echo $var && continue
- now_time=`date +"%F_%T"`
- if ! $rsync -avz --delete --password-file=/root/sh/pw "$var" dongnan@${rsync_add}::svn_data/ 2>>$log_dir/svn_err.txt;then
- echo $now_time >> $log_dir/svn_err.txt
- echo "#####" >> $log_dir/svn_err.txt
- fi
- done
cat /root/sh/mysql_bak.sh
- #!/bin/bash
- #backup mysql data
- #20111012 by dongnan
- #variables
- dump=/usr/bin/mysqldump
- mysql=/usr/local/bin/mysql
- option='-uroot -pdongnan --opt -e --master-data=2'
- databases=$(mysql -uroot -pdongnan -e 'show databases;' | awk '$1 !~ /Database|information_schema|test/ {print $1}')
- bak_dir=/data/
- l_time=`date +'%Y-%m-%d %H:%M:%S'`
- now_time=`date +'%Y-%m-%d'`
- expires_time=`date +"%Y%m%d" --date='4 days ago'`
- log=/root/sh/log/mysql_bak.err
- date_dir=$(date +'%Y%m%d')
- #delete old file (4days)
- find "$bak_dir" -name "${expires_time}" -type d | xargs rm -rf
- #date dir
- test -e ${bak_dir}${date_dir} || mkdir -p ${bak_dir}${date_dir}
- #backup and gzip database.sql
- for database in $databases;do
- $dump $option $database | gzip > ${bak_dir}${date_dir}/${now_time}-${database}.sql.gz || echo "$l_time $database backup error!!" >> $log
- done
gzip -dc 2012-06-01-uspace_uchome.sql.gz | mysql -uroot -pdongnan uspace_uchome
备份MySQL数据库的命令
mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql
备份MySQL数据库为带删除表的格式 备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库。
mysqldump -–add-drop-table -uusername -ppassword databasename > backupfile.sql
直接将MySQL数据库压缩备份
mysqldump -hhostname -uusername -ppassword databasename | gzip > backupfile.sql.gz
备份MySQL数据库某个(些)表
mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 > backupfile.sql
同时备份多个MySQL数据库
mysqldump -hhostname -uusername -ppassword –databases databasename1 databasename2 databasename3 > multibackupfile.sql
仅仅备份数据库结构
mysqldump –no-data –databases databasename1 databasename2 databasename3 > structurebackupfile.sql
备份服务器上所有数据库
mysqldump –all-databases > allbackupfile.sql
还原MySQL数据库的命令
mysql -hhostname -uusername -ppassword databasename < backupfile.sql
还原压缩的MySQL数据库
gunzip < backupfile.sql.gz | mysql -uusername -ppassword databasename
将数据库转移到新服务器
mysqldump -uusername -ppassword databasename | mysql –host=*.*.*.* -C databasename