initial commit
This commit is contained in:
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1659446231,
|
||||||
|
"narHash": "sha256-hekabNdTdgR/iLsgce5TGWmfIDZ86qjPhxDg/8TlzhE=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "eabc38219184cc3e04a974fe31857d8e0eac098d",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"ref": "nixos-21.11",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
21
flake.nix
Normal file
21
flake.nix
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
description = "A bash script that allows the user to backup and restore a specified PostgreSQL database with a timestamp in the backup file's name and command line parameters to display usage help text.";
|
||||||
|
|
||||||
|
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11;
|
||||||
|
|
||||||
|
outputs = { self, nixpkgs }: {
|
||||||
|
defaultPackage.x86_64-linux =
|
||||||
|
with import nixpkgs { system = "x86_64-linux"; };
|
||||||
|
stdenv.mkDerivation rec {
|
||||||
|
name = "pg-backup-restore";
|
||||||
|
src = self;
|
||||||
|
buildInputs = [ postgresql ];
|
||||||
|
# buildPhase = "gcc -o hello ./hello.c";
|
||||||
|
installPhase = ''
|
||||||
|
mkdir -p $out/bin
|
||||||
|
install -t $out/bin pg_backup_restore.sh
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
76
pg_backup_restore.sh
Executable file
76
pg_backup_restore.sh
Executable file
@@ -0,0 +1,76 @@
|
|||||||
|
#!/bin/env bash
|
||||||
|
|
||||||
|
# Variables
|
||||||
|
database_name=""
|
||||||
|
backup_file=""
|
||||||
|
|
||||||
|
# Function to display help text
|
||||||
|
display_help() {
|
||||||
|
echo "Usage: $0 [-b|--backup] [-r|--restore] [-d|--database] [-h|--help] database_name"
|
||||||
|
echo " -b or --backup: backup the specified database"
|
||||||
|
echo " -r or --restore: restore the specified database"
|
||||||
|
echo " -d or --database: specify the name of the database"
|
||||||
|
echo " -h or --help: display this help text"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse command line arguments
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
key="$1"
|
||||||
|
case $key in
|
||||||
|
-b|--backup)
|
||||||
|
option="backup"
|
||||||
|
;;
|
||||||
|
-r|--restore)
|
||||||
|
option="restore"
|
||||||
|
;;
|
||||||
|
-d|--database)
|
||||||
|
database_name="$2"
|
||||||
|
shift # past argument
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
display_help
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Invalid option: $1"
|
||||||
|
display_help
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
shift # past argument or value
|
||||||
|
done
|
||||||
|
|
||||||
|
# Validate input
|
||||||
|
if [[ -z $database_name ]]; then
|
||||||
|
echo "Error: missing database name"
|
||||||
|
display_help
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [[ -z $option ]]; then
|
||||||
|
echo "Error: missing option"
|
||||||
|
display_help
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Backup or restore the database
|
||||||
|
if [[ $option == "backup" ]]; then
|
||||||
|
timestamp=$(date +%Y-%m-%d-%H-%M-%S)
|
||||||
|
backup_file="$database_name-$timestamp.sql"
|
||||||
|
echo "Backing up $database_name to $backup_file..."
|
||||||
|
pg_dump $database_name > $backup_file
|
||||||
|
echo "Backup complete."
|
||||||
|
elif [[ $option == "restore" ]]; then
|
||||||
|
backup_file="$database_name.sql"
|
||||||
|
echo "Deleting $database_name..."
|
||||||
|
dropdb $database_name
|
||||||
|
echo "Creating $database_name..."
|
||||||
|
createdb $database_name
|
||||||
|
echo "Restoring $database_name from $backup_file..."
|
||||||
|
psql $database_name < $backup_file
|
||||||
|
echo "Restore complete."
|
||||||
|
else
|
||||||
|
echo "Error: invalid option"
|
||||||
|
display_help
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
Reference in New Issue
Block a user