Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i was doing my script..i think that it was all correct but seems that an error when was trying running the script saying bash: bla..bla.:command not found.
It would help if you told us which cmd it complained about ie show the 'exact' error msg.
Anyway, this line
FILE= mysql-$db.$NOW
I'd change to
FILE=mysql-${db}.$NOW
note the removal of leading space, that is wrong for bash assignments, also, disambiguation of $db var when interpolated.
To check exactly what's happening, insert this at the top of your script:
for db in $DBS
do
FILE= mysql-${db}.$NOW
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
+ for db in '$DBS'
+ FILE=
+ mysql-information_schema.2008-07-15_16h24
./test2.sh: line 17:
mysql-information_schema.2008-07-15_16h24: command not found
+ /usr/bin/mysqldump -u root -h localhost -ppassword information_schema
+ /usr/bin/gzip -9
./test2.sh: line 18: 1: ambiguous redirect
mysqldump: Got errno 32 on write
A quicker way, and less scripting to backup mysql is to backup the mysql folder that contains the databases, depending on which distro you are using, it may be under /var/lib/mysql, something like this will work fine.
Code:
#!/bin/sh
tar -czpf mysql-backup.tar.gz /var/lib/mysql
This will preserve all database permissions as well, then if you need to restore just use:
Code:
cd / && tar -zxf mysql-backup.tar.gz
or a specific database, say test
cd / && tar -zxf mysql-backup.tar.gz var/lib/mysql/test
A quicker way, and less scripting to backup mysql is to backup the mysql folder that contains the databases, depending on which distro you are using, it may be under /var/lib/mysql, something like this will work fine.
Code:
#!/bin/sh
tar -czpf mysql-backup.tar.gz /var/lib/mysql
This will preserve all database permissions as well, then if you need to restore just use:
Code:
cd / && tar -zxf mysql-backup.tar.gz
or a specific database, say test
cd / && tar -zxf mysql-backup.tar.gz var/lib/mysql/test
This is not reliable. If a database or table was getting written to at the time of backup, it could produce unreliable results. The only way to grab backups this way is to either use snapshots or shutdown MySQL, take the backup, then bring it back online. I don't recommend this on a live database, unless you can shut it down.
With mysqldump, you can lock the tables during the dump.
As for the error, try putting back ticks around the Variables that actually run commands.
Try this:
Code:
#!/bin/sh
# Date and File Info
NOW=`$(date +%Y-%m-%d_%Hh%M)`
FILE="mysql-$db.$NOW"
# Connection to MySQL Info
MUSER="root"
MPASS="password"
MHOST="localhost"
# Commands
MYSQL=`$(which mysql)`
MYSQLDUMP=`$(which mysqldump)`
GZIP=`$(which gzip)`
# Connection string to list Databases
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
# Run the MySQL dumps for each Database
for db in $DBS
do
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE.sql
done
The way I backup MySQL databases is to set up replication (this can even be done on the same machine), stop the slave, backup the slave, then restart the slave. I use tar to backup the data directory.
Here are the two files I use to backup mysql server.
The first one, dblist.sh goes into the database server and list all the databases there.
Then the second one takes the output of dblist.sh and backups the databases listed by dblist.sh
Just run dblist.sh first, and then dbbackups.sh
Call this file: dblist.sh
#!/bin/bash
DBPASS='mysqlpassword'
SQL='show databases;'
echo $SQL | mysql -u root --pass=$DBPASS > /opt/backup/192.168.1.123/full-backups/db-list.txt
Call this file: db-backups.sh
#!/bin/bash
# Set a value that we can use for a datestamp
DATE=`date +%Y-%m-%d`
# Our Base backup directory
BASEBACKUP="/opt/backup/192.168.1.123/full-backups/databases"
DBPASS="mysqlpassowrd"
for DATABASE in `cat /opt/backup/192.168.1.123/full-backups/db-list.txt`
do
# This is where we throw our backups.
FILEDIR="$BASEBACKUP/$DATABASE"
# Test to see if our backup directory exists.
# If not, create it.
if [ ! -d $FILEDIR ]
then
mkdir -p $FILEDIR
fi
my idea to back up mysql database is like this.i just want to play it around in my local only.
1-i tar the mysql folder in the var/lib/mysql,the name of the tar is todays date.
2-i move the tar file into the my site in http://localhost/backup
3-then my other linux machine which have other script that wget http://localhost/backup/today.tgz
how do i know the file that the other machine download completely the tar file.how do i checked it?!sorry if my explanation are not easy to understand.
my idea to back up mysql database is like this.i just want to play it around in my local only.
1-i tar the mysql folder in the var/lib/mysql,the name of the tar is todays date.
2-i move the tar file into the my site in http://localhost/backup
3-then my other linux machine which have other script that wget http://localhost/backup/today.tgz
how do i know the file that the other machine download completely the tar file.how do i checked it?!sorry if my explanation are not easy to understand.
Remember, if you want to backup doing a cold type backup by just tarballing the /var/lib/mysql files and directories, you need to shutdown mysql, then back these up, then restart mysql.
As for checking if the other machine is done, well, you'll probably have to write a script to check and see if there is a process that is moving the file, or copying, however it's being run or copied over.
can you give me a guide on how to check whether the file is download completely?!for example the during the transferring tar file somehow occur a problem. how do it face the problem?!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.