Skip to content

7️⃣ Backups & Log Rotation

sudo nano /usr/local/bin/gunbot-backup.sh
#!/bin/bash
# Backup all Gunbot instances (excluding runtime JSON)
for bot in gunbot-live gunbot-sim; do
  tar -czf /home/gunbot/backups/${bot}-$(date +%F).tar.gz /home/gunbot/${bot} --exclude='json'
done

# Keep backups for 7 days
find /home/gunbot/backups -type f -mtime +7 -delete
sudo chmod +x /usr/local/bin/gunbot-backup.sh
(crontab -l 2>/dev/null; echo "0 3 * * * /usr/local/bin/gunbot-backup.sh") | crontab -

Note:
Runs nightly at 3:00 AM, creating compressed backups for each Gunbot instance with automatic 7-day rotation.


📜 PM2 Log Rotation

PM2 includes a built-in log rotation module:

pm2 install pm2-logrotate
pm2 set pm2-logrotate:retain 7
pm2 set pm2-logrotate:max_size 50M
pm2 save

Note:
Keeps seven days of logs, limiting individual log files to 50 MB.


☁️ Optional — Off-VPS Backups

Improve reliability by syncing backups to another system or cloud.

Option A — Remote VPS or Secondary Server

rsync -avz -e "ssh -p 22" /home/gunbot/backups/ user@backupserver:/backups/gunbot/

Option B — Local Workstation

scp -r gunbot@your-vps-ip:/home/gunbot/backups ./gunbot_backups_local/

Option C — Cloud Storage (rclone)

sudo apt install rclone -y
rclone config
rclone copy /home/gunbot/backups remote:gunbot-backups

Note:
rclone supports encrypted, incremental syncs for cloud providers like Google Drive, Dropbox, and S3.


🕒 Automate Off-VPS Sync (Optional)

Add cron jobs to run after the 3 AM local backup:

# 3:30 AM — sync to remote VPS
(crontab -l 2>/dev/null; echo "30 3 * * * rsync -avz -e 'ssh -p 22' /home/gunbot/backups/ user@backupserver:/backups/gunbot/") | crontab -
# 3:45 AM — upload to cloud
(crontab -l 2>/dev/null; echo "45 3 * * * rclone copy /home/gunbot/backups remote:gunbot-backups") | crontab -

Note:
Adjust schedules or destinations as needed for your setup.