PowerShell Script For Semester Batches

This is a script I’ve developed after relying on CMD for so long and needing to use PowerShell to quickly export file names in large .ZIP files.

Be sure to run as Administrator and make sure your execution policy is not restricted.

You can call the function whatever you like. Export-Csv converts objects into a series of strings. Read more here.

Function GetClassList([string]$path, [string]$outputFile) {
$list = Get-ChildItem $path -Recurse | where {!$_.PSIsContainer}
$list | Select-Object BaseName | Export-Csv $outputFile
}
GetClassList "C:\Users\T\Desktop\fall2018\batch5" "C:\Users\T\Desktop\fall2018\batch5\list1.txt"

First argument is the directory and the second is your file destination.

Backup and Restore MySQL via MySQLdump

To backup:

# mysqldump -u root -p[password] database_name > database_name_backup.sql

[password] should be placed immediately after -p. For example: -pmypassword. database_name_backup.sql would be created in the root directory.

To restore:

  1. Create an appropriately named database on the target machine
  2. Load the file using the mysql command:
    # mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

To restore a database that already exists, use mysqlimport command:
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Source: How to Back Up and Restore a MySQL Database

Speed Up Site Performance

Set HTTP Cache Headers

<VirtualHost *:80>
# Your config...
ExpiresActive On
<FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
ExpiresDefault "access plus 1 year"
Header unset ETag
FileETag None
Header unset Last-Modified
</FilesMatch>
</VirtualHost>

Gzip Web Server Output

<VirtualHost *:80>
# Your config...
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \\bMSIE !no-gzip !gzip-only-text/html
</VirtualHost>

Source: Faster Sites Done Faster

Further reading: Using a far future expires header

Change Server Upload Permissions

  1. Enter ssh username@mason.gmu.edu replacing username appropriately.
  2. Enter your mason.gmu.edu password
  3. mason> pico .cshrc
  4. edit to reads umask 022 or umask 002 (If other people in your group edit the website, change your umask to 002; If only you edit your website, change it to 022)
  5. Save the file (Ctrl+O) (letter O)
  6. File Name to write: .cshrc will display towards the bottom of the window. Hit enter.
  7. Exit the file (Ctrl+X).
  8. Run pico .profile and pico .login and repeat Steps 3 and 4.
  9. Log out and then log back in again.

Source: Fix Server Upload Permissions

Frequently Used Linux Command Lines

Install WordPress
wget https://wordpress.org/latest.tar.gz
tar xfz latest.tar.gz
cp -r * ~/public_html/your/website/folder/

Change Permissions (rwx => 4 + 2 + 1 = 7)
For Directories: find /path/to/your/directory/ -type d -exec chmod 755 {} \;
For Files: find /path/to/your/file/ -type f -exec chmod 644 {} \;

Permissions
chmod -R 755 directory
chmod -R 644 files

Remove files and directory (use carefully)
rm -rf YourDirectory

Change Owner
chown -R apache new_name (change entire directory)

Remane Directory
mv {old} {new}

Database
To access database: mysql -u root -p
mysql> create database [database name];
mysql> show databases;
mysql> drop database [database name];
More

VI editor
Reference: Using the VI editor

Red Hat Training

Notes from Red Hat System Administration I (RH124) from April 2-6, 2012

Day 1

Covered:
Installing Red Hat 6, which took almost half a day.
Using GNOME graphical desktop.
Editing files with gedit.
Managing files with Nautilus.
Getting help with manuals (F1 for help)
Managing system clock.
Configuring printers.

Command Lines:
uname -r (find out what version of Red Hat)
$ su – (log in as substitute user)
# useradd visitor (add username “visitor”)
# passwd visitor (add password to “visitor”)
# ip addr show eth0 (show ip address)
# ls /usr/share/doc (documentations)
# man man (access manual)
# man -k print (-k is for keyword)

Day 2

Introduced to Bash

Commands entered at the shell prompt have three basic parts:
# chmod +x gedit demo.txt
[command] [options] [arguments]

Command lines:
# pwd (path you’re currently in)
# history
Using the tab key (“tab” “tab”) for completion
# scp filename user@ip-address:/ (copy file from local folder to virtual server)

Introduced to managing physical storage using the graphical interface
Learned to manage logical volumes
Introduced to monitoring system resources

Day 3

Yum
# yum list (get the list of .rpm, which stands for RPM Package Manager)
# yum search kernel (kernel is the name of the package)
# yum install kernel
# yum update kernel
# yum remove kernel

Network
# ip addr show eth0 (eth0 is the name of the network)
# more /etc/sysconfig/network-scripts/ifcfg-eth0
# service network restart
# system-config-network
# ip route
# host visualgui.com
# dig visualgui.com

Users and Groups
# useradd donny
# passwd donny
# vipw (let’s one user edit at the same time)

Manage Files
# echo $PATH
# pwd (current path)
# ls /home/{dir1,dir2} (show more two directories)
# ln file1 file2 (link file 1 to file 2)
# ln -s file1 file2 (symbolic link)
# su – username (change user)
# cp -av /dir/to/copy /dir/destination

Permission
r (view content)
w (change the content)
x (requires r to execute the content)

rw-rw-rw-
rw-r–r–
umask 002

u+s (suid=4: file executes as the user that owns the file, not the user that ran the file
g+s (sgid=2: file executes as the group that owns the file
o+t (sticky=1: no effect)

Day 4

Administer remote system including remote shell access, remote file transfer, using SSH keys

Managing physical storage including filesystem, partition and swap space

Install Linux with Anaconda.

SSH
# scp original-file new-file
# rsync -a /home/student demo:/tmp
-a=all

Archive
# tar -cvf target/to/archive source/to/archive
-c=create
-x=extract
-t=test (tell)
-v=verbose
-f=filename

Using SSH
# ssh-keygen
# ssh-copy-id example.com

# service crond status
# chkconfig –list crond

# vi /etc/ssh/sshd_config
PermissionRootLogin no
PassordAuthentication no
# service sshd restart

Day 5

FTP
# service vsftpd status
# service vsftpd start
# chkconfig vsftpd on
# chkconfig vsftpd –list

httpd
# ll /var/www/
# ln -s /var/ftp/pub /var/www/html/pub
# cat > /var/ftp/pub/index.html
Hello World!