- List all users in the current MySQL server:
mysql> SELECT User, Host FROM mysql.user;
- Remove a user from the server:
mysql> DROP USER '<User>'@'<Host>';
- Remove all binary logs:
mysql> RESET MASTER;
- Delete all binary logs but keep a week worth of logs:
mysql --verbose --execute="PURGE BINARY LOGS BEFORE '`date +"%Y-%m-%d" -d last-week`';"
And if you put this in a cron-tab, don’t forget to escape percents:
mysql --verbose --execute="PURGE BINARY LOGS BEFORE '`date +\%Y-\%m-\%d -d last-week`';"
- Check, auto-repair and optimize all databases:
mysqlcheck --auto-repair --optimize --all-databases
- Export a database:
mysqldump -u my_user "my-database" > data.sql
- Here is a
cron-able command to restart a MySQL service if no process found active:[ `ps axu | grep -v "grep" | grep --count "mysql"` -le 0 ] && /etc/init.d/mysql restart
- Monitor the queries being run (source):
watch -n 1 mysqladmin --user=XXXXX --password=XXXXX processlist
- Get the list of default configuration parameters the server will use regardless of the values set in config files (source):
mysqld --no-defaults --verbose --help
- Migrate all tables of all databases from MyISAM to InnoDB:
mysql --skip-column-names --silent --raw --execute="SELECT CONCAT(table_schema , '.', table_name) FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE' AND engine='MyISAM';" | xargs -I '{}' mysql --verbose --execute="ALTER TABLE {} ENGINE=InnoDB;"
Tag Archives: CLI
Git commands
- Get a clean local copy of my GitHub repository with read & write access:
git clone git@github.com:kdeldycke/scripts.git
- Switch to another branch:
git checkout another_branch
- Set the current repository in the state it was at commit
1234567:git checkout 1234567
- Get the current commit number:
git rev-parse HEAD
- Get a sorted list of all commit IDs:
git rev-list --all --pretty=oneline | cut -d ' ' -f 1 | sort
- Print a nice graph of your commits sorted by date:
git log --graph --all --pretty=oneline --abbrev-commit --date-order
- Destroy all your local changes and get back a sane repository:
git reset --hard
- Send local repository modifications to remote one:
git push origin
- Attach a tag to a given commit:
git tag "1.2.3" 8fe2934d1552c97246836987f0ea08e10ba749ae
- Publish all tags to the remote repository:
git push --tags
- Add a remote repository located on GitHub as a submodule in the
./folder/project-copyfolder:git submodule add https://github.com/my-id/project.git ./folder/project-copy
- While playing with backups of a local repository, you may encounter this error:
Cannot rewrite branch(es) with a dirty working directory.
In this case, you can get back a clean repository by removing all the unstaged changes:
git stash
Moving a WordPress blog to another domain
I provide hosting for free to some of my friends. One of them, QPX, had a side project called Lich’ti. But the latter is no longer active, so he decided to not renew the lich-ti.fr domain.
If Lich’ti’s domain name is dead, QPX’s personal blog is not. His website is powered by WordPress and was available at http://qpx.lich-ti.fr. My job is now to move it to http://qpx.coolcavemen.com. In this post, I’ll tell you how I’ve done it.
Before going further, backup everything, and be ready to revert back to your original situation at any moment ! What works for me will not necessary works for you…
To play nice with your visitors, you can setup a temporary maintenance page while we’re performing the migration.
Let’s start the migration by replacing, in the files served by Apache, all occurrences of the old domain name by the new one:
find /var/www/qpx-blog -mount -print -type f -exec sed -i 's/qpx.lich-ti.fr/qpx.coolcavemen.com/g' "{}" \;
If you have doubts about the efficiency of the command above, you can check the presence of the string we’re looking to replace via this command:
grep -RIi "qpx.lich-ti.fr" ./*
Then, we dump the database containing all WordPress content and config to a local file (the command will prompt for password):
mysqldump -p --host=localhost --port=3306 --user=root --opt --databases "qpx_blog" > qpx_dump.sql
And we replace all strings of the old domain by the new one:
sed 's/qpx.lich-ti.fr/qpx.coolcavemen.com/g' qpx_dump.sql > new_qpx.sql
Finally, we re-inject the modified database content after clearing the original:
mysql -p --host=localhost --port=3306 --user=root --execute='DROP DATABASE `qpx_blog`;' mysql -p --host=localhost --port=3306 --user=root < new_qpx.sql
Now you can disable the maintenance page and test the blog to check nothing’s broken.
Again, to play nice with your visitors (and search engines), you can redirect old URLs to the new domain, with apache directives similar to this one:
<VirtualHost *:80> ServerName qpx.lich-ti.fr RedirectMatch permanent (.*) http://qpx.coolcavemen.com$1 </VirtualHost>
dmg2img for Mandriva 2009.1
I’ve just created a RPM of dmg2img for the x86_64 version of Mandriva 2009.1. The package is available in my RPM repository.
dmg2img is a command line utility to extract the content of an Apple Disk Image .dmg file. Here is how I use it to access content:
dmg2img ./my-package.dmg mount -t hfsplus -o loop ./my-package.img /media/my-mount-point
dpkg, APT & Aptitude commands
- List all installed packages:
dpkg -l
- List all recently installed packages:
zcat -f /var/log/dpkg.log* | grep "\ install\ " | sort
- Install a package from a lower-priority repository, like the backport repository:
apt-get -t squeeze-backports install my-package
- Force reinstallation of a package:
apt-get -d --reinstall install my-package dpkg --install --force-confmiss /var/cache/apt/archives/my-package.deb
- Clean aptitude local cache:
apt-get clean
- Uninstall a package throughly (both program files and configuration):
apt-get remove --purge my_package
- Force removal of a package while ignoring all dependencies:
dpkg --remove --force-depends libsomething
- Remove orphaned pakages:
deborphan | xargs apt-get -y remove --purge
- Show the changelog of a package (here, the linux kernel of Ubuntu):
aptitude changelog linux-generic
- Which package contain a given file:
apt-file search file_to_search
- Get the list of files of a package:
apt-file list package_name
- Remove
dpkglock file:rm /var/lib/dpkg/lock
- Hold a package with either
dpkgoraptitude:echo "kdenlive hold" | dpkg --set-selections
aptitude hold kdenlive
- Unhold a package:
echo "kdenlive install" | dpkg --set-selections
aptitude unhold kdenlive
- List holded packages:
dpkg --get-selections | grep hold
Website Backup Script: bug fix release
14 months after the last release, here is a new version of my website backup script. As you can see in the changelog, this version is essentially released to fix some bugs.
Changelog:
- Check version of Python (at least v2.4 is required)
- Rename
--debugoption to--verbose - Add a
--dry-runoption for testing - Remove use of deprecated
pexpectmethods - Add and update some error messages