Got WordPress for years now and want to auto back it up.
Some one may of done this all ready but not sure. There my be a script out there all ready that does this.
Went to Bing AI and ask it to make this script put a rsync in it so it should only copy the changes.
Don't want it to copy the hole database. It's on a SSD and would were it out faster.
Can wright a post in WordPress and it only saves the changes so they must be a way.
This must not work right because if I delete all tables it will not restore all it's messed up then.
So I hope some one can look at this script and fix it.
I took the names and passwords out Replace names with a and b and passwords with x's.
Code:
#!/bin/bash
# Define the connection details for both servers
server_a="root@a"
server_b="root@b-backup"
# Define the path for the last backup file
last_backup_file="/root/last_backup.txt"
# Get the timestamp of the last backup
last_backup=$(cat $last_backup_file)
# Dump the database from server A with only new entries
mysqldump --no-tablespaces -u wordpressuser -pbxxxxx wordpress > dump.sql
# Replace all occurrences of the server A URL with the server B URL
sed -i 's%https://a/blog%http://b-backup%g' dump.sql
# Copy the dump file to server B
rsync -azvp -e ssh dump.sql $server_b:/root/dump.sql
# Import the dump file into the database on server B
ssh $server_b "mysql -u wordpress -pxxxxx wordpress_wp < /root/dump.sql"
# Update the last backup timestamp
date +%Y-%m-%d_%H:%M:%S > $last_backup_file
# Clean up the dump file
rm dump.sql
echo "Database updated successfully!"
It's be grate to have this work right.
All so I guess it will replace even the server name if have it in the post text. Just like it to change it from the media files. Audio, Video, and photos. Have a link in the database.
Used this one line command so server a and log on to server b with out a password.
Code:
cat ~/.ssh/id_rsa.pub | ssh root@b-backup "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
-Raymond Day