To backup data for websites you have a lot of solutions such as use FTP (FileZilla, WinSCP…), File Manager, crontab of Linux, backup wizard of Cpanel, Webmin…But to save your time you should setup backup website automatically with crontab and move data to a safe location.
Backup source code website
At first you must determine source code website’s location. In this example I will save source code website at /var/www/public_html. After that you use the following command to compress source code by date to /home/backup folder.
tar -zcvpf /home/backup/myweb-'date +%Y%m%d'.tar.gz /var/www/public_html
z : Compress the backup file with ‘gzip’ to make it small size.
c : Create a new backup archive
v : verbosely list files which are processed
p : Preserves the permissions of the files put in the archive for later restoration.
f : following is the archive file name
Backup database of a website
To backup database of a website you can use the following command:
mysqldump -u root -p123456 mydatabase| gzip > /var/www/home/backup/db/db-`date +%Y%m%d`.sql.gz
- mysqldump: command used to backup MySQL on Linux
- -u root -p123456: admin account of MySQL
- mydatabase: database name will be backuped.
- | : pipeline command symbol.
- gzip > : compress file with extension sql.gz, new file will be equal 1/5 original file.
- /var/www/home/backup/db/: location will save backup file.
- db-`date +%Y%m%d`.sql.gz: backup file name with file suffix is backup time such as: db-20160430.sql
Run backup automatically
To run backup command automatically you can use crontab command. Crontab (cron jobs) or cron schedule is a specific set of execution instructions specifing day, time and command to execute. crontab can have multiple execution statments.
Popular commands with crontab:
crontab -e Edit crontab file, or create one if it doesn’t already exist.
crontab -l crontab list of cronjobs , display crontab file contents.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
Below diagram will illustrate clearly about crontab command
Quite often, you will see an asterisk (*) instead of a number. This represents all possible numbers for that position. For example, asterisk in the minute position would make it run every minute.
01 * * * * root echo "This command is run at one min past every hour" 17 8 * * * root echo "This command is run daily at 8:17 am" 17 20 * * * root echo "This command is run daily at 8:17 pm" 00 4 * * 0 root echo "This command is run at 4 am every Sunday" * 4 * * Sun root echo "So is this" 42 4 1 * * root echo "This command is run 4:42 am every 1st of the month" 01 * 19 07 * root echo "This command is run hourly on the 19th of July"
Some of popular cronjobs:
@reboot: Run on reboot
@yearly: Run yearly ( 0 0 1 1 *)
@monthly: Run monthly ) 0 0 1 * *)
@weekly: Run weekly (0 0 * * 0)
@daily: Run daily (0 0 * * *)
@midnight: Run midnight daily
@hourly: run hourly (0 * * * *)
Backup source code automatically
In Ubuntu you type crontab -e and enter your crontab.
Below crontab will backup source code automatically daily on 3 am.
0 3 * * * tar -czf /home/backup/source-`date +\%Y\%m\%d-\%H\%M\%S`.tar.gz /var/www/mysite/public_html
Below crontab will backup MySQL automatically on 3 am.
0 3 * * * mysqldump -u root -p123456 mydb | gzip > /home/backup/db/db-`date +\%Y\%m\%d`.sql.gz