Ok well we decided months ago that making sure we not only have our live servers backed up properly, part of the hosting plan, that we should also make sure our development servers get the same treatment. While it wouldn’t be too much of a catastrophe if we went down the amount of time spent would out weight the amount of time spent just backing it up. So we set to writing simple scripts to backup what we needed and then scp the files over to a warm backup that we have off site. Call us paranoid if you want but we call it smart.
So here is a script that we use to backup all our databases. I recommend putting it in your users home in a directory and setting a cron to run nightly. It erases everything exactly one week older than the current date so you it is definitely set to run a specific way but changing it around to do other things would not be hard at all. I tried to layout a config section since I would be putting this one script up on the blog in case anyone ever needed something similar.
Before using it make sure to backup your database manually once and test it out because I am in no way responsible for misuse of it or you messing your own game up cause you didn’t read my post. No givsey backsies in other words.
#!/usr/bin/env ruby ############################################################################### # Configuration section, we set a few constants here and use them throughout # the file. Do not edit the other sections unless you can read very basic # code. ############################################################################### # MySQL Username USERNAME = 'user' # MySQL Password PASSWORD = 'password' # Get times starting with current time NOW = Time.now.strftime("%m-%d-%Y") # As far back as we are going to backup in seconds I believe because we all know # that (3600*24)*7=604800 or at least all the poor CS bastards know. BACKUP_END = (Time.now - 604800).strftime("%m-%d-%Y") # Filename to create only change the sqldump part. This could use two variables # but I see no point I only made this config section to simplify but there is a # limit, Rocky Dennis FILENAME = "sqldump-#{NOW}.sql" # Filename to remove, same as above just make sure it matches the same name # though or the cleanup won't really work. REMOVE_FILENAME = "sqldump-#{BACKUP_END}.sql" ############################################################################### # Here we will dump the database. I will include line comments above each # command to explain what it's purpose is. ############################################################################### # The command that we want to start with to dump the databases command = "mysqldump -u #{USERNAME} --password=#{PASSWORD} --all-databases >> #{FILENAME}" # Run the command that we just built with our config constants system(command) ############################################################################### # Here we will check to see if a file exists that is older than one week by the # variable set earlier. If it exists then we will remove it. You could be crea- # tive here and add it to a tar and then when you have 7 or so then GZip them # creating an incremental backup of everything. ############################################################################### # Check if file exists and then remove it if it does if File.exists?(REMOVE_FILENAME) command = "rm #{removeFile}" system(command) end ############################################################################### # This is optional, if you run some sort of offsite backups and are sending # this to that location then you would want to scp them over. This will also # only work if you have setup keys to allow you login over ssh without typing # a password. It is easy to setup and there are many tutorials. Just google for # "ssh without password" Again you could make this use the config section but # I figured most people wouldn't really use this part. ############################################################################### command = "scp #{FILENAME} user@192.168.1.1:SqlBackups/#{FILENAME}" # system(command)




3 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
No givsey backsies!
That was really nice…
Hope you don’t mind but I will definitely archive this for use later.
No, not at all. It can definitely be improved some so feel free to use it however you want.
Thanks
Tom