Shell simple full-backup mysql script case [root @ cacti mysql] # cat backup. sh #! /Bin/sh www.2cto.com # created by taiyang #2012-9-16 # function: backup mysql database # set local variablesDBname = backupBackupPath =/mysqlbak/DumpFile =$ {BackupPath }$ {DBname} 'date + % y % m % d _ % H % M % s '. bak v_username = 'root' if [$ v_username! = 'Root']; thenecho "ERROR: the script must be run as mysql" exit 1fi www.2cto.com echo "backup database path: $ DumpFile "echo" ---------------------------- "dateecho" *************************** "echo" backup database begin "echo" *************************** "mysqldump-uroot-proot -- opt $ DBname> $ DumpFile echo "***************************" echo" backup database end "echo" ************************** "date # delete backups before 5 daysfind $ BackupPath-ctime + 5-name '*. bak '-exec rm {}\; [root @ cacti mysql] # www.2cto.com restores the database information from the backup script;
[Root @ cacti mysqlbak] # lsbackup120928_114307.bak backup120928_114556.bak cacti120928_105558.bakbackup120928_114309.bak cacti120928_110443.bakbackup120928_114310.bak cacti120928_110443.bakbackup120928_114310.bak cacti120928_105155.bak cacti120928_110448.bakbackup120928_114311.bak cacti120928_110449.bak cacti120928_110449.bak [root @ cacti mysqlbak] # pwd/mysqlbak [root @ cacti mysqlbak] # [root @ cacti ~] # Mysql-hlocalhost-uroot-proot backup </mysqlbak/backup120928_114556.bak
[Root @ cacti ~] # Mysql-uroot-prootWelcome to the MySQL monitor. commands end with; or \ g. your MySQL connection id is 860 Server version: 5.0.22-log Type 'help; 'or' \ H' for help. type '\ C' to clear the buffer. www.2cto.com mysql> use backupDatabase changedmysql> show tables; + ------------------ + | Tables_in_backup | + tables + | B | t | + ---------------- + 2 rows in set (0.00 sec) mysql> exitBye