Bash scripts to simplify server backups and data synchronization using rsync and database dump tools.
-
Create Configuration File(s):
- Copy the
config.examplefile to a new file for each server you want to back up (e.g.,cp config.example config/my_server.conf). - It's recommended to store these configuration files in a dedicated directory, for example,
config/. Thebackup_all_servers.shscript typically looks for configurations in thisconfig/subdirectory. - Edit your new configuration file (e.g.,
config/my_server.conf) and populate it with the necessary parameters. Refer to the comments withinconfig.examplefor details on each parameter.
- Copy the
-
Setup SSH Access:
-
Ensure your SSH key (specified by
SSHPASSFILEin the config) allows passwordless login to the remote server(s). -
Run the SSH setup script. This script usually iterates through servers defined in your configuration files (e.g., in the
config/directory) and adds their host keys to~/.ssh/known_hosts../setup_ssh.sh
-
Note: Ensure
setup_ssh.shhas execution permissions (chmod +x setup_ssh.sh). This applies to all scripts.
-
-
Perform Backups:
-
Backup all configured servers (data and databases):
./backup_all_servers.sh
-
Backup a specific server's data:
./backup_server.sh path/to/your_server.conf
-
Backup a specific server's database:
./backup_db.sh path/to/your_server.conf
-
To configure the backup scripts for a server:
- Copy the provided
config.examplefile to a new name (e.g.,cp config.example config/my_server.conf). - Edit your newly created configuration file, uncommenting and setting values for all required parameters.
All parameters are documented directly within the config.example file, which indicates whether each setting is required or optional and provides example values.
All scripts should be run from the directory where they are located. Ensure they have execute permissions (e.g., chmod +x *.sh).
setup_ssh.sh:- Prepares SSH connections by adding host keys of configured servers to
~/.ssh/known_hosts. - It typically discovers servers from configuration files (e.g., in a
config/directory). - Usage:
./setup_ssh.sh
- Prepares SSH connections by adding host keys of configured servers to
backup_all_servers.sh:- Automates backups for all servers defined in configuration files (usually in a
config/directory). - For each server, it performs both data backup (like
backup_server.sh) and database backup (if DB details are configured, likebackup_db.sh). - Usage:
./backup_all_servers.sh
- Automates backups for all servers defined in configuration files (usually in a
backup_server.sh <CONFIG_FILE>:- Backs up data from the remote server specified in the
<CONFIG_FILE>using rsync. - Usage:
./backup_server.sh path/to/your_server.conf
- Backs up data from the remote server specified in the
sync_data <CONFIG_FILE> [OPTIONS]:- Synchronizes data between the backup storage, your local machine, and the remote server.
- Options:
-d <YYYY-MM-DD>: Specifies the date of the backup to use for restoration.--btos: Restore Backup to Server.--ltos: Sync Local data to Server.--stol: Sync Server data to Local.
- Usage Examples:
- Restore data to server:
./sync_data path/to/config.conf -d 2023-10-26 --btos - Sync local data to server:
./sync_data path/to/config.conf --ltos - Sync server data to local:
./sync_data path/to/config.conf --stol
- Restore data to server:
backup_db.sh <CONFIG_FILE>:- Backs up the database from the remote server specified in
<CONFIG_FILE>. - Requires
DB_USERNAME,DB_PASSWORD, andDB_NAMEto be set in the config. - Usage:
./backup_db.sh path/to/your_server.conf
- Backs up the database from the remote server specified in
sync_db <CONFIG_FILE> [OPTIONS]:- Synchronizes a database between the backup storage, your local machine, and the remote server.
- Options: (Same as
sync_data)-d <YYYY-MM-DD>--btos--ltos--stol
- Usage Examples:
- Restore DB to server:
./sync_db path/to/config.conf -d 2023-10-26 --btos - Sync local DB to server:
./sync_db path/to/config.conf --ltos - Sync server DB to local:
./sync_db path/to/config.conf --stol
- Restore DB to server: