Restore just the .sql files from multiple DirectAdmin tar.gz backups


Enter Your Query:
Use '%' for wildcards and quotes for "exact phrases"


Top Level » MySQL / MariaDB » Backup/Restore/Transfer

Restore just the .sql files from multiple DirectAdmin tar.gz backupsLast Modified: Jan 23, 2018, 10:05 pm
Say you have a large list of files in: /home/admin/admin_backups
and you wish to restore the databases from those backups, but not the rest of the data, you can use this script for a quick restore.
Note:
  1. This restore will not restore the mysql.user nor mysql.db tables. Only the username_db databases.
  2. This will overwrite any current tables in the existing databases.
  3. The DBs must already exist, as the .sql files do not contain the CREATE DATABASE commands, only drop/create tables, with the loading of the data.
  4. as it processes each tar.gz file, that tar.gz will be moved to the "done" folder, so you can run it multile times, or stop it ad resume later if needed.
A sample script that can do this, might look like this:

#!/bin/sh

P=/home/admin/admin_backups
DB_USER=da_admin
DB_PASS=12345

mkdir -p done

for i in `ls *.tar.gz`; do
{
       echo $i;
       cd $P
       mkdir -p temp
       cd temp

       tar xvzf ../$i backup/*.sql 2>/dev/null

       if [ -d backup ]; then
               ls -la backup
               cd backup
               for d in `ls *.sql`; do
               {
                       D_NAME=`echo $d | cut -d. -f1`
                       mysql -u${DB_USER} -p${DB_PASS} $D_NAME < $d
               };
               done;
       else
               cd $P
               echo "No .sql files"
       fi

       cd $P
       mv $i done
       rm -rf temp
};
done;
exit 0;

© 2018 JBMC Software, Suite 173  3-11 Bellerose Drive, St Albert, AB  T8N 1P7  Canada.  Mon-Fri 9AM-5PM MST