Difference between revisions of "Mysqldump"
Jump to navigation
Jump to search
(adds date) |
(improved file naming) |
||
Line 2: | Line 2: | ||
== Backup == | == Backup == | ||
+ | <code>cat ./backup.db.sh</code> | ||
<source lang="bash"> | <source lang="bash"> | ||
+ | #!/bin/sh | ||
+ | |||
DB=wiki | DB=wiki | ||
backupdir="$HOME/backups"; | backupdir="$HOME/backups"; | ||
Line 9: | Line 12: | ||
fi | fi | ||
backup="$backupdir/dump-$(date +%F).$(hostname)-$DB.sql"; | backup="$backupdir/dump-$(date +%F).$(hostname)-$DB.sql"; | ||
+ | # increment the filename if it already exists | ||
+ | # http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion | ||
+ | i=1 | ||
+ | filename=$(basename "$backup") # foo.txt | ||
+ | extension=${filename##*.} # .txt | ||
+ | file=${filename%.*} # foo | ||
+ | while [ -f $backup ]; do | ||
+ | backup="$backupdir/${file}.${i}.${extension}" | ||
+ | i=$(( i+1 )) # increments $i | ||
+ | done | ||
/usr/bin/mysqldump $DB > $backup; | /usr/bin/mysqldump $DB > $backup; | ||
ls -al $backup; | ls -al $backup; | ||
+ | |||
</source> | </source> | ||
Line 19: | Line 33: | ||
[[Category:Database]] | [[Category:Database]] | ||
+ | [[Category:Bash]] |
Revision as of 12:05, 11 December 2014
quick recipe on using mysqldump
Backup[edit | edit source]
cat ./backup.db.sh
#!/bin/sh
DB=wiki
backupdir="$HOME/backups";
if [ ! -d "$backupdir" ]; then
mkdir -p $backupdir;
fi
backup="$backupdir/dump-$(date +%F).$(hostname)-$DB.sql";
# increment the filename if it already exists
# http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion
i=1
filename=$(basename "$backup") # foo.txt
extension=${filename##*.} # .txt
file=${filename%.*} # foo
while [ -f $backup ]; do
backup="$backupdir/${file}.${i}.${extension}"
i=$(( i+1 )) # increments $i
done
/usr/bin/mysqldump $DB > $backup;
ls -al $backup;
Restore[edit | edit source]
mysql $DB < $backup