-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_database.rb
More file actions
executable file
·25 lines (19 loc) · 896 Bytes
/
backup_database.rb
File metadata and controls
executable file
·25 lines (19 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env ruby
######## Back up last 3 days or more databases file ##########
today = Time.now.strftime("%Y-%m-%d")
# change keep_file_number if you want to keep more backup versions
keep_file_number = 3
old_date = (Time.now-(keep_file_number*24*3600)).strftime("%Y-%m-%d")
backup_dir = "#{Dir.home}/sqldump"
Dir.mkdir(backup_dir) unless Dir.exist?(backup_dir)
db_config = {
:username=>'mysql user name',
:password=>'mysql password',
:host=>'mysql host',
:database=>'the database you want to backup'
}
options = "--skip-extended-insert --hex-blob --lock-tables=false"
backup_file = "#{backup_dir}/#{db_config[:database]}_#{today}.sql"
`mysqldump -h#{db_config[:host]} -u#{db_config[:username]} -p#{db_config[:password]} #{options} #{db_config[:database]} | gzip -c > #{backup_file}.gz`
old_file = "#{backup_dir}/#{db_config[:database]}_#{old_date}.sql.gz"
`rm -f #{old_file}`