Adam Cap

  • About
  • Mail
  • Archive/Search

command line

code | Language … see also: PHP / CSS / JavaScript / HTML

Move from Live Production Environment to Local macOS Development Sandbox (MAMP)

↗︎ Sep 29, 2019⇠ | skip ⇢

This is the converse of slash companion to this guide.

Finalize everything in your live production environment (by adding, removing, editing, and updating plugins, themes, WordPress, content, etc.) before proceeding.

First: Back up

  1. Local wp-content folder:
    open new Terminal window/tab
    cd [Local WordPress Path] (drag WordPress folder from Finder to Terminal to get path)
    mkdir -p my-wp-backups && tar -czvf my-wp-backups/wp-content-`date +%F`.tar.gz wp-content
  2. Local wp-config.php file:
    cp wp-config.php my-wp-backups/wp-config-`date +%F`.php
  3. Local MySQL database:
    [Local mysqldump Path] -u [DB_USER] -p[DB_PASSWORD] --opt [DB_NAME] > wp-database.sql (e.g., /Applications/MAMP/Library/bin/mysqldump) (see local wp-config.php file for DB_USER, DB_NAME, and DB_PASSWORD)
    tar -czvf my-wp-backups/wp-database-`date +%F`.sql.tar.gz wp-database.sql
    rm wp-database.sql
  4. Production wp-content folder:
    open new Terminal window/tab
    ssh username@example.com (e.g., ssh adamcap.com@adamcap.com)
    enter password
    cd [Production WordPress Path] (e.g., cd html)
    mkdir -p my-wp-backups && tar -czvf my-wp-backups/wp-content-`date +%F`.tar.gz wp-content
  5. Production wp-config.php file:
    cp wp-config.php my-wp-backups/wp-config-`date +%F`.php
  6. Production MySQL database:
    wp db export wp-database.sql
    tar -czvf my-wp-backups/wp-database-`date +%F`.sql.tar.gz wp-database.sql
    rm wp-database.sql
  7. Production my-wp-backups folder:
    tar -czvf my-wp-backups.tar.gz my-wp-backups
    (rm my-wp-backups)
    (rm my-wp-backups.tar.gz)

Second: Download my-wp-backups

  1. Download production my-wp-backups folder:
    scp [username@example.com]:[Production WordPress Path]/my-wp-backups.tar.gz [Local Desktop Path] (obtain from web host; e.g., adamcap.com@adamcap.com:/var/www/html/ad/adamcap.com) (drag Desktop from Finder to Terminal to get path or ~/Desktop)

Third: Unzip, rename, and move

  1. Unzip:
    double-click my-wp-backups.sql.tar.gz file on Desktop to unzip folder
    enter folder
    double-click wp-database-`date +%F`.sql.tar.gz file to unzip database
  2. Rename:
    add suffix -new to each item’s file/folder name
  3. Move:
    drag wp-content-new and wp-database-new.sql to local WordPress folder

Fourth: Prepare wp-config.php

  1. Copy local wp-config.php file:
    open new Terminal window/tab
    cd [Local WordPress Path] (drag WordPress folder from Finder to Terminal to get path)
    cp wp-config.php wp-config-new.php
  2. Edit local wp-config-new.php file:
    nano wp-config-new.php
    Update DB_NAME, DB_USER, and DB_PASSWORD (and verify $table_prefix) (be certain to update DB_NAME, e.g., database-XXXX-XX-XX)
    control + O (to save), control + X (to exit)
  3. (Switch local wp-config.php file:)
    mv wp-config.php wp-config-old.php && mv wp-config-new.php wp-config.php
  4. (Revert local wp-config.php file:)
    mv wp-config.php wp-config-new.php && mv wp-config-old.php wp-config.php

Fifth: Import database

  1. Create new local database:
    open new Terminal window/tab
    cd [Local WordPress Path] (drag WordPress folder from Finder to Terminal to get path)
    wp db drop ; wp db create OR drop/create via phpMyAdmin
  2. Switch local wp-content folder:
    mv wp-content wp-content-old && mv wp-content-new wp-content
  3. Switch local wp-config.php file:
    mv wp-config.php wp-config-old.php && mv wp-config-new.php wp-config.php
  4. Import wp-database-new.sql file into local database:
    wp db import wp-database-new.sql OR [Local mysql Path] -u [DB_USER] -p[DB_PASSWORD] [DB_NAME] < wp-database-new.sql (e.g., /Applications/MAMP/Library/bin/mysql)
  5. Search/replace local database:
    wp search-replace $(wp option get siteurl) [Local Site URL](e.g., http://localhost:8888/adamcap or http://adamcap.local)
    (wp search-replace [Production Hostname] [Local Hostname]) (e.g., adamcap.com) (e.g., localhost:8888/adamcap)
    (wp search-replace https://[Production Hostname] http://[Local Hostname]) (e.g, https://adamcap.com) (e.g., http://localhost:8888/adamcap)
  6. Flush rewrite rules (i.e., save permalinks)
  7. (Revert local wp-content folder:)
    mv wp-content wp-content-new && mv wp-content-old wp-content
  8. (Revert local wp-config.php file:)
    mv wp-config.php wp-config-new.php && mv wp-config-old.php wp-config.php
  9. (Delete unneeded/old local files and folders:)
    rm wp-content-new.tar.gz
    rm wp-database-new.sql
    rm wp-database-new.sql.tar.gz
    rm -rf wp-content-old
    rm wp-config-old.php

Me

circa 2017 (29 y/o)

about adam

Jump…

  • 19 Mar 23: Move from Live Production Environment to Local macOS Development Sandbox (MAMP) #command line #WordPress
  • 19 Jan 26: Use Git to Manage WordPress Projects Developed Locally on macOS #command line #WordPress
  • 19 Jan 16: Move from Local macOS Development Sandbox (MAMP) to Live Production Environment #command line #WordPress

More…
WordPress (Tag) / code (Post Type)

Use Git to Manage WordPress Projects Developed Locally on macOS

↗︎ Sep 30, 2019⇠ | skip ⇢

Git is a system for keeping track of changes in code. It’s particularly useful for collaboration (“Hello, world of fellow programmers!”) and reversion when a project’s source code has inadvertently ventured treacherously and irrevocably astray (fervently spamming CMD+Z is not always enough to re-establish baseline).

I obdurately refused to learn git for many years, because I am a doofus, and worked mostly solo on projects (verily to detriment). I recommend learning git, sharing code, and collaborating with others. Feedback is invaluable; you’ll get way better way faster than I did working this way.

Because I didn’t exactly hit the ground running, in terms of adapting git, I’ve typed out loose directions for personal reference—and for others whose interest is piqued and could use a guiding hand.

First: Initialize Git

  1. Install Xcode command line tools:
    open new Terminal tab/window
    xcode-select --install (or xcode-select --reset) (see: link)
  2. Go through seemingly pertinent GitHub setup guides: link
  3. Generate SSH key (and add passphrase to keychain): link
  4. Add SSH key to GitHub account: link

Second: Create Origin Repository

  1. Visit: link
  2. Choose repository name and public/private (skip other options)
  3. Create repository
  4. Leave succeeding page open (it lists helpful commands and SSH URL needed below)

Third: Initialize & Commit Remote Repository to Origin

  1. Navigate to local, top-level project directory (e.g., WordPress theme or root):
    open new Terminal window/tab
    cd [Local Path] (drag folder from Finder to Terminal to get path)
  2. Add remote repository:
    git init
    git remote add origin git@github.com:[GitHub Username]/[Repository Name].git
  3. (Pull remote repository:)
    git pull origin master (see: link)
  4. Create .gitignore file to ignore files in repository:
    touch .gitignore
    edit w/ text editor (see: link for guidance w/ file contents)
  5. Add files and commit changes:
    git add -A
    git commit -am "[Message]" (e.g., git commit -am "first commit")
  6. Push to remote repository:
    git push -u origin master

Misc. Maintenance

  • View status:
    git status
  • Remove .DS_Store:
    find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch (see: link)
    git add -A
    git commit -am "delete .DS_Store"
  • Remove remote repository:
    rm -rf .git
    rm .gitignore

Me

circa 2010 (22 y/o)

Popularly…

  • 19 Jan 18: Fix Line Height for Input Placeholders in Safari #CSS
  • 19 May 4: Prevent iOS + WordPress from Replacing Arrows (and Other HTML Symbols) with Emoji #CSS #HTML
  • 19 Jan 30: Save Jetpack “Site Stats” Pageviews as Post Meta #JavaScript #PHP #WordPress
  • 19 Jan 18: Add Partner/Associates/Tracking ID/Tag to Amazon Links #PHP #regex #WordPress
  • 19 Jan 16: Move from Local macOS Development Sandbox (MAMP) to Live Production Environment #command line #WordPress
  • 19 Jan 26: Use Git to Manage WordPress Projects Developed Locally on macOS #command line #WordPress
  • 19 Mar 23: Move from Live Production Environment to Local macOS Development Sandbox (MAMP) #command line #WordPress
  • 19 Jan 15: Add Media/Attachment Source Fields (and Display Image Sources in Content) #PHP #regex #WordPress
  • 19 Jan 18: Add Fancybox Attribute to Image Links #PHP #regex #WordPress
  • 19 Jan 7: Remove Nonbreaking Spaces (&nbsp;’s) from Content and Text Widgets #PHP #WordPress
  • 19 Jan 18: Automatically Bold (or Style) the First X Words of Content #PHP #regex #WordPress

More…
WordPress (Tag) / code (Post Type)

Move from Local macOS Development Sandbox (MAMP) to Live Production Environment

↗︎ Mar 24, 2019⇠ | skip ⇢

Follow these steps to move a local, MAMP development environment to a live, production environment (that has shell and WP-CLI enabled; good web hosts do). We will be using command line to achieve this end. The reason to use command line instead of graphical interfaces, like the myriad of available (S)FTP clients or phpMyAdmin, is that command line lends better performance and reliability. Graphical interfaces will time out, or seemingly take forever to load, if your files are large. There is a higher learning curve to command line—it will feel initially like yawping haphazardly into a void—but the reaped time savings make it well worth practicing.

Finalize everything in your local development environment (by adding, removing, editing, and updating plugins, themes, WordPress, content) before proceeding.

First: Back Up

  1. Local wp-content folder:
    open new Terminal window/tab
    cd [Local WordPress Path] (drag WordPress folder from Finder to Terminal to get path)
    mkdir -p my-wp-backups && tar -czvf wp-content-`date +%F`.tar.gz wp-content -C my-wp-backups
  2. Local wp-config.php file:
    cp wp-config.php my-wp-backups/wp-config-`date +%F`.php
  3. Local MySQL database:
    [Local mysqldump Path] -u [DB_USER] -p --opt [DB_NAME] > wp-database.sql (e.g., /Applications/MAMP/Library/bin/mysqldump) (see local wp-config.php file for DB_USER, DB_NAME, and DB_PASSWORD)
    enter DB_PASSWORD
    tar -czvf wp-database-`date +%F`.sql.tar.gz wp-database.sql -C my-wp-backups
    rm wp-database.sql
  4. Production wp-content folder:
    open new Terminal window/tab
    ssh username@example.com (e.g., ssh adamcap.com@adamcap.com)
    enter password
    cd [Production WordPress Path] (e.g., cd html)
    mkdir -p my-wp-backups && tar -czvf wp-content-`date +%F`.tar.gz wp-content -C my-wp-backups
  5. Production wp-config.php file:
    cp wp-config.php my-wp-backups/wp-config-`date +%F`.php
  6. Production MySQL database:
    wp db export wp-database.sql
    tar -czvf wp-database-`date +%F`.sql.tar.gz wp-database.sql -C my-wp-backups
    rm wp-database.sql

Second: Upload wp-content

  1. Gzip local wp-content folder:
    open new Terminal window/tab
    cd [Local WordPress Path] (drag WordPress folder from Finder to Terminal to get path)
    mv wp-content wp-content-new && tar -czvf wp-content-new.tar.gz wp-content-new && mv wp-content-new wp-content
  2. Upload local .tar.gz file to production:
    open new Terminal window/tab
    scp [Local .tar.gz Path] [username@example.com]:[Production WordPress Path] (drag .tar.gz file from Finder to Terminal to get path) (obtain from web host; e.g., adamcap.com@adamcap.com:/var/www/html/ad/adamcap.com)
    enter password
  3. Unzip production .tar.gz file:
    ssh username@example.com (e.g., ssh adamcap.com@adamcap.com)
    enter password
    cd [Production WordPress Path] (e.g., cd html)
    mkdir wp-content-new && tar -xzvf wp-content-new.tar.gz -C wp-content-new --strip-components 1
  4. (Switch production wp-content folder:)
    mv wp-content wp-content-old && mv wp-content-new wp-content
  5. (Revert production wp-content folder:)
    mv wp-content wp-content-new && mv wp-content-old wp-content

Third: Prepare wp-config.php

  1. Copy production wp-config.php file:
    open new Terminal window/tab
    ssh username@example.com (e.g., ssh adamcap.com@adamcap.com)
    enter password
    cd [Production WordPress Path] (e.g., cd html)
    cp wp-config.php wp-config-new.php
  2. Edit production wp-config-new.php file:
    nano wp-config-new.php
    Update DB_NAME, DB_USER, and DB_PASSWORD (and verify $table_prefix)
    control + O (to save), control + X (to exit)
  3. (Switch production wp-config.php file:)
    mv wp-config.php wp-config-old.php && mv wp-config-new.php wp-config.php
  4. (Revert production wp-config.php file:)
    mv wp-config.php wp-config-new.php && mv wp-config-old.php wp-config.php

Fourth: Move database

  1. Export local MySQL database:
    open new Terminal window/tab
    cd [Local WordPress Path] (drag WordPress folder from Finder to Terminal to get path)
    [Local mysqldump Path] -u [DB_USER] -p[DB_PASSWORD] --opt [DB_NAME] > wp-database-new.sql (e.g., /Applications/MAMP/Library/bin/mysqldump) (see local wp-config.php file for DB_USER, DB_NAME, and DB_PASSWORD)
  2. Gzip local .sql file:
    tar -czvf wp-database-new.sql.tar.gz wp-database-new.sql
  3. Upload local .tar.gz file:
    open new Terminal window/tab
    scp [Local .tar.gz Path] [username@example.com]:[Production WordPress Path] (drag .tar.gz file from Finder to Terminal to get path) (obtain from web host; e.g., adamcap.com@adamcap.com:/var/www/html/ad/adamcap.com)
    enter password
  4. (Delete unneeded/old local files and folders:)
    rm wp-content-new.tar.gz
    rm wp-database-new.sql
    rm wp-database-new.sql.tar.gz
  5. Unzip production .tar.gz file:
    ssh [username@example.com] (e.g., ssh adamcap.com@adamcap.com)
    enter password
    cd [Production WordPress Path] (e.g., cd html)
    tar -xzvf wp-database-new.sql.tar.gz
  6. Switch production wp-content folder:
    mv wp-content wp-content-old && mv wp-content-new wp-content
  7. Switch production wp-config.php file:
    mv wp-config.php wp-config-old.php && mv wp-config-new.php wp-config.php
    (wp config shuffle-salts)
  8. Create new production database:
    wp db drop ; wp db create
  9. Import production .sql file into production database:
    wp db import wp-database-new.sql
  10. Search/replace production database:
    wp search-replace $(wp option get siteurl) [Production Site URL] (e.g., wp search-replace $(wp option get siteurl) https://adamcap.com)
    (wp search-replace [Local Hostname] [Production Hostname])
    (wp search-replace http://[Production Hostname] https://[Production Hostname])
  11. (Revert production wp-content folder:)
    mv wp-content wp-content-new && mv wp-content-old wp-content
  12. (Revert production wp-config.php file:)
    mv wp-config.php wp-config-new.php && mv wp-config-old.php wp-config.php
  13. (Delete unneeded/old production files and folders:)
    rm wp-content-new.tar.gz
    rm wp-database-new.sql
    rm wp-database-new.sql.tar.gz
    rm -rf wp-content-old
    rm wp-config-old.php

See Also

  • Move from Live Production Environment to Local macOS Development Sandbox (MAMP) (coming soon)

Me

circa 2013 (25 y/o)

Randomly…

  • 19 Jan 7: Remove Nonbreaking Spaces (&nbsp;’s) from Content and Text Widgets #PHP #WordPress
  • 19 Jan 18: Add Partner/Associates/Tracking ID/Tag to Amazon Links #PHP #regex #WordPress
  • 19 May 4: Prevent iOS + WordPress from Replacing Arrows (and Other HTML Symbols) with Emoji #CSS #HTML
  • 19 Jan 18: Fix Line Height for Input Placeholders in Safari #CSS
  • 19 Mar 23: Move from Live Production Environment to Local macOS Development Sandbox (MAMP) #command line #WordPress
  • 19 Jan 16: Move from Local macOS Development Sandbox (MAMP) to Live Production Environment #command line #WordPress
  • 19 Jan 18: Add Fancybox Attribute to Image Links #PHP #regex #WordPress
  • 19 Jan 18: Automatically Bold (or Style) the First X Words of Content #PHP #regex #WordPress
  • 19 Jan 15: Add Media/Attachment Source Fields (and Display Image Sources in Content) #PHP #regex #WordPress
  • 19 Jan 30: Save Jetpack “Site Stats” Pageviews as Post Meta #JavaScript #PHP #WordPress
  • 19 Jan 26: Use Git to Manage WordPress Projects Developed Locally on macOS #command line #WordPress

More…
WordPress (Tag) / code (Post Type)

  • Home
  • About
  • Archive
  • Mail
  • Random
  • Dingus
  • Reading
  • Code

ADAM CAP is an elastic waistband enthusiast, hammock admirer, and rare dingus collector hailing from Berwyn, Pennsylvania.

My main interests at this time include reading, walking, and learning how to do everything faster.

Psst: If you find my website helpful or enjoyable, please join my newsletter and/or send me an email—I want to hear from you!

Disclosure: As an Amazon Associate I earn from qualifying purchases.

© 2009–2023 Adam Cap(riola) top ⇡