Easy Round Robin Scheduled Database Backup on Linux


We backup files/data a lot. Usually, we save the backups to a folder regularly (e.g. by a crontab job that runs at a specified interval). How do you keep a maximum number of backups without having another job that cleans every few days? You could also keep every copies (file name appended with a timestamp), and then you have another cron job that runs occasionally (e..g every month) to delete the oldest copies. If your web space is limited, then you have to clean up spaces every now and then, manually or automatically.

The idea is to save data to files with specified time pattern, either by hour, minute, or by day, week etc. For example, if you have a script (BASH) like this:

1
2
H=`date +"%H"`
ls > backup_$H  # your backup command (e.g. mysqldump)
H=`date +"%H"`
ls > backup_$H  # your backup command (e.g. mysqldump)

and sets a crontab every hour, so you will have at most 24 backups. If you schedule it every 2 hours and then you will have at most 12 backups. You can always sort the backups by timestamp in descending order. The backup names are generated and overwritten: backup_0, backup_1 … backup_23. 24 files for last 24 hours (hourly backup). The new backup will automatically overwrites to the one that was generated 24 hours ago.

The same idea can be applied to having a round robin scheduled backup every day and keep the last 7 days copies. The date format string is:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 20)
  %d   day of month (e.g., 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour, space padded ( 0..23); same as %_H
  %l   hour, space padded ( 1..12); same as %_I
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric time zone (e.g., -0400)
  %:z  +hh:mm numeric time zone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

So, if you want to keep the last 30 days copies (a copy every day), then you should have a variable like this:

H=`date +"%e"`

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
633 words
Last Post: Enter the Command Shell Prompt with Specified Directory on Windows (Registry)
Next Post: How to Ban Specified IPs in Apache2 Server?

The Permanent URL is: Easy Round Robin Scheduled Database Backup on Linux

Leave a Reply