Difference between revisions of "Mysqldump"
Jump to navigation
Jump to search
(add single-transaction switch) |
(improved file naming) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | quick recipe on using mysqldump | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | == Backup == | ||
<code>cat ./backup.db.sh</code> | <code>cat ./backup.db.sh</code> | ||
<source lang="bash"> | <source lang="bash"> | ||
#!/bin/sh | #!/bin/sh | ||
− | |||
− | |||
− | |||
− | |||
− | + | DB=wiki | |
− | + | backupdir="$HOME/backups"; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | backupdir="/backups"; | ||
if [ ! -d "$backupdir" ]; then | if [ ! -d "$backupdir" ]; then | ||
− | mkdir -p | + | mkdir -p $backupdir; |
fi | 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 | |
− | |||
− | backup="$ | ||
− | # | ||
i=1 | i=1 | ||
− | filename=$(basename "$backup") # foo.txt | + | filename=$(basename "$backup") # foo.txt |
− | extension=${filename##*.} | + | extension=${filename##*.} # .txt |
− | file=${filename%.*} | + | file=${filename%.*} # foo |
− | |||
− | |||
while [ -f $backup ]; do | while [ -f $backup ]; do | ||
− | backup="$backupdir/${file}.$ | + | backup="$backupdir/${file}.${i}.${extension}" |
− | i=$(( i+1 )) | + | i=$(( i+1 )) # increments $i |
− | |||
done | done | ||
− | + | /usr/bin/mysqldump $DB > $backup; | |
− | + | ls -al $backup; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
Line 92: | Line 30: | ||
<source lang="bash"> | <source lang="bash"> | ||
mysql $DB < $backup | mysql $DB < $backup | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
[[Category:Database]] | [[Category:Database]] | ||
[[Category:Bash]] | [[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