CLI Basic Linux Commands

Yep, every operating system has a handful of commands that help you find your way around the file system, deal with directories and files, and in this case help you organize your MySQL database backups. What I want to do is ignore the majority of the operating systems and commands and focus on Linux and the ones you need to help you get started.

Resource: http://en.wikibooks.org/wiki/Linux_Guide/Linux_commands

As a normal user or a super-user you have access to some or all directories and folders. To use mysqldump or mysql you don’t need to be a super-user. I usually recommend to login with SSH as the user of the site you want to back up a database for. For example, a XenForo forum.

Assuming you are already logged in ..

To find out which system you’re on, and what the uptime is, etc:

w

To find out who you are, which is handy when you log in as one user, but sudo up to another user, etc.

whoami

To find out where you are within the file system. Note that the first directory on the server is /

pwd

To quick go back to the home directory of the user you’re logged in with:

cd

To find out which directories and files are within the current working directory:

ls

To get the same list, but with all attributes, ownerships, permissions:

ls -al

(little explanation, the list will have a column with letters d r and w repeating, these stand for directory, read, and write. These are the permissions for that directory or file.)

To change into one of the directories, example, you’re in your home dir, and ls showed there’s public_html/

cd public_html/

To go back one directory, you could go up to the parent directory with:

cd ..

(little explanation, when you do ls -all you might notice a single dot at the top, and then two dots. The first dot basically stands for the current directory, and the double dot is the parent directory)

To make a new directory within the current directory you can use:

mkdir backups

Assuming you use mysqldump to get your .sql dump inside the backups directory, and you want to delete it:
(let’s see if we are in the backups directory)

pwd
> /home/username/backups/

(let’s see if there are any .sql files in there)

ls *.sql
> backup.sql

(ok, let’s remove the backup.sql file, we no longer need it)

rm backup.sql

There are more advanced commands which require parameters and filenames, etc, such as tar (to compress the .sql to a smaller gzipped tarball) and cp (to copy one file to a new file, optionally at a new location) and we can go into those later, but I want to at least mention mv (to rename), as I can imagine you might want to rename backup.sql to backup-April2012.sql

To rename a file to a new filename: (mv = move basically, to move a file from a to b)

mv backup.sql backup-April2012.sql

(You can use ls *.sql again to first see if the file to rename is in the list, and afterwards again to see if the new file is in the list)

Pro-tip about setting the date:

mv backup.sql backup-$(date +"%F").sql

Example:

ls *.sql
> backup-2012-04-24.sql

Okay, there we have it.

You should now be able to login with ssh, browse around your file system and find the backup directory. From where you can see which files there are, how to rename or remove them. The next step is to learn how to dump the MySQL database for your site into the backups directory. So you can download it with your FTP client (over sftp protocol!)


Posted

in

by

Tags: