写一段定时备份PG数据库的脚本
时间: 2024-05-30 18:16:12 浏览: 65
以下是一段定时备份PG数据库的脚本:
#!/bin/bash
# Set the POSTGRES password
export PGPASSWORD="password"
# Set the backup directory
BACKUP_DIR="/var/backups/postgresql"
# Set the date format for the backup file name
DATE=`date +%Y-%m-%d_%H-%M-%S`
# Set the name of the backup file
BACKUP_FILE="pg_backup_$DATE.sql.gz"
# Set the name of the database to backup
DATABASE="mydatabase"
# Run the backup command and compress the output
pg_dump -U postgres $DATABASE | gzip > "$BACKUP_DIR/$BACKUP_FILE"
# Delete any backups older than 7 days
find $BACKUP_DIR -mtime +7 -type f -delete
# Echo a message to confirm the backup was successful
echo "Backup successful: $BACKUP_DIR/$BACKUP_FILE"
阅读全文