Skip to content

Instantly share code, notes, and snippets.

@caok
Last active October 12, 2015 17:58
Show Gist options
  • Save caok/4065767 to your computer and use it in GitHub Desktop.
Save caok/4065767 to your computer and use it in GitHub Desktop.
mysql数据库备份crontab(10 3 * * * sh backup_mysql.sh) 数据导入: mysql -u username -p project_name < mysql-2012-12-17.sql
#!/bin/bash
HOST=127.0.0.1
USER=root
PASS=root
# DB="db1 db2 ..."
DB="erp"
DATE=$(date "+%Y-%m-%d")
BACKDIR=/home/rails/backup
KEEP="30 days"
# create dir if not exists
if ! [ -d $BACKDIR ]; then
mkdir $BACKDIR
fi
# dump database
cd $BACKDIR
mysqldump -h$HOST -u$USER -p$PASS $DB | gzip > mysql-$DATE.sql.gz
# remove old files
rm -rf /home/rails/backup/mysql-`date --date="$KEEP ago" "+%Y-%m-%d"`.sql.*
# for create test file
# ruby -rubygems -e 'require "active_support/all"; 31.times.each {|i| `touch mysql-#{i.days.ago.to_date.to_s :db}.sql.tgz`}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment