Importing & Exporting

=Intro=

Are you managing a host with a website on it? Or multiple hosts? If you have to copy all the content and database to your local machine daily or frequently, it can be a tedious job. The small script below automates it to make it easier for you. This can be useful to moves sites from one host to another as well.

=Features=


 * Automates dumping the MySQL database
 * Automates compressing all the project to one archive
 * Optionally, exclude some files or folders from the archive

=Directions=


 * First of all, get the information you need for mysql and the project name from the settings.php.


 * Copy and Save Script 1 below as dobak.sh (or whatever name) in the home directory, outside the htdocs/ folder. When you connect to a host, you're automatically in the home folder, with other folders shown in there such as www or htdocs for your website, logs for your logs, etc...


 * Make the script executable: chmod a+x dobak.sh


 * Now everytime you SSH into your server, just execute the script: sh dobak.sh, and it'll dump the sql into the htdocs/ folder, create a .tbz archive of it outside the htdocs/ folder, and delete the sql file afterwards for security reasons.

=Explanation=

Note: Yes i know you can see the password in the mysql line, but If you really think it's insecure, remove it from the script by just having -p instead of -pmypassword and manually enter the password instead. I've done that in Script 2 for you. Notice the difference.

Finally, If you did remove the password and wish to add it manually, It can be tedious to remember all the passwords for every website you have. An easy thing is to print the password automatically for you. Script 3 does that by adding this line as the first line. grep mysqli htdocs/sites/default/settings.php where settings.php is your settings file that has the MySQL line. The above example was for Drupal's CMS.

All 3 scripts are more or less identical.

=Script=

Script 1

 * 1) !/bin/bash

cd htdocs mysqldump -h 75.11.18.131 -u myusername -pmypassword mydatabase > `date +%Y%m%d`-project.sql
 * 1) enter the htdocs directory and dump the database there

cd .. rm tar-exclude ls -R htdocs/bk/ > tar-exclude tar cjf `date +%Y%m%d`-jcb.tbz htdocs/ -X tar-exclude

rm htdocs/`date +%Y%m%d`-jcb.sql

Script 2

 * 1) !/bin/bash

cd htdocs mysqldump -h 75.11.18.131 -u myusername -p mydatabase > `date +%Y%m%d`-project.sql
 * 1) enter the htdocs directory and dump the database there

cd .. rm tar-exclude ls -R htdocs/bk/ > tar-exclude tar cjf `date +%Y%m%d`-jcb.tbz htdocs/ -X tar-exclude

rm htdocs/`date +%Y%m%d`-jcb.sql

Script 3

 * 1) !/bin/bash

grep mysql htdocs/sites/default/settings.php
 * 1) print the MySQL line from the settings file that has the username/password details

cd htdocs mysqldump -h 75.11.18.131 -u myusername -p mydatabase > `date +%Y%m%d`-project.sql
 * 1) enter the htdocs directory and dump the database there

cd .. rm tar-exclude ls -R htdocs/bk/ > tar-exclude tar cjf `date +%Y%m%d`-jcb.tbz htdocs/ -X tar-exclude

rm htdocs/`date +%Y%m%d`-jcb.sql