During the development of the site on Django had to start to understand the Bash scripts to automate routine tasks. For example, creating and downloading a database dump from a site, as well as reserving media files.
We assume that you already have access to the server via ssh , and your user on the server that manages the site has access rights, with which he can dump the database.
Directory structure with scripts
To perform backup of media files and a database dump, you will need to write several scripts:
- The main script
- A script to create a dump that will be executed on a remote server
- Script to delete the dump on a remote server, so as not to waste valuable disk space
Структура будет следующей
./remote-scripts/create_dump.sh ./remote-scripts/remove_dump.sh ./backup.sh
When a user enters the server, it appears in its home directory. Therefore, we will write scripts based on the fact that we are in the given home directory.
#!/bin/bash # Let's remember the time of creating a dump current_date=$(date +"%Y_%m_%d_%H:%M:%S") # Create a directory where we will save the dump mkdir db_dumps # create a dump pg_dump database_name > ~/db_dumps/db_$current_date # disconnect from the server exit
I create a directory for the dump, because it's easier to have a script to call rsync, just merge the entire dump into some directory and everything. However, other versions of the dump that we downloaded earlier will not be deleted, even if they do not exist in the db_dumps directory.
Script to remove the dump from a remote server
#!/bin/bash # delete the directory with the database dump rm -rf db_dumps # disconnect from the server exit
And now the time for the most important script, which will collect everything together
#!/bin/bash # Пути к скриптам REMOTE_SCRIPTS_PATH="remote-scripts" SCRIPT_PATH_CREATE_DUMP="create_dump.sh" SCRIPT_PATH_REMOVE_DUMP="remove_dump.sh" # Let's create a database dump ssh email@example.com 'bash -s' < "$REMOTE_SCRIPTS_PATH/$SCRIPT_PATH_CREATE_DUMP" # Download the dump to the backup directory rsync -av --progress firstname.lastname@example.org:~/db_dumps ~/backup # Download media files to the backup directory rsync -av --progress email@example.com:~/virtual_env/yourproject/media ~/backup # Delete the dump from the remote server ssh firstname.lastname@example.org 'bash -s' < "$REMOTE_SCRIPTS_PATH/$SCRIPT_PATH_REMOVE_DUMP"
In this case, we make a connection using ssh with the username and IP address of the server.
This line sends to the remote server the script create_dump.sh from your local PC
ssh email@example.com 'bash -s' < "$REMOTE_SCRIPTS_PATH/$SCRIPT_PATH_CREATE_DUMP"