Skip to content

Instantly share code, notes, and snippets.

@pugsley
Last active March 19, 2025 12:06
Show Gist options
  • Select an option

  • Save pugsley/2914b9c0d1e7b866eab2a4cc0ceb0ead to your computer and use it in GitHub Desktop.

Select an option

Save pugsley/2914b9c0d1e7b866eab2a4cc0ceb0ead to your computer and use it in GitHub Desktop.

Revisions

  1. pugsley revised this gist May 16, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ gzip -d [filename].sql.gz

    ## Clean up backups
    ```
    ls -tp [dir] | grep -v '/$' | tail -n +8 | xargs -I {} rm -- {}
    cd [dir] && ls -tp | grep -v '/$' | tail -n +8 | xargs -I {} rm -- {}
    ```
    Keep the latest 7 files in `[dir]`.

  2. pugsley revised this gist May 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,6 @@ Keep the latest 7 files in `[dir]`.



    References:
    References:
    https://www.everythingcli.org/secure-mysqldump-script-with-encryption-and-compression/
    http://stackoverflow.com/questions/25785/delete-all-but-the-most-recent-x-files-in-bash
  3. pugsley revised this gist May 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -46,4 +46,4 @@ Keep the latest 7 files in `[dir]`.

    References:
    https://www.everythingcli.org/secure-mysqldump-script-with-encryption-and-compression/

    http://stackoverflow.com/questions/25785/delete-all-but-the-most-recent-x-files-in-bash
  4. pugsley revised this gist May 9, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,13 @@ gzip -d [filename].sql.gz
    ```


    ## Clean up backups
    ```
    ls -tp [dir] | grep -v '/$' | tail -n +8 | xargs -I {} rm -- {}
    ```
    Keep the latest 7 files in `[dir]`.





  5. pugsley revised this gist Apr 29, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,6 @@ Generate openssl keys:
    ```
    openssl req -x509 -nodes -newkey rsa:2048 -keyout mysqldump-key.priv.pem -out mysqldump-key.pub.pem
    ```
    Enter whatever at the prompts.

    Create a mysql default file:
    ```
  6. pugsley revised this gist Apr 29, 2016. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ Bash script:
    #!/bin/bash
    DATE=`date +%Y-%m-%d-%H-%M-%S`
    ARCHIVE=${DATE}.sql.enc.gz
    ARCHIVE=${DATE}.sql.gz.enc
    MYSQLINFO=~/.mysqldump
    DATABASE=databasename
    PUBLIC_KEY=~/.mysqldump-key.pub.pem
    @@ -28,6 +28,15 @@ mysqldump --defaults-extra-file=${MYSQLINFO} ${DATABASE} --single-transaction --
    | openssl smime -encrypt -binary -text -aes256 -out ${ARCHIVE} -outform DER ${PUBLIC_KEY}
    ```

    Decrypt & decompress
    ```
    openssl smime -decrypt -in [filename].sql.gz.enc -binary -inform DEM -inkey mysqldump-secure.priv.pem -out [filename].sql.gz
    gzip -d [filename].sql.gz
    ```





    References:
    https://www.everythingcli.org/secure-mysqldump-script-with-encryption-and-compression/
  7. pugsley revised this gist Apr 29, 2016. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -23,9 +23,6 @@ MYSQLINFO=~/.mysqldump
    DATABASE=databasename
    PUBLIC_KEY=~/.mysqldump-key.pub.pem
    echo ${DATE}
    echo ${ARCHIVE}
    mysqldump --defaults-extra-file=${MYSQLINFO} ${DATABASE} --single-transaction --routines --events --triggers \
    | gzip -c \
    | openssl smime -encrypt -binary -text -aes256 -out ${ARCHIVE} -outform DER ${PUBLIC_KEY}
  8. pugsley created this gist Apr 29, 2016.
    37 changes: 37 additions & 0 deletions mysql-backup.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    Generate openssl keys:
    ```
    openssl req -x509 -nodes -newkey rsa:2048 -keyout mysqldump-key.priv.pem -out mysqldump-key.pub.pem
    ```
    Enter whatever at the prompts.

    Create a mysql default file:
    ```
    # ~/.mysqldump
    [mysqldump]
    host = host.here.com
    user = user
    password = "password"
    ```

    Bash script:
    ```
    #!/bin/bash
    DATE=`date +%Y-%m-%d-%H-%M-%S`
    ARCHIVE=${DATE}.sql.enc.gz
    MYSQLINFO=~/.mysqldump
    DATABASE=databasename
    PUBLIC_KEY=~/.mysqldump-key.pub.pem
    echo ${DATE}
    echo ${ARCHIVE}
    mysqldump --defaults-extra-file=${MYSQLINFO} ${DATABASE} --single-transaction --routines --events --triggers \
    | gzip -c \
    | openssl smime -encrypt -binary -text -aes256 -out ${ARCHIVE} -outform DER ${PUBLIC_KEY}
    ```


    References:
    https://www.everythingcli.org/secure-mysqldump-script-with-encryption-and-compression/