#!/usr/bin/perl -w # Backup all mysql databases to a date-stamped tarball. use strict; use POSIX qw(strftime); # My source and destination for backups. my $backupname = shift(); my $backupsource = shift(); my @options = @ARGV; ($backupname) = `/bin/hostname` if not $backupname; chomp $backupname; $backupname .= ".sql"; # Setup my backup file prefix and extension. my $backupprefix = sprintf('%s-%s', $backupname, strftime("%Y-%m-%d", localtime)); my $backupextension = ".gz"; # Setup my default backup file name. my $backupfile = $backupprefix . $backupextension; # If my desired backup file already exists, keep appending a "-n" # to the file name until we find a file that doesn't exist and use # that file name. my $fileindex = 0; while(-e "$backupfile") { # Increment my index. $fileindex++; # Build my new file name. $backupfile = $backupprefix . '-' . $fileindex . $backupextension; } # Make a tarball. system "mysqldump --all-databases -u root -p | gzip > $backupfile";