To avoid files started with “.” in find result
find . -name ‘.snapshot’ -prune -o -name ‘file.mp4’
To delete files older than 7*24h
find /var/tmp -mtime +7 -exec rm -f {} \;
To avoid files started with “.” in find result
find . -name ‘.snapshot’ -prune -o -name ‘file.mp4’
To delete files older than 7*24h
find /var/tmp -mtime +7 -exec rm -f {} \;
1. dump all database into backup file
mysqldump –opt –all-databases > /tmp/databkup
(–opt includes lock table option)
2. dump several databases
mysqldump –opt –databases db1 db2 db3 > /tmp/databkup
3. dump single table
mysqldump –opt db1 table1 > /tmp/databkup
Touble shooting
Error: Access denied for user x@y to database z when using lock tables
mysqldump -u username -p database –single-transaction >dump.sql
User tar to back up db
mysql> FLUSH TABLES WITH READ LOCK;
(This will lock the db so it won’t be updated by other threads. Write threads by other process will wait till the lock is released)
#tar cvzf /var/tmp/datadump.tgz /var/lib/mysql/*
mysql> UNLOCK TABLES;
Connect and Close connection to DB
$DB_HOST = “localhost”;
$DB_NAME = “mysql”;
$DB_USER = “mysql”;
$DB_PASS = “password”;
function conn_db(){
global $DB_HOST;
global $DB_USER;
global $DB_PASS;
global $DB_NAME;
if(!($conn = mysql_connect($DB_HOST, $DB_USER, $DB_PASS))) {
echo “Failed to connect DB”;
echo mysql_error($conn);
die;
}
if(!(mysql_select_db($DB_NAME))) {
echo “Failed to select DB”;
echo mysql_error($conn);
die;
}
return $conn;
}
function close_db($conn){
mysql_close($conn);
}
$conn = conn_db();
$sql = “select * from users where date>’20081001′ order by id”;
if (!($rs=mysql_query($sql))) {
echo mysql_error($conn);
die;
}
while ($row = mysql_fetch_array($rs)) {
echo “$row[0] $row[1]\n”;
}
Some old data were missingon db slave server
copy data from master db01.lalife.net to slave db02.lalife.net and reset slave
On Master:
Lock table
mysql> FLUSH TABLES WITH READ LOCK;
mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| db01-bin.000901 | 84861692 | | |
+——————+———-+————–+——————+
cd /var/lib
tar cvfz TARFILE_NAME.tgz mysql/
mysql > unlock tables;
On Slave:
cd /var/tmp/
tar -zxvf db01_bk.tar.gz
service mysql stop
##backup old db
mv /www/mysql /www/mysql0603
mv /var/tmp/www/mysql/ /www/
service mysql start
mysql>STOP SLAVE;
mysql>RESET SLAVE;
mysql> CHANGE MASTER TO MASTER_HOST=’db001.lalife.net’, MASTER_USER=’replication’, MASTER_PASSWORD=’xxxxx’, MASTER_LOG_FILE=’db01-bin.000900′, MASTER_LOG_POS=13482116;
mysql>START SLAVE;
How to get master bin file and position:
On master server
mysql> show master status;
+——————+———–+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———–+————–+——————+
| db01-bin.000900 | 437292441 | | |
+——————+———–+————–+——————+
Setup keys for xxx to login yyy without password
[xxx-sync@xxx ~]$ ssh-keygen -t dsa
cp id_dsa.pub to xxx /home/xxx-sync/.ssh