How to skip specific tables with drush sql-dump?
Question
I have a drush config file ~/.drush/drushrc.php:
if (!isset($options['structure-tables']['common'])) {
$options['structure-tables']['common'] = array(
'cache', 'cache_*', 'history', 'search_*', 'sessions', 'watchdog'
);
}
$options['structure-tables']['common'] = array_merge($options['structure-tables']['common'],
array('ctools_css_cache', 'ctools_object_cache', 'logz', 'views_object_cache')
);
And I have a bash script file:
/usr/bin/drush sql-dump --root="/home/username/domains/sitename/www" --skip-tables-key="common" --gzip --result-file=/home/username/backup/$year/$month/dbname_$date_now_time.sql
But when I execute it the backup file still have data in cache tables. What am I doing wrong?
And second question is - If I put this into crontab do I need to put configuration file somewhere else?
I use Drush version 8.
Solution
Sorry I can't help you much with your first question ...
Your 2nd question asked:
And second question is - If I put this into crontab do I need to put configuration file somewhere else?
The drush documentation site links to drushrc.php example file that says where you may place the rc file:
https://raw.githubusercontent.com/drush-ops/drush/master/examples/example.drushrc.php
Rename this file to drushrc.php and optionally copy it to one of the places listed below in order of precedence:
- Drupal site folder (e.g. sites/{default|example.com}/drushrc.php).
- Drupal /drush and sites/all/drush folders, or the /drush folder in the directory above the Drupal root.
- In any location, as specified by the --config (-c) option.
- User's .drush folder (i.e. ~/.drush/drushrc.php).
- System wide configuration folder (e.g. /etc/drush/drushrc.php).
- Drush installation folder.
For Cron the documentation recommends that Drush be configured to run as the same user that runs your webserver. That being said I recommend you place your rc file in location (5), a system-wide configuration folder. Yes, more localized Drush rc files will then need to override your system-wide configuration file as needed (oh well).
OTHER TIPS
Dump all tables except the cache
table and tables starting with cache_
.
drush sql-dump --skip-tables-list=cache,cache_* > dumpfile.sql
Database structure only. No data at all.
drush sql-dump --extra=--no-data > dumpfile.sql
Data only. No cache data and no cache structure.
drush sql-dump --skip-tables-list=cache,cache_* --data-only > dumpfile.sql
No cache data but dump in their structures.
drush sql-dump --structure-tables-list=cache,cache_* > dumpfile.sql
For some reason cache_* did not work for me with drush 9+ until I omitted the underscore. However, this may be specific to my (server) bash/terminal. What worked for me:
drush sql-dump --skip-tables-list=cache*
and you can always add the --verbose
to see the actual mysql dump and it's --ignore parameter.