Backup from VPS to Remote Server
- 11 January 2016
- Volodymyr Hodiak
- Administration
- 5529
The article will be useful for website owners who have their hosting on cloud servers like DigitalOcean, or those who configure servers to their own needs without using a panel like ispmanager, cpanel, Vesta, etc.
It's been a long time since I decided to write the script, but I had no time for this. However, the number of websites on their own VPS is increasing so browsing the directories and making backups has become dull (the base is backed up automatically on a daily basis bt the automysqlbackup script, but the files were archived and moved manually).
There is no point in keeping backups on the same server with the website. You sleep soundly when the current version of the site is on your hard drive or another server.
Let's get started.
Just create a new folder for backups (/var/www/backup in my case), set up configs – path to the directory, access to the base and ftp.
The script itself:
#!/bin/sh # Company Otakoyi.com # Author wmgodyak mailto:wmgodyak@gmail.com # Config BACKUP=/var/www/backup BACKUP_DIR=/var/www/example.com #MySQL MUSER="root" MPASS="" MDB="" # FTP FTPD="/" FTPU="" FTPP="" FTPS="" # end config # Binaries TAR="$(which tar)" GZIP="$(which gzip)" FTP="$(which ftp)" # archive prefix NOW=$(date +%Y%m%d) DUMPFILE=full-$NOW.tar.gz # tmp dir mkdir $BACKUP/$NOW # backup site and db to tmp dir $TAR -czf $BACKUP/$NOW/site.tar.gz $BACKUP_DIR mysqldump -u$MUSER -p$MPASS $MDB > $BACKUP/$NOW/db.sql ARCHIVE=$BACKUP/$DUMPFILE ARCHIVED=$BACKUP/$NOW # backup all to one archive $TAR -zcvf $ARCHIVE $ARCHIVED # ftp cd $BACKUP $FTP -np $FTPS <
This simple script can create dump files; then they are added to an archive and sent to a remote FTP server.
Once the operations are done, it deletes the archive from the current server.
To get this work done automatically, place the script in /etc./cron.daily/ftpbackup.sh
You can download it here
.