Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named parameters #3

Merged
merged 4 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ root@0:~# /usr/bin/certbot --version
certbot 0.28.0
```

Whilst these versions have been tested your mileage may vary, there is very little reason if you are using an older/newer version of Debian or another flavour of Linux you may need to alter the directory locations, but the script itself should still work as it relies primarily on simple BASH file system commands and Tar.
Whilst these versions have been tested your mileage may vary, there is very little reason if you are using an older/newer version of Debian or another flavour of Linux you may need to alter the directory locations, but the script itself should still work as it relies primarily on simple BASH file system commands and `tar`.

## Getting Started

Expand All @@ -81,37 +81,55 @@ The simplest way to get started is to clone the repository:
git clone https://github.com/AlexWinder/letsencrypt-backup.git
```

This script assumes that you are using the default directory of `/etc/letsencrypt`. If your Let's Encrypt configuration files are in a different location then you will need to amend this as appropriate.
This script assumes that you are using the default directory of `/etc/letsencrypt`. If your Let's Encrypt configuration files are in a different location then you will need to amend this as appropriate, as detailed in the section below.

Once cloned you will need to set up a crontab to run periodically to execute the [backup.sh](backup.sh) script. The example below will run the backup script every day at 00:00, however you are free to run the script as often or as little as your requirements or resources permit.
Once cloned you will need to set up a crontab to run periodically to execute the [letsencrypt-backup.sh](letsencrypt-backup.sh) script. The example below will run the backup script every day at 00:00, however you are free to run the script as often or as little as your requirements or resources permit.

```crontab
0 0 * * * /location/to/letsencrypt-backup/backup.sh
0 0 * * * /location/to/letsencrypt-backup/letsencrypt-backup.sh
```

You will need to drop in the correct location to the directory as per your system when you cloned the repository.

### File Permissions

You may run in to some file permissions issues, this is normally caused by the backup.sh script not be accessible by the current user. To resolve this you should change the permissions of the file to allow it to be executable by the current user.
You may run in to some file permissions issues, this is normally caused by the letsencrypt-backup.sh script not be accessible by the current user. To resolve this you should change the permissions of the file to allow it to be executable by the current user.

```bash
chmod 700 /location/to/letsencrypt-backup/backup.sh
chmod 700 /location/to/letsencrypt-backup/letsencrypt-backup.sh
```

You will need to drop in the correct location to the directory as per your system when you cloned the repository.

To test that the permissions issue is now resolved you can attempt to execute the script manually.

```bash
/location/to/letsencrypt-backup/backup.sh
/location/to/letsencrypt-backup/letsencrypt-backup.sh
```

### Things of Note
### Custom Paths

The [backup.sh](backup.sh) script will by default put compressed backup files in the `/var/backups/letsencrypt` directory. If you would prefer this be in a different location then please change this as per your system requirements.
By default the script will use the following settings:

By default the script will keep configuration files up to 120 days old. Configuration files older than this will be automatically deleted as per the backup script. If you wish to change this then you are welcome to do so, this is currently configured as per the `days` variable on line 10.
- Configuration files are backed up from `/etc/letsencrypt/`.
- Backup files are sent to `/var/backups/letsencrypt/`.
- Backup files are kept for 120 days.

If you wish to override any of these options you can pass in any of the following flags to the script with your custom argument:

```bash
./letsencrypt-backup.sh --from <configuration location> --to <backup location> --days <number of days to store backups>
```

For example:

```bash
./letsencrypt-backup.sh --from /etc/certbot --to /home/certbot/backups --days 365
```

In the above example we are taking the configuration files in `/etc/certbot`, compressing and then sending them to `/home/certbot/backups`, and deleting any which are older than 365 days old.

You are free to use any combination of the above flags (`--from`, `--to`, and `--days`). Any which you do not specify will take the default value as listed above.

## Extracting Backups

Expand All @@ -125,4 +143,4 @@ You should swap in the path and filename as per your own setup. This will extrac

## License

This project is licensed under the [MIT License](LICENSE.md).
This project is licensed under the [MIT License](LICENSE.md).
53 changes: 0 additions & 53 deletions backup.sh

This file was deleted.

93 changes: 93 additions & 0 deletions letsencrypt-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

######################
##### PARAMETERS #####
######################

# Location where the Let's Encrypt configuration files are (and the ones which are to be backed up)
from="${from:-/etc/letsencrypt/}"
# Where files are to be backed up to
to="${to:-/var/backups/letsencrypt/}"
# The number of days to keep backup files before deleting them
days="${days:-120}"

# Enable named parameters to be passed in
while [ $# -gt 0 ]; do

# If help has been requested
if [[ $1 == *"--help" ]]; then
echo
echo "Let's Encrypt Backup Script"
echo
echo "This script allows you to easily backup configuration settings, keys and certificates issued by Let's Encrypt."
echo "https://github.com/AlexWinder/letsencrypt-backup"
echo
echo "Options:"
echo
echo "--from The location where your Let's Encrypt configuration files are. Default: /etc/letsencrypt"
echo "--to Where you would like to back the files up to. Default: /var/backups/letsencrypt"
echo "--days The number of days to keep backup files before deleting them. Default: 120 (days)"
echo "--help Display help about this script"
echo
exit 0
fi

# Check all other passed in parameters
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param="$2"
fi

shift
done

#########################
##### PREREQUISITES #####
#########################

# Obtain the current datetime stamp
date=$(date +"%Y%m%d-%H%M")

# Location of temporary directory where files will be stored for a short period whilst they are compressed
tmp_location="/tmp/"

# Build the backup name
backup_name="letsencrypt_backup-${date}"

# Make sure that directory paths are in the correct format - remove all trailing slashes then add one onto the end
from=$(echo $from | sed 's:/*$::')/
to=$(echo $to | sed 's:/*$::')/

#######################
##### SCRIPT BODY #####
#######################

# Make a temporary directory
mkdir -p ${tmp_location}${backup_name}

# Copy the configuration files to the temporary directory
cp -r ${from}. ${tmp_location}${backup_name}

# Access the temporary directory
cd $tmp_location

# Set default file permissions
umask 177

# Compress the backup into a tar file
tar -cvzf ${tmp_location}${backup_name}.tar.gz ${backup_name}

# Create the backup location, if it doesn't already exist
mkdir -p ${to}

# Move the tar.gz file to the backup location
mv ${tmp_location}${backup_name}.tar.gz ${to}

# Delete the old directory from the temporary folder
rm -rf ${tmp_location}${backup_name}/

# Set a value to be used to find all backups with the same name
find_backup_name="${to}letsencrypt_backup-*.tar.gz"

# Delete files which are older than the number of days defined
find $find_backup_name -mtime +$days -type f -delete