In ubuntu 24, I have a postgresql database and I'm using vps, I want to create a daily backup for that database, how can I achieve that easily and free??
the database is small... not large one...
like creating a daily task that sends the database to ftp??? or any other easier solutions???
Assistant
To create a daily backup of your PostgreSQL database on your Ubuntu 24 VPS and automatically send it to an FTP server, you can set up a shell script that performs the backup and schedule it to run daily using Cron. Here's a step-by-step guide to achieve this easily and for free:
Step 1: Prepare Your Environment
Install Necessary Tools:
First, ensure that you have PostgreSQL's command-line tools and FTP client installed.
Replace /home/your_username/ with Your Actual User Home Directory Path.
Save and Exit the Crontab Editor.
Step 6: Verify the Cron Job
Cron will now execute the backup script daily at the specified time. You can verify Cron jobs with:
bash
crontab -l
Check the log file backup_postgresql.log periodically to monitor the backup process.
Additional Notes:
Security Considerations:
FTP Security: FTP transmits data in plain text, which is not secure. If possible, use SFTP (FTP over SSH) or FTPS (FTP over SSL/TLS) for encrypted transfers.
Using SFTP Instead of FTP:
Modify the upload section in your script to use scp or sftp:
bash
# Upload using scp
scp "$BACKUP_FILE" "$FTP_USER"@"$FTP_HOST":"$FTP_DIR/"
Protecting Passwords:
Avoid storing passwords in scripts. Use environment variables or configuration files with restricted permissions.
For FTP passwords, some clients support using a .netrc file.
Database User Permissions:
Ensure the database user used for backups has the necessary privileges to read the entire database.
Automated Cleanup:
The script includes a command to delete local backup files older than 7 days. Adjust the -mtime +7 value as needed.
Alternative: Using PostgreSQL's Built-in Tools
PostgreSQL offers backup tools like pg_basebackup for more complex backup needs, but for a small database, pg_dump is sufficient and straightforward.
Summary
By following these steps, you'll have an automated, daily backup of your PostgreSQL database that is uploaded to your FTP server. This solution is easy to set up, free, and ensures that your data is safely stored off-site.
If you have any questions or need further assistance with any of the steps, feel free to ask!